📄 lib_str.c
字号:
*
* Argument(s) : pstr Pointer to string (see Note #1).
*
* len_max Maximum number of characters to search (see Notes #2e & #3).
*
* srch_char Search character.
*
* Return(s) : Pointer to first occurrence of search character in string, if any.
*
* Pointer to NULL, otherwise.
*
* Caller(s) : Application.
*
* Note(s) : (1) String buffer NOT modified.
*
* (2) String search terminates when :
*
* (a) String pointer passed a NULL pointer.
* (1) No string search performed; NULL pointer returned.
*
* (b) String pointer points to NULL.
* (1) String overlaps with NULL address.
* (2) String searched up to but NOT beyond or including the NULL address.
*
* (c) String's terminating NULL character found.
* (1) Search character NOT found in search string; NULL pointer returned.
* (2) Applicable ONLY IF search character is NOT the terminating NULL character.
*
* (d) Search character found.
* (1) Return pointer to first occurrence of search character in search string.
*
* (e) 'len_max' number of characters searched.
* (1) 'len_max' number of characters does NOT include terminating NULL character.
*
* (3) Ideally, the 'len_max' parameter would be the last parameter in this function's
* paramter list for consistency with all other custom string library functions.
* However, the 'len_max' parameter is ordered to comply with the standard library
* function's parameter list.
*********************************************************************************************************
*/
CPU_CHAR *Str_Char_N (CPU_CHAR *pstr,
CPU_SIZE_T len_max,
CPU_CHAR srch_char)
{
CPU_CHAR *pstr_next;
CPU_SIZE_T len_srch;
if (pstr == (CPU_CHAR *)0) { /* Rtn NULL if srch str ptr NULL (see Note #2a). */
return ((CPU_CHAR *)0);
}
if (len_max == (CPU_SIZE_T)0) { /* Rtn NULL if srch len equals zero (see Note #2e). */
return ((CPU_CHAR *)0);
}
pstr_next = pstr;
pstr_next++;
len_srch = 0;
while (( pstr_next != (CPU_CHAR *)0) && /* Srch str until NULL ptr(s) (see Note #2b) ... */
(*pstr != (CPU_CHAR )0) && /* ... or NULL char (see Note #2c) ... */
(*pstr != (CPU_CHAR )srch_char) && /* ... or srch char found (see Note #2d); ... */
( len_srch < (CPU_SIZE_T)len_max)) { /* ... or max nbr chars srch'd (see Note #2e). */
pstr++;
pstr_next++;
len_srch++;
}
if (*pstr != srch_char) { /* If srch char NOT found, str points to NULL; ... */
return ((CPU_CHAR *)0); /* ... rtn NULL (see Notes #2b & #2c). */
}
return (pstr); /* Else rtn ptr to found srch char (see Note #2d). */
}
/*$PAGE*/
/*
*********************************************************************************************************
* Str_Char_Last()
*
* Description : Search string for last occurrence of specific character.
*
* Argument(s) : pstr Pointer to string (see Note #1).
*
* srch_char Search character.
*
* Return(s) : Pointer to last occurrence of search character in string, if any.
*
* Pointer to NULL, otherwise.
*
* Caller(s) : Application.
*
* Note(s) : (1) String buffer NOT modified.
*
* (2) String search terminates when :
*
* (a) String pointer passed a NULL pointer.
* (1) No string search performed; NULL pointer returned.
*
* (b) String pointer points to NULL.
* (1) String overlaps with NULL address.
* (2) String searched up to but NOT beyond or including the NULL address.
* (3) NULL address boundary handled in Str_Len().
*
* (c) String searched from end to beginning.
* (1) Search character NOT found in search string; NULL pointer returned.
* (2) Applicable ONLY IF search character is NOT the terminating NULL character.
*
* (d) Search character found.
* (1) Return pointer to first occurrence of search character in search string.
*********************************************************************************************************
*/
CPU_CHAR *Str_Char_Last (CPU_CHAR *pstr,
CPU_CHAR srch_char)
{
CPU_CHAR *pstr_next;
CPU_SIZE_T str_len;
if (pstr == (CPU_CHAR *)0) { /* Rtn NULL if srch str ptr NULL (see Note #2a). */
return ((CPU_CHAR *)0);
}
pstr_next = pstr;
str_len = Str_Len(pstr);
pstr_next += str_len;
while (( pstr_next != pstr) && /* Srch str from end until beg (see Note #2c) ... */
(*pstr_next != srch_char)) { /* ... until srch char found (see Note #2d). */
pstr_next--;
}
if (*pstr_next != srch_char) { /* If srch char NOT found, str points to NULL; ... */
return ((CPU_CHAR *)0); /* ... rtn NULL (see Notes #2b & #2c). */
}
return (pstr_next); /* Else rtn ptr to found srch char (see Note #2d). */
}
/*$PAGE*/
/*
*********************************************************************************************************
* Str_Str()
*
* Description : Search string for first occurence of a specific search string.
*
* Argument(s) : pstr Pointer to string (see Note #1).
*
* psrch_str Pointer to search string (see Note #1).
*
* Return(s) : Pointer to first occurrence of search string in string, if any.
*
* Pointer to NULL, otherwise.
*
* Caller(s) : Application.
*
* Note(s) : (1) String buffers NOT modified.
*
* (2) String search terminates when :
*
* (a) String pointer passed a NULL pointer.
* (1) No string search performed; NULL pointer returned.
*
* (b) Search string length greater than string length.
* (1) No string search performed; NULL pointer returned.
*
* (c) Search string length equal to zero.
* (1) NULL search string at end of string returned.
*
* (d) Entire string has been searched.
* (1) Maximum size of the search is defined as the subtraction of the
* search string length from the string length.
* (2) Search string not found; NULL pointer returned.
*
* (e) Search string found.
* (1) Search string found according to Str_Cmp_N() return value.
* (2) Return pointer to first occurrence of search string in string.
*********************************************************************************************************
*/
CPU_CHAR *Str_Str (CPU_CHAR *pstr,
CPU_CHAR *psrch_str)
{
CPU_SIZE_T str_len;
CPU_SIZE_T srch_str_len;
CPU_SIZE_T srch_len;
CPU_SIZE_T srch_ix;
CPU_BOOLEAN srch_done;
CPU_INT16S srch_cmp;
CPU_CHAR *pstr_srch_ix;
/* Rtn NULL if str ptr(s) NULL (see Note #2a). */
if (pstr == (CPU_CHAR *)0) {
return ((CPU_CHAR *)0);
}
if (psrch_str == (CPU_CHAR *)0) {
return ((CPU_CHAR *)0);
}
str_len = Str_Len(pstr);
srch_str_len = Str_Len(psrch_str);
if (srch_str_len > str_len) { /* If srch str len > str len, rtn NULL (see Note #2b). */
return ((CPU_CHAR *)0);
}
if (srch_str_len == 0) { /* If srch str len = 0, srch str equal NULL str; ... */
pstr_srch_ix = (CPU_CHAR *)(pstr + str_len); /* ... rtn ptr to NULL str found in str (see Note #2c). */
return (pstr_srch_ix);
}
srch_len = str_len - srch_str_len; /* Determine srch len (see Note #2d1). */
srch_ix = 0;
srch_done = DEF_NO;
while ((srch_done == DEF_NO) && (srch_ix <= srch_len)) {
pstr_srch_ix = (CPU_CHAR *)(pstr + srch_ix);
srch_cmp = Str_Cmp_N(pstr_srch_ix, psrch_str, srch_str_len);
srch_done = (srch_cmp == 0) ? DEF_YES : DEF_NO;
srch_ix++;
}
if (srch_cmp != 0) { /* If srch str NOT found, rtn NULL (see Note #2d). */
return ((CPU_CHAR *)0);
}
return (pstr_srch_ix); /* Rtn ptr to srch str found in str (see Note #2e). */
}
/*$PAGE*/
/*
*********************************************************************************************************
* Str_FmtNbr_Int32U()
*
* Description : Format unsigned integer into a multi-digit character string.
*
* Argument(s) : nbr Number to format.
*
* nbr_dig Number of digits to format (see Note #1).
*
* nbr_base Base of number to format (see Note #2).
*
* lead_zeros Prepend leading zeros (see Note #3) :
*
* DEF_NO Do NOT prepend leading zeros to string.
* DEF_YES Prepend leading zeros to string.
*
* lower_case Format alphabetic characters (if any) in lower case :
*
* DEF_NO Format alphabetic characters in upper case.
* DEF_YES Format alphabetic characters in lower case.
*
* nul Append terminating NULL-character (see Note #4) :
*
* DEF_NO Do NOT append terminating NULL-character to string.
* DEF_YES Append terminating NULL-character to string.
*
* pstr Pointer to character array to return formatted number string (see Note #5).
*
* Return(s) : Pointer to formatted string, if NO errors (see Note #6).
*
* Pointer to NULL, otherwise.
*
* Caller(s) : Application.
*
* Note(s) : (1) If the number of digits to format ('nbr_dig') is less than the number of significant
* integer digits of the number to format ('nbr'); then the most-significant digits of
* the formatted number will be truncated.
*
* Example :
*
* nbr = 23456
* nbr_dig = 3
*
* pstr = "456"
*
* (2) The number's base MUST be between 2 & 36, inclusive.
*
* (3) (a) Leading zeros option prepends leading '0's prior to the first non-zero digit.
* The number of leading zeros is such that the total number integer digits is
* equal to the requested number of integer digits to format ('nbr_dig').
*
* (b) (1) If leading zeros option is DISABLED, ...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -