squareball  0.2.0.42-fe5f
A general-purpose library for C99
sb-strfuncs.h File Reference

String related helper functions. More...

#include <stdarg.h>
#include <stdbool.h>

Go to the source code of this file.

Functions

char * sb_strdup (const char *s)
 
char * sb_strndup (const char *s, size_t n)
 
char * sb_strdup_vprintf (const char *format, va_list ap)
 
char * sb_strdup_printf (const char *format,...)
 
bool sb_str_starts_with (const char *str, const char *prefix)
 
bool sb_str_ends_with (const char *str, const char *suffix)
 
char * sb_str_lstrip (char *str)
 
char * sb_str_rstrip (char *str)
 
char * sb_str_strip (char *str)
 
char ** sb_str_split (const char *str, char c, size_t max_pieces)
 
char * sb_str_replace (const char *str, const char search, const char *replace)
 
char * sb_str_find (const char *str, char c)
 
void sb_strv_free (char **strv)
 
char * sb_strv_join (char **strv, const char *separator)
 
size_t sb_strv_length (char **strv)
 

Detailed Description

String related helper functions.

Function Documentation

bool sb_str_ends_with ( const char *  str,
const char *  suffix 
)

Function that checks if a string ends with a given suffix.

Parameters
strThe string.
suffixThe suffix that should be looked for in the string.
Returns
A boolean that indicates if the string ends with the given suffix.
char* sb_str_find ( const char *  str,
char  c 
)

Function that returns a pointer to the first occurrence of character in a string.

This is somewhat similar to strchr, but respects '\' escaping.

Parameters
strThe string.
cThe character that should be searched in the string.
Returns
The pointer to the first occurrence of c in str.
char* sb_str_lstrip ( char *  str)

Function that strips whitespace from the beginning of a given string.

Parameters
strThe string.
Returns
A pointer to the new start of the string. Please note that the string is changed in-place, and no new memory is allocated.
char* sb_str_replace ( const char *  str,
const char  search,
const char *  replace 
)

Function that replaces all the occurences of a given character in a string with another given string.

Parameters
strThe string.
searchThe character that should be replaced.
replaceThe string that should replace all the occurences of c.
Returns
A newly-allocated string.
char* sb_str_rstrip ( char *  str)

Function that strips whitespace from the end of a given string.

Parameters
strThe string.
Returns
A pointer to the start of the string. It is usually the same as str. Please note that the string is changed in-place, and no new memory is allocated.
char** sb_str_split ( const char *  str,
char  c,
size_t  max_pieces 
)

Function that splits a string in all occurences of a given character, excluding this character from the resulting elements.

Parameters
strThe string.
cThe character that should be looked for.
max_piecesThe max number of pieces that should be splitted. After this, the character will be kept untouched. If 0, split until the end of the string.
Returns
An NULL-terminated array of strings, that should be free'd with sb_strv_free.
bool sb_str_starts_with ( const char *  str,
const char *  prefix 
)

Function that checks if a string starts with a given prefix.

Parameters
strThe string.
prefixThe prefix that should be looked for in the string.
Returns
A boolean that indicates if the string starts with the given prefix.
char* sb_str_strip ( char *  str)

Function that strips whitespace from the beginnning and from the end of a given string.

Parameters
strThe string.
Returns
A pointer to the new start of the string. Please note that the string is changed in-place, and no new memory is allocated.
char* sb_strdup ( const char *  s)

Replacement for glibc's strdup(3).

Parameters
sThe string to be duplicated.
Returns
A newly-allocated string.
Examples:
hello_custom_error_type.c, hello_slist.c, and hello_trie.c.
char* sb_strdup_printf ( const char *  format,
  ... 
)

Function that creates a dynamically allocated string, with a printf(3)-like interface.

Parameters
formatA printf(3) format.
...One or more printf(3)-like parameters.
Returns
A newly-allocated string.
Examples:
hello_custom_error_type.c, and hello_shell.c.
char* sb_strdup_vprintf ( const char *  format,
va_list  ap 
)

Function that creates a dynamically allocated string, with a vprintf(3)-like interface.

Parameters
formatA printf(3) format.
apA va_list variable, as used in vprintf(3).
Returns
A newly-allocated string.
char* sb_strndup ( const char *  s,
size_t  n 
)

Replacement for glibc's strndup(3).

Parameters
sThe string to be duplicated.
nMaximum number of bytes to copy from s
Returns
A newly-allocated string.
void sb_strv_free ( char **  strv)

Function that frees the memory allocated for a NULL-terminated array of strings.

Parameters
strvThe NULL-terminated array of strings.
Examples:
hello_configparser.c.
char* sb_strv_join ( char **  strv,
const char *  separator 
)

Function that joins a NULL-terminated array of strings in a single string, using a given string as separator.

Parameters
strvThe NULL-terminated array of strings.
separatorThe string to be used as separator.
Returns
A newly-allocated string.
Examples:
hello_configparser.c.
size_t sb_strv_length ( char **  strv)

Function that returns the number of elements in a NULL-terminated array of strings.

Parameters
strvThe NULL-terminated array of strings.
Returns
The length of the given NULL-terminated array of strings.