feat: (cstr) strisblank
This commit is contained in:
parent
260d5559f3
commit
c87149338c
22
lib/cstr.c
22
lib/cstr.c
|
@ -140,3 +140,25 @@ strupcase (char *str)
|
|||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a string only consists of whitespace characters.
|
||||
*
|
||||
* @param str: the string to check
|
||||
*
|
||||
* @return true if the string only consists of whitespace characters, or false
|
||||
* if other characters are present. false in case of error.
|
||||
*
|
||||
* @errno EINVAL: `str` is a null pointer
|
||||
*/
|
||||
bool
|
||||
strisblank (const char *str)
|
||||
{
|
||||
if (str == NULL) return errno = EINVAL, false;
|
||||
|
||||
while (*str)
|
||||
if (!isspace (*str++))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef CUTILS_CSTR_H_
|
||||
#define CUTILS_CSTR_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
|
@ -10,5 +11,6 @@ ssize_t strtrimr (char *str); /* trim whitespace of a cstring on the right (end
|
|||
ssize_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 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 */
|
||||
|
||||
#endif // CUTILS_CSTR_H_
|
||||
|
|
|
@ -42,6 +42,11 @@ test_cstr (void)
|
|||
test_add (&group, test_assert (!strcmp (s4, "hello, world!\n")), "strdowncase");
|
||||
test_add (&group, test_assert (!strcmp (s5, "HELLO, WORLD!\n")), "strupcase");
|
||||
|
||||
char *s6 = " x ";
|
||||
char *s7 = " \r\n\t\v\f";
|
||||
test_add (&group, test_assert (!strisblank (s6)), "strisblank false");
|
||||
test_add (&group, test_assert (strisblank (s7)), "strisblank true");
|
||||
|
||||
return test_group_get_results (&group);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue