Dynamic Strings And String Helpers

Provides string helper structures/functions (roughly equivalent to std::string).

#include <util/dstr.h>

Dynamic String Structure (struct dstr)

struct dstr
char *dstr.array
size_t dstr.len
size_t dstr.capacity

General String Helper Functions

int astrcmpi(const char *str1, const char *str2)

Case insensitive string comparison function.


int wstrcmpi(const wchar_t *str1, const wchar_t *str2)

Case insensitive wide string comparison function.


int astrcmp_n(const char *str1, const char *str2, size_t n)

String comparison function for a specific number of characters.


int wstrcmp_n(const wchar_t *str1, const wchar_t *str2, size_t n)

Wide string comparison function for a specific number of characters.


int astrcmpi_n(const char *str1, const char *str2, size_t n)

Case insensitive string comparison function for a specific number of characters.


int wstrcmpi_n(const wchar_t *str1, const wchar_t *str2, size_t n)

Case insensitive wide string comparison function for a specific number of characters.


char *astrstri(const char *str, const char *find)

Case insensitive version of strstr.


wchar_t *wstrstri(const wchar_t *str, const wchar_t *find)

Case insensitive version of wcsstr.


char *strdepad(char *str)

Removes padding characters (tab, space, CR, LF) from the front and end of a string.


wchar_t *wcsdepad(wchar_t *str)

Removes padding characters (tab, space, CR, LF) from the front and end of a wide string.


char **strlist_split(const char *str, char split_ch, bool include_empty)

Splits a string in to a list of multiple sub-strings, terminated by NULL. If split_ch does not exist in the string, the first sub-string will be the same as str. Free with strlist_free().

Parameters:
  • str – The string to be split

  • split_ch – The delimiter

  • include_empty – If true, empty strings caused by having the split_ch right next to another will be included in the list. If false, they won’t be included.

Sample usage:

char **words = strlist_split("OBS    Studio", ' ', false);
int count = 0;

for (char **word = words; *word; ++word) {
        count++;
        blog(LOG_DEBUG, "%s", *word);
}

strlist_free(words);
// count == 2

void strlist_free(char **strlist)

Frees a string list created with strlist_split().


Dynamic String Functions

void dstr_init(struct dstr *dst)

Initializes a dynamic string variable (just zeroes the variable).

Parameters:
  • dst – Dynamic string to initialize


void dstr_init_move(struct dstr *dst, struct dstr *src)

Moves a src to dst without copying data and zeroes src.

Parameters:
  • dst – Destination

  • src – Source


void dstr_init_move_array(struct dstr *dst, char *str)

Sets a bmalloc-allocated string as the dynamic string without copying/reallocating.

Parameters:
  • dst – Dynamic string to initialize

  • str – bmalloc-allocated string


void dstr_init_copy(struct dstr *dst, const char *src)

Initializes a dynamic string with a copy of a string

Parameters:
  • dst – Dynamic string to initialize

  • src – String to copy


void dstr_init_copy_dstr(struct dstr *dst, const struct dstr *src)

Initializes a dynamic string with a copy of another dynamic string

Parameters:
  • dst – Dynamic string to initialize

  • src – Dynamic string to copy


void dstr_free(struct dstr *dst)

Frees a dynamic string.

Parameters:
  • dst – Dynamic string


void dstr_copy(struct dstr *dst, const char *array)

Copies a string.

Parameters:
  • dst – Dynamic string

  • array – String to copy


void dstr_copy_dstr(struct dstr *dst, const struct dstr *src)

Copies another dynamic string.

Parameters:
  • dst – Dynamic string

  • src – Dynamic string to copy


void dstr_ncopy(struct dstr *dst, const char *array, const size_t len)

Copies a specific number of characters from a string.

Parameters:
  • dst – Dynamic string

  • array – String to copy

  • len – Number of characters to copy


void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *src, const size_t len)

Copies a specific number of characters from another dynamic string.

Parameters:
  • dst – Dynamic string

  • src – Dynamic string to copy

  • len – Number of characters to copy


void dstr_resize(struct dstr *dst, const size_t num)

Sets the size of the dynamic string. If the new size is bigger than current size, zeroes the new characters.

Parameters:
  • dst – Dynamic string

  • num – New size


void dstr_reserve(struct dstr *dst, const size_t num)

Reserves a specific number of characters in the buffer (but does not change the size). Does not work if the value is smaller than the current reserve size.

Parameters:
  • dst – Dynamic string

  • num – New reserve size


bool dstr_is_empty(const struct dstr *str)

Returns whether the dynamic string is empty.

Parameters:
  • str – Dynamic string

Returns:

true if empty, false otherwise


void dstr_cat(struct dstr *dst, const char *array)

Concatenates a dynamic string.

Parameters:
  • dst – Dynamic string

  • array – String to concatenate with


void dstr_cat_dstr(struct dstr *dst, const struct dstr *str)

Concatenates a dynamic string with another dynamic string.

Parameters:
  • dst – Dynamic string to concatenate to

  • str – Dynamic string to concatenate with


void dstr_cat_ch(struct dstr *dst, char ch)

Concatenates a dynamic string with a single character.

Parameters:
  • dst – Dynamic string to concatenate to

  • ch – Character to concatenate


void dstr_ncat(struct dstr *dst, const char *array, const size_t len)

Concatenates a dynamic string with a specific number of characters from a string.

