squareball 0.2.0.29-1f41 A general-purpose library for C99

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

All Data Structures Files Functions Variables Typedefs
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

  str | The string.
  suffix | The 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

  str | The string.
  c | The 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

  str | The 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

  str | The string.
  search | The character that should be replaced.
  replace | The 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

  str | The 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

  str | The string.
  c | The character that should be looked for.
  max_pieces | The 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

  str | The string.
  prefix | The 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

  str | The 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

  s | The string to be duplicated.

Returns
  A newly-allocated string.

Examples:
  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

  format | A printf(3) format.
  ... | One or more printf(3)-like parameters.

Returns
  A newly-allocated string.

Examples:
  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

  format | A printf(3) format.
  ap | A 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

  s | The string to be duplicated.
  n | Maximum 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

  strv | The 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

  strv | The NULL-terminated array of strings.
  separator | The 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

  strv | The NULL-terminated array of strings.

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