feat: (cstr) strisblank

This commit is contained in:
thetek 2023-02-12 17:34:54 +01:00
parent 260d5559f3
commit c87149338c
3 changed files with 29 additions and 0 deletions

View File

@ -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;
}

View File

@ -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_

View File

@ -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);
}