CLibs
Loading...
Searching...
No Matches
string_utils.h File Reference
#include "headers/attributes.h"
#include <regex.h>
#include <stdbool.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>

Go to the source code of this file.

Macros

#define strndup   string_nduplicate
#define strdup   string_duplicate
#define ESCAPED_CHARS   "\n\t\r\f\v\"\\"
 Charset of special characters that can be escaped.
#define STRSPLIT_EXCLUDE_EMPTY   ( 1 << 0 )
#define STRSPLIT_KEEP_DELIM_BEFORE   ( 1 << 1 )
#define STRSPLIT_KEEP_DELIM_AFTER   ( 1 << 2 )
#define STRSPLIT_STRIP_RESULTS   ( 1 << 3 )
 Only for string_split() (not regex)

Typedefs

typedef const char * string_t
 Immutable string.
typedef char * str_t
 Mutable string.
typedef unsigned int strsplit_mode_t
typedef const string_tstrjoin_strarr_t
 String array being passed to string_join may be typecast to this.

Functions

LibraryDefined UseResult str_t string_nduplicate (string_t s, size_t l)
 strndup implementation (standard in POSIX and C23+)
LibraryDefined UseResult str_t string_duplicate (string_t s)
 strdup implementation (standard in POSIX and C23+)
int vsnprintf (char *str, size_t size, const char *restrict format, va_list ap)
 va_list version of snprintf()
LibraryDefined int vasprintf (str_t *strp, const string_t fmt, va_list args)
LibraryDefined int asprintf (str_t *strp, const string_t fmt,...)
bool string_is_blank (string_t)
bool string_is_blank_l (string_t, size_t len)
bool string_is_empty (string_t s)
UseResult str_t string_stripped (string_t)
UseResult str_t string_stripped_l (string_t, size_t length)
void string_strip (str_t)
UseResult str_t string_escaped (string_t)
UseResult str_t string_unescaped (string_t)
 Opposite of string_escaped()
UseResult str_t string_reversed (string_t)
void string_reverse (str_t)
void string_to_upper (str_t)
UseResult str_t string_as_upper (string_t)
void string_to_lower (str_t)
UseResult str_t string_as_lower (string_t)
UseResult str_t string_replaced (string_t, string_t old, string_t new)
int string_replace (str_t string, string_t old, string_t new)
ssize_t string_split (str_t **str_arr_cont, string_t string, string_t split_tok, strsplit_mode_t mode)
ssize_t string_split_regex (str_t **str_arr_cont, string_t string, const regex_t *__restrict regexp, strsplit_mode_t mode)
void string_split_destroy (size_t size, str_t **str_arr_cont)
UseResult str_t string_join (size_t len, const string_t strarr[len], string_t joiner)
UseResult char * add_uint_strings (const char *, const char *)
UseResult char * mul_uint_strings (const char *, const char *)
void string_strip_lead_zeroes (str_t)
UseResult str_t hex_to_decimal (string_t)

Macro Definition Documentation

◆ ESCAPED_CHARS

#define ESCAPED_CHARS   "\n\t\r\f\v\"\\"

Charset of special characters that can be escaped.

◆ strdup

#define strdup   string_duplicate

◆ strndup

#define strndup   string_nduplicate

◆ STRSPLIT_EXCLUDE_EMPTY

#define STRSPLIT_EXCLUDE_EMPTY   ( 1 << 0 )

◆ STRSPLIT_KEEP_DELIM_AFTER

#define STRSPLIT_KEEP_DELIM_AFTER   ( 1 << 2 )

◆ STRSPLIT_KEEP_DELIM_BEFORE

#define STRSPLIT_KEEP_DELIM_BEFORE   ( 1 << 1 )

◆ STRSPLIT_STRIP_RESULTS

#define STRSPLIT_STRIP_RESULTS   ( 1 << 3 )

Only for string_split() (not regex)

Typedef Documentation

◆ str_t

typedef char* str_t

Mutable string.

◆ string_t

typedef const char* string_t

Immutable string.

◆ strjoin_strarr_t

typedef const string_t* strjoin_strarr_t

String array being passed to string_join may be typecast to this.

◆ strsplit_mode_t

typedef unsigned int strsplit_mode_t

Flags for the string_split[_regex] functions

- resulting string array doesn't include empty strings ("")
STRSPLIT_KEEP_DELIM_BEFORE // = 0x02
- items include the delimiting strings;
the delim is included at the end of the previous item
STRSPLIT_KEEP_DELIM_AFTER // = 0x04
- items include the delimiting strings;
the delim is included at the start of the next item
STRSPLIT_STRIP_RESULTS // = 0x08
- strips whitespace from either end of each entry
only for string_split() (not regex)
#define STRSPLIT_EXCLUDE_EMPTY
Definition string_utils.h:242

Function Documentation

◆ add_uint_strings()

UseResult char * add_uint_strings ( const char * ,
const char *  )

◆ asprintf()

LibraryDefined int asprintf ( str_t * strp,
const string_t fmt,
... )

Like sprintf(), except it heap-allocates memory for the resulting string. *strp may be passed to free(3) *

◆ hex_to_decimal()

UseResult str_t hex_to_decimal ( string_t )

◆ mul_uint_strings()

UseResult char * mul_uint_strings ( const char * ,
const char *  )

◆ string_as_lower()

UseResult str_t string_as_lower ( string_t )

Creates a new string with all uppercase letters replaced with lowercase ones

Returns
new string, should be freed

◆ string_as_upper()

UseResult str_t string_as_upper ( string_t )

Creates a new string with all lowercase letters replaced with uppercase ones

Returns
new string, should be freed

◆ string_duplicate()

LibraryDefined UseResult str_t string_duplicate ( string_t s)

strdup implementation (standard in POSIX and C23+)

◆ string_escaped()

UseResult str_t string_escaped ( string_t )

Escapes characters defined in ESCAPED_CHARS

Example:

#include "string_utils.h"
string_t old = "Hello, \"World\"!\n";
str_t esc = string_escaped( old );
printf( "\"%s\"\n", old );
printf( "\"%s\"\n", esc );
UseResult str_t string_escaped(string_t)
char * str_t
Mutable string.
Definition string_utils.h:24
const char * string_t
Immutable string.
Definition string_utils.h:22

prints

"Hello, "World"!
"
"Hello,\"World\"!\n"
Returns
New escaped string. Pointer should be freed with free(3).

◆ string_is_blank()

bool string_is_blank ( string_t )

Returns true if ascii string is blank

Returns
true if each char in s tests positive for isspace(3)

◆ string_is_blank_l()

bool string_is_blank_l ( string_t ,
size_t len )

Returns true if ascii string is blank

Parameters
lenmax string length
Returns
true if each char in s tests positive for isspace(3)

◆ string_is_empty()

bool string_is_empty ( string_t s)

Evaluates to true if string contains no characters

Parameters
sstring
Returns
true if string is empty – ""

◆ string_join()

UseResult str_t string_join ( size_t len,
const string_t strarr[len],
string_t joiner )

Joins a string array into one long string.

Parameters
lenlength of strarr
strarrarray of string (not modified in this function)
joinerstring to be put between each member of strarr
Returns
new heap-allocated string to be freed with free(3)

◆ string_nduplicate()

LibraryDefined UseResult str_t string_nduplicate ( string_t s,
size_t l )

strndup implementation (standard in POSIX and C23+)

◆ string_replace()

int string_replace ( str_t string,
string_t old,
string_t new )

Replaces all occurrences of a substring with another one. This is done in-place.

The substitution string must be the same length as the old substituted string. (for arbitrary lengths use string_replaced)

Parameters
stringthis string is modified
oldmust be the same length as new
newmust be the same length as old
Returns
RV_SUCCESS/RV_EXCEPTION

◆ string_replaced()

UseResult str_t string_replaced ( string_t ,
string_t old,
string_t new )

Creates a new string with all occurrences of the sub-string old replaced with new

Parameters
oldold substring
newnew substring
Returns
new string, should be freed

◆ string_reverse()

void string_reverse ( str_t )

Reverses the string. This is done in place.

◆ string_reversed()

UseResult str_t string_reversed ( string_t )

Creates a new string with the same characters as the supplied one, reversed.

Returns
New reversed string. Pointer should be freed with free(3).

◆ string_split()

ssize_t string_split ( str_t ** str_arr_cont,
string_t string,
string_t split_tok,
strsplit_mode_t mode )

Splits str at places matching split_tok

Substring must match perfectly

Parameters
str_arr_contcontainer for the string_array for the result; the resulting string array is allocated in this function
stringstring to be split
split_tokboundary
modesee strsplit_mode_t
Returns
number of strings in the result, RV_ERROR on allocation fail, RV_EXCEPTION if split_tok is invalid

◆ string_split_destroy()

void string_split_destroy ( size_t size,
str_t ** str_arr_cont )

free()s all memory allocated by string_split() or string_split_regex().

Also sets str_arr to NULL

Parameters
sizenumber of strings in *str_arr_cont
str_arr_contsame variable as the one passed to string_split()

◆ string_split_regex()

ssize_t string_split_regex ( str_t ** str_arr_cont,
string_t string,
const regex_t *__restrict regexp,
strsplit_mode_t mode )

Splits str at places matching regex
Substring must match regex

Parameters
str_arr_contcontainer for the string_array for the result; the resulting string array is allocated in this function
stringstring to be split
regexpboundary; compiled regex
modesee strsplit_mode_t
Returns
number of strings in the result, RV_ERROR on allocation fail or regex error, RV_EXCEPTION if a parameter is invalid

◆ string_strip()

void string_strip ( str_t )

Removes all whitespace from either end of the string. This is done in place.

string_stripped() is implemented separately from string_strip for memory efficiency

◆ string_strip_lead_zeroes()

void string_strip_lead_zeroes ( str_t )

◆ string_stripped()

UseResult str_t string_stripped ( string_t )

Heap-allocates a new string with all whitespace (as defined in isspace(3)) from either end stripped.

Returns
New string – copy of the original with no whitespace at either end. Pointer should be freed with free(3).

◆ string_stripped_l()

UseResult str_t string_stripped_l ( string_t ,
size_t length )

Heap-allocates a new string with all whitespace (as defined in isspace(3)) from either end stripped.

String is treated as if it was length characters long (at most, may be less)

Parameters
lengthmaximum number of characters read from string
Returns
New string – copy of the original with no whitespace at either end. Pointer should be freed with free(3).

◆ string_to_lower()

void string_to_lower ( str_t )

Replaces all uppercase letters to corresponding lowercase letters

◆ string_to_upper()

void string_to_upper ( str_t )

Replaces all lowercase letters to corresponding uppercase letters

◆ string_unescaped()

UseResult str_t string_unescaped ( string_t )

Opposite of string_escaped()

◆ vasprintf()

LibraryDefined int vasprintf ( str_t * strp,
const string_t fmt,
va_list args )

Like vsprintf(), except it heap-allocates memory for the resulting string. *strp may be passed to free(3)

◆ vsnprintf()

int vsnprintf ( char * str,
size_t size,
const char *restrict format,
va_list ap )

va_list version of snprintf()