fix: (cstr) renovate cstr functions

This commit is contained in:
thetek 2023-02-15 09:43:27 +01:00
parent dc1e58971f
commit 261498b03f
2 changed files with 23 additions and 53 deletions

View File

@ -12,15 +12,11 @@
* *
* @return a pointer to the beginning of the trimmed string. the string is not * @return a pointer to the beginning of the trimmed string. the string is not
* copied, and the returned pointer points to somewhere within the * copied, and the returned pointer points to somewhere within the
* original string. returns null on error. * original string.
*
* @errno EINVAL: `str` is a null pointer
*/ */
char * char *
strtrim (char *str) strtrim (char *str)
{ {
if (str == NULL) return errno = EINVAL, NULL;
strtrimr (str); strtrimr (str);
return strtriml (str); return strtriml (str);
} }
@ -33,15 +29,11 @@ strtrim (char *str)
* *
* @return a pointer to the beginning of the trimmed string. the string is not * @return a pointer to the beginning of the trimmed string. the string is not
* copied, and the returned pointer points to somewhere within the * copied, and the returned pointer points to somewhere within the
* original string. returns null on error. * original string.
*
* @errno EINVAL: `str` is a null pointer
*/ */
char * char *
strtriml (char *str) strtriml (char *str)
{ {
if (str == NULL) return errno = EINVAL, NULL;
while (*str && isspace (*str)) while (*str && isspace (*str))
str++; str++;
@ -55,17 +47,13 @@ strtriml (char *str)
* *
* @param str: the string to trim * @param str: the string to trim
* *
* @return the new string length, or -1 on error. * @return the new string length.
*
* @errno EINVAL: `str` is a null pointer
*/ */
ssize_t size_t
strtrimr (char *str) strtrimr (char *str)
{ {
size_t len, i; size_t len, i;
if (str == NULL) return errno = EINVAL, -1;
i = len = strlen (str); i = len = strlen (str);
while (i <= len && isspace (str[i - 1])) while (i <= len && isspace (str[i - 1]))
i--; i--;
@ -80,17 +68,13 @@ strtrimr (char *str)
* @param str: the string to search in * @param str: the string to search in
* @param c: the character to search for * @param c: the character to search for
* *
* @return the amount of occurances, or -1 on error. * @return the amount of occurances of `c` in `str`
*
* @errno EINVAL: `str` is a null pointer
*/ */
ssize_t size_t
strcount (const char *str, char c) strcount (const char *str, char c)
{ {
size_t count; size_t count;
if (str == NULL) return errno = EINVAL, -1;
count = 0; count = 0;
while (*str) while (*str)
if (*str++ == c) if (*str++ == c)
@ -103,62 +87,48 @@ strcount (const char *str, char c)
* transform all uppercase letters of a string into lowercase letters. * transform all uppercase letters of a string into lowercase letters.
* *
* @param str: the string to convert * @param str: the string to convert
*
* @errno EINVAL: `str` is a null pointer
*/ */
void void
strdowncase (char *str) strdowncase (char *str)
{ {
if (str == NULL) while (*str)
errno = EINVAL; {
else if (isupper (*str))
while (*str) *str += 0x20;
{ str++;
if (isupper (*str)) }
*str += 0x20;
str++;
}
} }
/** /**
* transform all lowercase letters of a string into uppercase letters. * transform all lowercase letters of a string into uppercase letters.
* *
* @param str: the string to convert * @param str: the string to convert
*
* @errno EINVAL: `str` is a null pointer
*/ */
void void
strupcase (char *str) strupcase (char *str)
{ {
if (str == NULL) while (*str)
errno = EINVAL; {
else if (islower (*str))
while (*str) *str -= 0x20;
{ str++;
if (islower (*str)) }
*str -= 0x20;
str++;
}
} }
/** /**
* check if a string only consists of whitespace characters. * check if a string only consists of whitespace characters. whitespaces
* include the following characters: ' ', '\n', '\r', '\t', '\v', '\f'.
* *
* @param str: the string to check * @param str: the string to check
* *
* @return true if the string only consists of whitespace characters, or false * @return true if the string only consists of whitespace characters, or false
* if other characters are present. false in case of error. * if other characters are present.
*
* @errno EINVAL: `str` is a null pointer
*/ */
bool bool
strisblank (const char *str) strisblank (const char *str)
{ {
if (str == NULL) return errno = EINVAL, false;
while (*str) while (*str)
if (!isspace (*str++)) if (!isspace (*str++))
return false; return false;
return true; return true;
} }

View File

@ -7,8 +7,8 @@
char *strtrim (char *str); /* trim whitespace of a cstring (both beginning and end) */ char *strtrim (char *str); /* trim whitespace of a cstring (both beginning and end) */
char *strtriml (char *str); /* trim whitespace of a cstring on the left (beginning of string) */ char *strtriml (char *str); /* trim whitespace of a cstring on the left (beginning of string) */
ssize_t strtrimr (char *str); /* trim whitespace of a cstring on the right (end of string) */ size_t strtrimr (char *str); /* trim whitespace of a cstring on the right (end of string) */
ssize_t strcount (const char *str, char c); /* count number of occurances of a character within a string */ size_t strcount (const char *str, char c); /* count number of occurances of a character within a string */
void strdowncase (char *str); /* transform all uppercase letters of a string into lowercase letters */ void strdowncase (char *str); /* transform all uppercase letters of a string into lowercase letters */
void strupcase (char *str); /* transform all lowercase letters of a string into uppercase letters */ void strupcase (char *str); /* transform all lowercase letters of a string into uppercase letters */
bool strisblank (const char *str); /* check if a string only consists of whitespace characters */ bool strisblank (const char *str); /* check if a string only consists of whitespace characters */