2023-02-11 15:06:47 +01:00
|
|
|
#include "cstr.h"
|
|
|
|
#include <ctype.h>
|
2023-02-12 17:04:36 +01:00
|
|
|
#include <errno.h>
|
2023-02-11 15:06:47 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* trim whitespace of a cstring (both beginning and end). a null terminator
|
|
|
|
* will be placed after the last non-whitespace character. this modifies the
|
|
|
|
* string.
|
|
|
|
*
|
|
|
|
* @param str: the string to trim
|
|
|
|
*
|
|
|
|
* @return a pointer to the beginning of the trimmed string. the string is not
|
|
|
|
* copied, and the returned pointer points to somewhere within the
|
2023-02-12 17:10:26 +01:00
|
|
|
* original string. returns null on error.
|
|
|
|
*
|
|
|
|
* @errno EINVAL: `str` is a null pointer
|
2023-02-11 15:06:47 +01:00
|
|
|
*/
|
|
|
|
char *
|
|
|
|
strtrim (char *str)
|
|
|
|
{
|
2023-02-12 17:04:36 +01:00
|
|
|
if (str == NULL) return errno = EINVAL, NULL;
|
|
|
|
|
2023-02-11 15:06:47 +01:00
|
|
|
strtrimr (str);
|
|
|
|
return strtriml (str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* trim whitespace of a cstring on the left (beginning of string). the original
|
|
|
|
* string will be preserved.
|
|
|
|
*
|
|
|
|
* @param str: the string to trim
|
|
|
|
*
|
|
|
|
* @return a pointer to the beginning of the trimmed string. the string is not
|
|
|
|
* copied, and the returned pointer points to somewhere within the
|
2023-02-12 17:10:26 +01:00
|
|
|
* original string. returns null on error.
|
|
|
|
*
|
|
|
|
* @errno EINVAL: `str` is a null pointer
|
2023-02-11 15:06:47 +01:00
|
|
|
*/
|
|
|
|
char *
|
2023-02-11 16:25:24 +01:00
|
|
|
strtriml (char *str)
|
2023-02-11 15:06:47 +01:00
|
|
|
{
|
2023-02-12 17:04:36 +01:00
|
|
|
if (str == NULL) return errno = EINVAL, NULL;
|
|
|
|
|
2023-02-11 15:06:47 +01:00
|
|
|
while (*str && isspace (*str))
|
|
|
|
str++;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* trim whitespace of a cstring on the right (end of string). a null terminator
|
|
|
|
* will be placed after the last non-whitespace character. this modifies the
|
|
|
|
* string.
|
|
|
|
*
|
|
|
|
* @param str: the string to trim
|
|
|
|
*
|
2023-02-12 17:10:26 +01:00
|
|
|
* @return the new string length, or -1 on error.
|
|
|
|
*
|
|
|
|
* @errno EINVAL: `str` is a null pointer
|
2023-02-11 15:06:47 +01:00
|
|
|
*/
|
2023-02-12 17:04:36 +01:00
|
|
|
ssize_t
|
2023-02-11 15:06:47 +01:00
|
|
|
strtrimr (char *str)
|
|
|
|
{
|
|
|
|
size_t len, i;
|
|
|
|
|
2023-02-12 17:04:36 +01:00
|
|
|
if (str == NULL) return errno = EINVAL, -1;
|
|
|
|
|
2023-02-11 15:06:47 +01:00
|
|
|
i = len = strlen (str);
|
|
|
|
while (i <= len && isspace (str[i - 1]))
|
|
|
|
i--;
|
|
|
|
str[i] = '\0';
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
2023-02-12 16:53:02 +01:00
|
|
|
|
2023-02-12 17:10:26 +01:00
|
|
|
/**
|
|
|
|
* count number of occurances of a character within a string.
|
|
|
|
*
|
|
|
|
* @param str: the string to search in
|
|
|
|
* @param c: the character to search for
|
|
|
|
*
|
|
|
|
* @return the amount of occurances, or -1 on error.
|
|
|
|
*
|
|
|
|
* @errno EINVAL: `str` is a null pointer
|
|
|
|
*/
|
2023-02-12 17:04:36 +01:00
|
|
|
ssize_t
|
2023-02-12 16:53:02 +01:00
|
|
|
strcount (const char *str, char c)
|
|
|
|
{
|
|
|
|
size_t count;
|
|
|
|
|
2023-02-12 17:04:36 +01:00
|
|
|
if (str == NULL) return errno = EINVAL, -1;
|
|
|
|
|
2023-02-12 16:53:02 +01:00
|
|
|
count = 0;
|
|
|
|
while (*str)
|
|
|
|
if (*str++ == c)
|
|
|
|
count += 1;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2023-02-12 17:00:07 +01:00
|
|
|
|
2023-02-12 17:10:26 +01:00
|
|
|
/**
|
|
|
|
* transform all uppercase letters of a string into lowercase letters.
|
|
|
|
*
|
|
|
|
* @param str: the string to convert
|
|
|
|
*
|
|
|
|
* @errno EINVAL: `str` is a null pointer
|
|
|
|
*/
|
2023-02-12 17:00:07 +01:00
|
|
|
void
|
|
|
|
strdowncase (char *str)
|
|
|
|
{
|
2023-02-12 17:04:36 +01:00
|
|
|
if (str == NULL)
|
|
|
|
errno = EINVAL;
|
|
|
|
else
|
|
|
|
while (*str)
|
|
|
|
{
|
|
|
|
if (isupper (*str))
|
|
|
|
*str += 0x20;
|
|
|
|
str++;
|
|
|
|
}
|
2023-02-12 17:00:07 +01:00
|
|
|
}
|
|
|
|
|
2023-02-12 17:10:26 +01:00
|
|
|
/**
|
|
|
|
* transform all lowercase letters of a string into uppercase letters.
|
|
|
|
*
|
|
|
|
* @param str: the string to convert
|
|
|
|
*
|
|
|
|
* @errno EINVAL: `str` is a null pointer
|
|
|
|
*/
|
2023-02-12 17:00:07 +01:00
|
|
|
void
|
|
|
|
strupcase (char *str)
|
|
|
|
{
|
2023-02-12 17:04:36 +01:00
|
|
|
if (str == NULL)
|
|
|
|
errno = EINVAL;
|
|
|
|
else
|
|
|
|
while (*str)
|
|
|
|
{
|
|
|
|
if (islower (*str))
|
|
|
|
*str -= 0x20;
|
|
|
|
str++;
|
|
|
|
}
|
2023-02-12 17:00:07 +01:00
|
|
|
}
|
2023-02-12 17:34:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|