dynamic_string.h v0.2.2
A modern, efficient, single-file string library for C
|
Modern, efficient, single-file string library for C. More...
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
Go to the source code of this file.
Classes | |
struct | ds_codepoint_iter |
Unicode codepoint iterator for UTF-8 strings. More... | |
struct | ds_stringbuilder |
Macros | |
#define | DS_MALLOC malloc |
#define | DS_REALLOC realloc |
#define | DS_FREE free |
#define | DS_ASSERT assert |
#define | DS_ATOMIC_REFCOUNT 0 |
Enable atomic reference counting (default: 0) | |
#define | DS_DEF extern |
#define | DS_ATOMIC_SIZE_T size_t |
#define | DS_ATOMIC_FETCH_ADD(ptr, val) (*(ptr) += (val), *(ptr) - (val)) |
#define | DS_ATOMIC_FETCH_SUB(ptr, val) (*(ptr) -= (val), *(ptr) + (val)) |
#define | DS_ATOMIC_LOAD(ptr) (*(ptr)) |
#define | DS_ATOMIC_STORE(ptr, val) (*(ptr) = (val)) |
#define | ds_empty() ds_new("") |
#define | ds_from_literal(lit) ds_new(lit) |
Typedefs | |
typedef char * | ds_string |
String handle - points directly to null-terminated string data. | |
Functions | |
DS_DEF ds_string | ds_new (const char *text) |
Create a new string from a C string. | |
DS_DEF ds_string | ds_create_length (const char *text, size_t length) |
Create a string from a buffer with explicit length. | |
DS_DEF ds_string | ds_retain (ds_string str) |
Increment reference count and return shared handle. | |
DS_DEF void | ds_release (ds_string *str) |
Decrement reference count and free memory if last reference. | |
DS_DEF ds_string | ds_append (ds_string str, const char *text) |
Append text to a string. | |
DS_DEF ds_string | ds_append_char (ds_string str, uint32_t codepoint) |
Append a Unicode codepoint to a string. | |
DS_DEF ds_string | ds_prepend (ds_string str, const char *text) |
Prepend text to the beginning of a string. | |
DS_DEF ds_string | ds_insert (ds_string str, size_t index, const char *text) |
Insert text at a specific position in a string. | |
DS_DEF ds_string | ds_substring (ds_string str, size_t start, size_t len) |
Extract a substring from a string. | |
DS_DEF ds_string | ds_concat (ds_string a, ds_string b) |
Concatenate two strings. | |
DS_DEF ds_string | ds_join (ds_string *strings, size_t count, const char *separator) |
Join multiple strings with a separator. | |
DS_DEF size_t | ds_length (ds_string str) |
Get the length of a string in bytes. | |
DS_DEF int | ds_compare (ds_string a, ds_string b) |
Compare two strings lexicographically. | |
DS_DEF int | ds_compare_ignore_case (ds_string a, ds_string b) |
Compare two strings lexicographically (case-insensitive) | |
DS_DEF size_t | ds_hash (ds_string str) |
Calculate hash value for string. | |
DS_DEF int | ds_find (ds_string str, const char *needle) |
Find the first occurrence of a substring. | |
DS_DEF int | ds_find_last (ds_string str, const char *needle) |
Find the last occurrence of a substring. | |
DS_DEF int | ds_contains (ds_string str, const char *needle) |
Check if string contains a substring. | |
DS_DEF int | ds_starts_with (ds_string str, const char *prefix) |
Check if string starts with a prefix. | |
DS_DEF int | ds_ends_with (ds_string str, const char *suffix) |
Check if string ends with a suffix. | |
DS_DEF ds_string | ds_trim (ds_string str) |
Remove whitespace from both ends of a string. | |
DS_DEF ds_string | ds_trim_left (ds_string str) |
Remove whitespace from the beginning of a string. | |
DS_DEF ds_string | ds_trim_right (ds_string str) |
Remove whitespace from the end of a string. | |
DS_DEF ds_string | ds_replace (ds_string str, const char *old, const char *new) |
Replace the first occurrence of a substring. | |
DS_DEF ds_string | ds_replace_all (ds_string str, const char *old, const char *new) |
Replace all occurrences of a substring. | |
DS_DEF ds_string | ds_to_upper (ds_string str) |
Convert string to uppercase. | |
DS_DEF ds_string | ds_to_lower (ds_string str) |
Convert string to lowercase. | |
DS_DEF ds_string | ds_repeat (ds_string str, size_t times) |
Repeat a string multiple times. | |
DS_DEF ds_string | ds_truncate (ds_string str, size_t max_length, const char *ellipsis) |
Truncate string to maximum length with optional ellipsis. | |
DS_DEF ds_string | ds_reverse (ds_string str) |
Reverse a string (Unicode-aware) | |
DS_DEF ds_string | ds_pad_left (ds_string str, size_t width, char pad) |
Pad string on the left to reach specified width. | |
DS_DEF ds_string | ds_pad_right (ds_string str, size_t width, char pad) |
Pad string on the right to reach specified width. | |
DS_DEF ds_string * | ds_split (ds_string str, const char *delimiter, size_t *count) |
Split string into array by delimiter. | |
DS_DEF void | ds_free_split_result (ds_string *array, size_t count) |
Free the result array from ds_split() | |
DS_DEF ds_string | ds_format (const char *fmt,...) |
Create formatted string using printf-style format specifiers. | |
DS_DEF ds_string | ds_format_v (const char *fmt, va_list args) |
Create formatted string using printf-style format specifiers (va_list version) | |
DS_DEF ds_string | ds_escape_json (ds_string str) |
Escape string for JSON. | |
DS_DEF ds_string | ds_unescape_json (ds_string str) |
Unescape JSON string. | |
DS_DEF size_t | ds_refcount (ds_string str) |
Get the current reference count of a string. | |
DS_DEF int | ds_is_shared (ds_string str) |
Check if a string has multiple references. | |
DS_DEF int | ds_is_empty (ds_string str) |
Check if a string is empty. | |
DS_DEF ds_codepoint_iter | ds_codepoints (ds_string str) |
Create an iterator for Unicode codepoints in a string. | |
DS_DEF uint32_t | ds_iter_next (ds_codepoint_iter *iter) |
Get the next Unicode codepoint from iterator. | |
DS_DEF int | ds_iter_has_next (const ds_codepoint_iter *iter) |
Check if iterator has more codepoints. | |
DS_DEF size_t | ds_codepoint_length (ds_string str) |
Count the number of Unicode codepoints in a string. | |
DS_DEF uint32_t | ds_codepoint_at (ds_string str, size_t index) |
Get Unicode codepoint at specific index. | |
DS_DEF ds_stringbuilder | ds_builder_create (void) |
DS_DEF ds_stringbuilder | ds_builder_create_with_capacity (size_t capacity) |
DS_DEF void | ds_builder_destroy (ds_stringbuilder *sb) |
DS_DEF int | ds_builder_append (ds_stringbuilder *sb, const char *text) |
DS_DEF int | ds_builder_append_char (ds_stringbuilder *sb, uint32_t codepoint) |
DS_DEF int | ds_builder_append_string (ds_stringbuilder *sb, ds_string str) |
DS_DEF int | ds_builder_insert (ds_stringbuilder *sb, size_t index, const char *text) |
DS_DEF void | ds_builder_clear (ds_stringbuilder *sb) |
DS_DEF ds_string | ds_builder_to_string (ds_stringbuilder *sb) |
DS_DEF size_t | ds_builder_length (const ds_stringbuilder *sb) |
DS_DEF size_t | ds_builder_capacity (const ds_stringbuilder *sb) |
DS_DEF const char * | ds_builder_cstr (const ds_stringbuilder *sb) |
Modern, efficient, single-file string library for C.
A modern, efficient, single-file string library for C featuring:
Dual licensed under your choice of:
#define DS_ASSERT assert |
#define DS_ATOMIC_FETCH_ADD | ( | ptr, | |
val | |||
) | (*(ptr) += (val), *(ptr) - (val)) |
#define DS_ATOMIC_FETCH_SUB | ( | ptr, | |
val | |||
) | (*(ptr) -= (val), *(ptr) + (val)) |
#define DS_ATOMIC_LOAD | ( | ptr | ) | (*(ptr)) |
#define DS_ATOMIC_REFCOUNT 0 |
Enable atomic reference counting (default: 0)
#define DS_ATOMIC_SIZE_T size_t |
#define DS_ATOMIC_STORE | ( | ptr, | |
val | |||
) | (*(ptr) = (val)) |
#define DS_DEF extern |
#define ds_empty | ( | ) | ds_new("") |
#define DS_FREE free |
#define ds_from_literal | ( | lit | ) | ds_new(lit) |
#define DS_MALLOC malloc |
#define DS_REALLOC realloc |
typedef char* ds_string |
String handle - points directly to null-terminated string data.
This is a char* that points directly to UTF-8 string data. Metadata (refcount, length) is stored at negative offsets before the string data. This allows ds_string to be used directly with all C string functions.
Memory layout: [refcount|length|string_data|\0] ^ ds_string points here
Append text to a string.
str | Source string (must not be NULL) |
text | Text to append (must not be NULL) |
Append a Unicode codepoint to a string.
str | Source string (may be NULL) |
codepoint | Unicode codepoint to append (invalid codepoints become U+FFFD) |
DS_DEF int ds_builder_append | ( | ds_stringbuilder * | sb, |
const char * | text | ||
) |
DS_DEF int ds_builder_append_char | ( | ds_stringbuilder * | sb, |
uint32_t | codepoint | ||
) |
DS_DEF int ds_builder_append_string | ( | ds_stringbuilder * | sb, |
ds_string | str | ||
) |
DS_DEF size_t ds_builder_capacity | ( | const ds_stringbuilder * | sb | ) |
DS_DEF void ds_builder_clear | ( | ds_stringbuilder * | sb | ) |
DS_DEF ds_stringbuilder ds_builder_create | ( | void | ) |
DS_DEF ds_stringbuilder ds_builder_create_with_capacity | ( | size_t | capacity | ) |
DS_DEF const char * ds_builder_cstr | ( | const ds_stringbuilder * | sb | ) |
DS_DEF void ds_builder_destroy | ( | ds_stringbuilder * | sb | ) |
DS_DEF int ds_builder_insert | ( | ds_stringbuilder * | sb, |
size_t | index, | ||
const char * | text | ||
) |
DS_DEF size_t ds_builder_length | ( | const ds_stringbuilder * | sb | ) |
DS_DEF ds_string ds_builder_to_string | ( | ds_stringbuilder * | sb | ) |
Get Unicode codepoint at specific index.
str | String to access (may be NULL) |
index | Codepoint index (0-based) |
Count the number of Unicode codepoints in a string.
str | String to count (may be NULL) |
DS_DEF ds_codepoint_iter ds_codepoints | ( | ds_string | str | ) |
Create an iterator for Unicode codepoints in a string.
str | String to iterate over (may be NULL) |
Compare two strings lexicographically.
a | First string (must not be NULL) |
b | Second string (must not be NULL) |
Compare two strings lexicographically (case-insensitive)
a | First string (may be NULL) |
b | Second string (may be NULL) |
Concatenate two strings.
a | First string (may be NULL) |
b | Second string (may be NULL) |
Check if string contains a substring.
str | String to search in (may be NULL) |
needle | Substring to search for (may be NULL) |
Check if string ends with a suffix.
str | String to check (may be NULL) |
suffix | Suffix to look for (may be NULL) |
Escape string for JSON.
str | String to escape (may be NULL) |
Find the first occurrence of a substring.
str | String to search in (may be NULL) |
needle | Substring to search for (may be NULL) |
Find the last occurrence of a substring.
str | String to search in (may be NULL) |
needle | Substring to search for (may be NULL) |
Create formatted string using printf-style format specifiers.
fmt | Format string (may be NULL) |
... | Arguments for format specifiers |
Create formatted string using printf-style format specifiers (va_list version)
fmt | Format string (may be NULL) |
args | Variable argument list |
Free the result array from ds_split()
array | Array returned by ds_split() (may be NULL) |
count | Number of elements in array |
Calculate hash value for string.
str | String to hash (may be NULL) |
Insert text at a specific position in a string.
str | Source string (may be NULL) |
index | Byte position where to insert text (0-based) |
text | Text to insert (may be NULL) |
Check if a string is empty.
str | String to check (may be NULL) |
Check if a string has multiple references.
str | String to check (may be NULL) |
DS_DEF int ds_iter_has_next | ( | const ds_codepoint_iter * | iter | ) |
Check if iterator has more codepoints.
iter | Iterator to check (may be NULL) |
DS_DEF uint32_t ds_iter_next | ( | ds_codepoint_iter * | iter | ) |
Get the next Unicode codepoint from iterator.
iter | Iterator to advance (must not be NULL) |
Join multiple strings with a separator.
strings | Array of ds_string to join (may contain NULL entries) |
count | Number of strings in the array |
separator | Separator to insert between strings (may be NULL) |
Get the length of a string in bytes.
str | String to measure (must not be NULL) |
Pad string on the left to reach specified width.
str | String to pad (may be NULL) |
width | Target width in characters |
pad | Character to use for padding |
Pad string on the right to reach specified width.
str | String to pad (may be NULL) |
width | Target width in characters |
pad | Character to use for padding |
Prepend text to the beginning of a string.
str | Source string (may be NULL) |
text | Text to prepend (may be NULL) |
Get the current reference count of a string.
str | String to inspect (may be NULL) |
Repeat a string multiple times.
str | String to repeat (may be NULL) |
times | Number of repetitions |
Replace the first occurrence of a substring.
str | Source string (may be NULL) |
old | Substring to replace (may be NULL) |
new | Replacement text (may be NULL) |
Replace all occurrences of a substring.
str | Source string (may be NULL) |
old | Substring to replace (may be NULL) |
new | Replacement text (may be NULL) |
Reverse a string (Unicode-aware)
str | String to reverse (may be NULL) |
Split string into array by delimiter.
str | String to split (may be NULL) |
delimiter | Delimiter to split on (may be NULL) |
count | Output parameter for number of parts (may be NULL) |
Check if string starts with a prefix.
str | String to check (may be NULL) |
prefix | Prefix to look for (may be NULL) |
Extract a substring from a string.
str | Source string (may be NULL) |
start | Starting byte position (0-based) |
len | Number of bytes to include in substring |
Convert string to lowercase.
str | String to convert (may be NULL) |
Convert string to uppercase.
str | String to convert (may be NULL) |
Remove whitespace from both ends of a string.
str | String to trim (may be NULL) |
Remove whitespace from the beginning of a string.
str | String to trim (may be NULL) |
Remove whitespace from the end of a string.
str | String to trim (may be NULL) |
Truncate string to maximum length with optional ellipsis.
str | String to truncate (may be NULL) |
max_length | Maximum length in bytes (not including ellipsis) |
ellipsis | Ellipsis string to append if truncated (may be NULL) |