Parameters:
  • dst – Dynamic string to concatenate to

  • array – String to concatenate with

  • len – Number of characters to concatenate with


void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str, const size_t len)

Concatenates a dynamic string with a specific number of characters from another dynamic string.

Parameters:
  • dst – Dynamic string to concatenate to

  • str – Dynamic string to concatenate with

  • len – Number of characters to concatenate with


void dstr_insert(struct dstr *dst, const size_t idx, const char *array)

Inserts a string in to a dynamic string at a specific index.

Parameters:
  • dst – Dynamic string to insert in to

  • idx – Character index to insert at

  • array – String to insert


void dstr_insert_dstr(struct dstr *dst, const size_t idx, const struct dstr *str)

Inserts another dynamic string in to a dynamic string at a specific index.

Parameters:
  • dst – Dynamic string to insert in to

  • idx – Character index to insert at

  • str – Dynamic string to insert


void dstr_insert_ch(struct dstr *dst, const size_t idx, const char ch)

Inserts a character in to a dynamic string at a specific index.

Parameters:
  • dst – Dynamic string to insert in to

  • idx – Character index to insert at

  • ch – Character to insert


void dstr_remove(struct dstr *dst, const size_t idx, const size_t count)

Removes a specific number of characters starting from a specific index.

Parameters:
  • dst – Dynamic string

  • idx – Index to start removing characters at

  • count – Number of characters to remove


void dstr_printf(struct dstr *dst, const char *format, ...)
void dstr_vprintf(struct dstr *dst, const char *format, va_list args)

Sets a dynamic string to a formatted string.

Parameters:
  • dst – Dynamic string

  • format – Format string


void dstr_catf(struct dstr *dst, const char *format, ...)
void dstr_vcatf(struct dstr *dst, const char *format, va_list args)

Concatenates a dynamic string with a formatted string.

Parameters:
  • dst – Dynamic string

  • format – Format string


const char *dstr_find_i(const struct dstr *str, const char *find)

Finds a string within a dynamic string, case insensitive.

Parameters:
  • str – Dynamic string

  • find – String to find

Returns:

Pointer to the first occurrence, or NULL if not found


const char *dstr_find(const struct dstr *str, const char *find)

Finds a string within a dynamic string.

Parameters:
  • str – Dynamic string

  • find – String to find

Returns:

Pointer to the first occurrence, or NULL if not found


void dstr_replace(struct dstr *str, const char *find, const char *replace)

Replaces all occurrences of find with replace.

Parameters:
  • str – Dynamic string

  • find – String to find

  • replace – Replacement string


int dstr_cmp(const struct dstr *str1, const char *str2)

Compares with a string.

Parameters:
  • str1 – Dynamic string

  • str2 – String to compare

Returns:

0 if equal, nonzero otherwise


int dstr_cmpi(const struct dstr *str1, const char *str2)

Compares with a string, case-insensitive.

Parameters:
  • str1 – Dynamic string

  • str2 – String to compare

Returns:

0 if equal, nonzero otherwise


int dstr_ncmp(const struct dstr *str1, const char *str2, const size_t n)

Compares a specific number of characters.

Parameters:
  • str1 – Dynamic string

  • str2 – String to compare

  • n – Number of characters to compare

Returns:

0 if equal, nonzero otherwise


int dstr_ncmpi(const struct dstr *str1, const char *str2, const size_t n)

Compares a specific number of characters, case-insensitive.

Parameters:
  • str1 – Dynamic string

  • str2 – String to compare

  • n – Number of characters to compare

Returns:

0 if equal, nonzero otherwise


void dstr_depad(struct dstr *dst)

Removes all padding characters (tabs, spaces, CR, LF) from the front and ends of a dynamic string.

Parameters:
  • dst – Dynamic string


void dstr_left(struct dstr *dst, const struct dstr *str, const size_t pos)

Copies a certain number of characters from the left side of one dynamic string in to another.

Parameters:
  • dst – Destination

  • str – Source

  • pos – Number of characters


void dstr_mid(struct dstr *dst, const struct dstr *str, const size_t start, const size_t count)

Copies a certain number of characters from the middle of one dynamic string in to another.

Parameters:
  • dst – Destination

  • str – Source

  • start – Starting index within str

  • count – Number of characters to copy


void dstr_right(struct dstr *dst, const struct dstr *str, const size_t pos)

Copies a certain number of characters from the right of one dynamic string in to another.

Parameters:
  • dst – Destination

  • str – Source

  • pos – Index of str to copy from


char dstr_end(const struct dstr *str)
Parameters:
  • str – Dynamic string

Returns:

The last character of a dynamic string


void dstr_from_wcs(struct dstr *dst, const wchar_t *wstr)

Copies a wide string in to a dynamic string and converts it to UTF-8.

Parameters:
  • dst – Dynamic string

  • wstr – Wide string


wchar_t *dstr_to_wcs(const struct dstr *str)

Converts a dynamic array to a wide string. Free with bfree().

Parameters:
  • str – Dynamic string

Returns:

Wide string allocation. Free with bfree()


void dstr_to_upper(struct dstr *str)

Converts all characters within a dynamic array to uppercase.

Parameters:
  • str – Dynamic string


void dstr_to_lower(struct dstr *str)

Converts all characters within a dynamic array to lowercase.

Parameters:
  • str – Dynamic string