📄 lib_str.c
字号:
*
* (1) "The strlen() function shall return the length of 's' ('pstr');" ...
* (2) "no return value shall be reserved to indicate an error."
*
* (3) String length calculation terminates when :
*
* (a) String pointer points to NULL.
* (1) String buffer overlaps with NULL address.
* (2) String length calculated for string up to but NOT beyond or including
* the NULL address.
*
* (b) Terminating NULL character found.
* (1) String length calculated for string up to but NOT including
* the NULL character (see Note #2a2).
*
* (c) 'len_max' number of characters searched.
* (1) 'len_max' number of characters does NOT include the terminating NULL character.
*********************************************************************************************************
*/
CPU_SIZE_T Str_Len_N (const CPU_CHAR *pstr,
CPU_SIZE_T len_max)
{
const CPU_CHAR *pstr_len;
CPU_SIZE_T len;
pstr_len = pstr;
len = 0u;
while (( pstr_len != (const CPU_CHAR *) 0 ) && /* Calc str len until NULL ptr (see Note #3a) ... */
(*pstr_len != ( CPU_CHAR )'\0') && /* ... or NULL char found (see Note #3b) ... */
( len < ( CPU_SIZE_T)len_max)) { /* ... or max nbr chars srch'd (see Note #3c). */
pstr_len++;
len++;
}
return (len); /* Rtn str len (see Note #3b1). */
}
/*$PAGE*/
/*
*********************************************************************************************************
* Str_Copy()
*
* Description : Copy source string to destination string buffer.
*
* Argument(s) : pstr_dest Pointer to destination string buffer to receive source string copy (see Note #1a).
*
* pstr_src Pointer to source string to copy into destination string buffer (see Note #1b).
*
* Return(s) : Pointer to destination string, if NO error(s) [see Note #2b1].
*
* Pointer to NULL, otherwise (see Note #2b2A).
*
* Caller(s) : Application.
*
* Note(s) : (1) (a) Destination buffer size NOT validated; buffer overruns MUST be prevented by caller.
*
* (1) Destination buffer size MUST be large enough to accommodate the entire source
* string size including the terminating NULL character.
*
* (b) Source buffer NOT modified.
*
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'strcpy() : DESCRIPTION' states that :
*
* (1) "The strcpy() function shall copy the string pointed to by 's2' ('pstr_src')
* ... into the array pointed to by 's1' ('pstr_dest')" ...
* (2) "(including the terminating null byte)."
*
* (b) IEEE Std 1003.1, 2004 Edition, Section 'strcpy() : RETURN VALUE' states that :
*
* (1) "The strcpy() function shall return 's1' ('pstr_dest');" ...
* (2) "no return value is reserved to indicate an error."
* (A) #### This requirement is intentionally NOT implemented in order to return
* NULL for any error(s).
*
* (c) IEEE Std 1003.1, 2004 Edition, Section 'strcpy() : DESCRIPTION' states that "if
* copying takes place between objects that overlap, the behavior is undefined".
*
* (3) String copy terminates when :
*
* (a) Destination/Source string pointer(s) are passed NULL pointers.
* (1) No string copy performed; NULL pointer returned.
*
* (b) Destination/Source string pointer(s) point to NULL.
* (1) String buffer(s) overlap with NULL address; NULL pointer returned.
*
* (c) Source string's terminating NULL character found.
* (1) Entire source string copied into destination string buffer (see Note #2a).
*********************************************************************************************************
*/
CPU_CHAR *Str_Copy ( CPU_CHAR *pstr_dest,
const CPU_CHAR *pstr_src)
{
CPU_CHAR *pstr_rtn;
pstr_rtn = Str_Copy_N(pstr_dest,
pstr_src,
DEF_INT_CPU_U_MAX_VAL);
return (pstr_rtn);
}
/*$PAGE*/
/*
*********************************************************************************************************
* Str_Copy_N()
*
* Description : Copy source string to destination string buffer, up to a maximum number of characters.
*
* Argument(s) : pstr_dest Pointer to destination string buffer to receive source string copy (see Note #1a).
*
* pstr_src Pointer to source string to copy into destination string buffer (see Note #1b).
*
* len_max Maximum number of characters to copy (see Notes #2a2 & #3d).
*
* Return(s) : Pointer to destination string, if NO error(s) [see Note #2b1].
*
* Pointer to NULL, otherwise (see Note #2b2A).
*
* Caller(s) : Application.
*
* Note(s) : (1) (a) Destination buffer size NOT validated; buffer overruns MUST be prevented by caller.
*
* (1) Destination buffer size MUST be large enough to accommodate the entire source
* string size including the terminating NULL character.
*
* (b) Source string buffer NOT modified.
*
* (2) (a) (1) IEEE Std 1003.1, 2004 Edition, Section 'strncpy() : DESCRIPTION' states that :
*
* (A) "The strncpy() function shall copy ... the array pointed to by 's2'
* ('pstr_src') to the array pointed to by 's1' ('pstr_dest')"; ...
* (B) but "not more than 'n' ('len_max') bytes" ...
* (C) & "(bytes that follow a null byte are not copied)".
*
* (2) (A) IEEE Std 1003.1, 2004 Edition, Section 'strncpy() : DESCRIPTION' adds that
* "if the array pointed to by 's2' ('pstr_src') is a string that is shorter
* than 'n' ('len_max') bytes, null bytes shall be appended to the copy in
* the array pointed to by 's1' ('pstr_dest'), until 'n' ('len_max') bytes
* in all are written."
*
* (1) #### Since Str_Copy() limits the maximum number of characters to copy
* via Str_Copy_N() by the CPU's maximum number of addressable characters,
* this requirement is intentionally NOT implemented to avoid appending
* a potentially large number of unnecessary terminating NULL characters.
*
* (B) IEEE Std 1003.1, 2004 Edition, Section 'strncpy() : APPLICATION USAGE' also
* states that "if there is no null byte in the first 'n' ('len_max') bytes of
* the array pointed to by 's2' ('pstr_src'), the result is not null-terminated".
*
* (b) IEEE Std 1003.1, 2004 Edition, Section 'strncpy() : RETURN VALUE' states that :
*
* (1) "The strncpy() function shall return 's1' ('pstr_dest');" ...
* (2) "no return value is reserved to indicate an error."
* (A) #### This requirement is intentionally ignored in order to return NULL
* for any error(s).
*
* (c) IEEE Std 1003.1, 2004 Edition, Section 'strncpy() : DESCRIPTION' states that "if
* copying takes place between objects that overlap, the behavior is undefined".
*
* (3) String copy terminates when :
*
* (a) Destination/Source string pointer(s) are passed NULL pointers.
* (1) No string copy performed; NULL pointer returned.
*
* (b) Destination/Source string pointer(s) point to NULL.
* (1) String buffer(s) overlap with NULL address; NULL pointer returned.
*
* (c) Source string's terminating NULL character found.
* (1) Entire source string copied into destination string buffer (see Note #2a1A).
*
* (d) 'len_max' number of characters copied.
* (1) 'len_max' number of characters MAY include the terminating NULL character
* (see Note #2a1C).
* (2) Null copies allowed (i.e. zero-length copies).
* (A) No string copy performed; destination string returned (see Note #2b1).
*********************************************************************************************************
*/
/*$PAGE*/
CPU_CHAR *Str_Copy_N ( CPU_CHAR *pstr_dest,
const CPU_CHAR *pstr_src,
CPU_SIZE_T len_max)
{
CPU_CHAR *pstr_copy_dest;
const CPU_CHAR *pstr_copy_src;
CPU_SIZE_T len_copy;
/* Rtn NULL if str ptr(s) NULL (see Note #3a1). */
if (pstr_dest == (CPU_CHAR *)0) {
return ((CPU_CHAR *)0);
}
if (pstr_src == (const CPU_CHAR *)0) {
return ((CPU_CHAR *)0);
}
pstr_copy_dest = pstr_dest;
pstr_copy_src = pstr_src;
len_copy = 0u;
while (( pstr_copy_dest != ( CPU_CHAR *) 0 ) && /* Copy str until NULL ptr(s) [see Note #3b] ... */
( pstr_copy_src != (const CPU_CHAR *) 0 ) &&
(*pstr_copy_src != ( CPU_CHAR )'\0') && /* ... or NULL char found (see Note #3c); ... */
( len_copy < ( CPU_SIZE_T)len_max)) { /* ... or max nbr chars copied (see Note #3d). */
*pstr_copy_dest = *pstr_copy_src;
pstr_copy_dest++;
pstr_copy_src++;
len_copy++;
}
/* Rtn NULL if NULL ptr(s) found (see Note #3b1). */
if ((pstr_copy_dest == ( CPU_CHAR *)0) ||
(pstr_copy_src == (const CPU_CHAR *)0)) {
return ((CPU_CHAR *)0);
}
if (len_copy < len_max) { /* If copy str len < max buf len (see Note #2a2A), ... */
*pstr_copy_dest = (CPU_CHAR)'\0'; /* ... copy NULL char (see Note #3c1). */
}
return (pstr_dest); /* Rtn ptr to dest str (see Note #2b1). */
}
/*$PAGE*/
/*
*********************************************************************************************************
* Str_Cat()
*
* Description : Append concatenation string to destination string.
*
* Argument(s) : pstr_dest Pointer to destination string to append concatenation string (see Note #1a).
*
* pstr_cat Pointer to concatenation string to append to destination string (see Note #1b).
*
* Return(s) : Pointer to destination string, if NO error(s) [see Note #2b1].
*
* Pointer to NULL, otherwise (see Note #2b2A).
*
* Caller(s) : Application.
*
* Note(s) : (1) (a) Destination buffer size NOT validated; buffer overruns MUST be prevented by caller.
*
* (1) Destination buffer size MUST be large enough to accommodate the entire
* concatenated string size including the terminating NULL character.
*
* (b) Concatenation string buffer NOT modified.
*
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'strcat() : DESCRIPTION' states that :
*
* (1) "The strcat() function shall append a copy of the string pointed to by 's2'
* ('pstr_cat') ... to the end of the string pointed to by 's1' ('pstr_dest')."
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -