X_STRING_VERSION_MAJOR
#define X_STRING_VERSION_MAJOR 1
STDX - Lightweight String Utilities
Part of the STDX General Purpose C Library by marciovmf
License: MIT
https://github.com/marciovmf/stdx
To compile the implementation define X_IMPL_STRING
in one source file before including this header.
This header provides: - C string helpers: case-insensitive prefix/suffix matching - XSmallstr: fixed-capacity, stack-allocated strings - XSlice: immutable, non-owning views into C strings - Tokenization and trimming - UTF-8-aware string length calculation - Fast substring and search operations - Case-sensitive and case-insensitive comparisons
#define X_STRING_VERSION_MAJOR 1
#define X_STRING_VERSION_MINOR 0
#define X_STRING_VERSION_PATCH 0
#define X_STRING_VERSION(X_STRING_VERSION_MAJOR *10000+X_STRING_VERSION_MINOR *100+X_STRING_VERSION_PATCH)
#define X_SMALLSTR_MAX_LENGTH 256
typedef struct{
const char *ptr;
size_t length;
}XSlice;
typedef struct{
const wchar_t *ptr;
size_t length;
}XWStrview;
typedef struct{
char buf[X_SMALLSTR_MAX_LENGTH+1];
size_t length;
}XSmallstr;
typedef struct{
wchar_t buf[X_SMALLSTR_MAX_LENGTH+1];
size_t length;
}XWSmallstr;
typedef enum XUtf8Error{
X_UTF8_OK=0,
X_UTF8_ERR_EOF=-1,
X_UTF8_ERR_INVALID=-2,
X_UTF8_ERR_OVERLONG=-3,
X_UTF8_ERR_RANGE=-4
}XUtf8Error;
Sets the process locale used by locale-aware string operations.
X_STRING_API void x_set_locale(const char *locale);
const char *localeNo return value.
Computes a 32-bit FNV hash for a null-terminated C-string.
X_STRING_API uint32_t x_cstr_hash(const char *str);
const char *str32 bit FNV hash
Searches for a substring within a null-terminated C-string.
X_STRING_API char * x_cstr_str(
const char *haystack,
const char *needle
);
const char *haystackconst char *needlepointer within haystack where needle is found
Checks whether the string ends with the specified suffix.
X_STRING_API bool x_cstr_ends_with(
const char *str,
const char *suffix
);
const char *strconst char *suffixtrue if the condition holds; false otherwise.
Checks whether a null-terminated C-string starts with the given prefix.
X_STRING_API bool x_cstr_starts_with(
const char *str,
const char *prefix
);
const char *strconst char *prefixtrue if the condition holds; false otherwise.
Case-insensitive check whether the string ends with the specified suffix.
X_STRING_API bool x_cstr_ends_with_ci(
const char *str,
const char *suffix
);
const char *strconst char *suffixtrue if the condition holds; false otherwise.
Checks whether a null-terminated C-string starts with the given prefix.
X_STRING_API bool x_cstr_starts_with_ci(
const char *str,
const char *prefix
);
const char *strconst char *prefixtrue if the condition holds; false otherwise.
Checks whether the string starts with the specified prefix.
X_STRING_API bool x_wcstr_starts_with(
const wchar_t *str,
const wchar_t *prefix
);
const wchar_t *strconst wchar_t *prefixtrue if the condition holds; false otherwise.
Checks whether a null-terminated wide C-string ends with the given suffix.
X_STRING_API bool x_wcstr_ends_with(
const wchar_t *str,
const wchar_t *suffix
);
const wchar_t *strconst wchar_t *suffixtrue if the condition holds; false otherwise.
Case-insensitive check whether the string starts with the specified prefix.
X_STRING_API bool x_wcstr_starts_with_ci(
const wchar_t *str,
const wchar_t *prefix
);
const wchar_t *strconst wchar_t *prefixtrue if the condition holds; false otherwise.
Checks whether a null-terminated wide C-string ends with the given suffix.
X_STRING_API bool x_wcstr_ends_with_ci(
const wchar_t *str,
const wchar_t *suffix
);
const wchar_t *strconst wchar_t *suffixtrue if the condition holds; false otherwise.
Case-insensitively compares two wide C-strings.
X_STRING_API int32_t x_wcstr_cicmp(
const wchar_t *a,
const wchar_t *b
);
const wchar_t *aconst wchar_t *bFunction-specific integer result.
Utility for null-terminated C-strings.
X_STRING_API size_t x_cstr_to_wcstr(
const char *src,
wchar_t *dest,
size_t max
);
const char *srcwchar_t *destsize_t maxNumber of units written or measured (excluding the final terminator, if any).
Converts between string encodings.
X_STRING_API size_t x_wcstr_to_utf8(
const wchar_t *wide,
char *utf8,
size_t max
);
const wchar_t *widechar *utf8size_t maxNumber of units written or measured (excluding the final terminator, if any).
Utility for null-terminated wide C-strings.
X_STRING_API size_t x_wcstr_to_cstr(
const wchar_t *src,
char *dest,
size_t max
);
const wchar_t *srcchar *destsize_t maxNumber of units written or measured (excluding the final terminator, if any).
Converts between string encodings.
X_STRING_API size_t x_utf8_to_wcstr(
const char *utf8,
wchar_t *wide,
size_t max
);
const char *utf8wchar_t *widesize_t maxNumber of units written or measured (excluding the final terminator, if any).
Returns the number of UTF-8 codepoints in a null-terminated string.
X_STRING_API size_t x_utf8_strlen(const char *utf8);
const char *utf8Number of UTF-8 codepoints.
Compares two null-terminated UTF-8 strings.
X_STRING_API int32_t x_utf8_strcmp(
const char *a,
const char *b
);
const char *aconst char *bNegative, zero, or positive according to lexicographic order.
Converts ASCII letters to lower case; leaves multibyte UTF-8 unchanged.
X_STRING_API char * x_utf8_tolower(
char *dest,
const char *src
);
char *destconst char *srcFunction-specific result.
Converts ASCII letters to upper case; leaves multibyte UTF-8 unchanged.
X_STRING_API char * x_utf8_toupper(
char *dest,
const char *src
);
char *destconst char *srcFunction-specific result.
Checks whether the string contains exactly one UTF-8 codepoint.
X_STRING_API bool x_utf8_is_single_char(const char *s);
const char *sFunction-specific result.
Decodes a single UTF-8 codepoint from the specified byte span.
X_STRING_API int32_t x_utf8_decode(
const char *ptr,
const char *end,
size_t *out_len
);
const char *ptrconst char *endsize_t *out_lenDecoded codepoint on success; negative X_UTF8_ERR_* on failure.
Returns the expected byte length for a UTF-8 starter byte.
X_STRING_API int32_t x_utf8_codepoint_length(unsigned char first_byte);
unsigned char first_byte1..4 for a valid starter byte; negative error code if invalid.
Initializes a fixed-capacity small string from a source C-string.
X_STRING_API int32_t x_smallstr_init(
XSmallstr *s,
const char *str
);
XSmallstr *sconst char *strFunction-specific integer result.
Formats text into a small string; truncates if necessary.
X_STRING_API size_t x_smallstr_format(
XSmallstr *s,
const char *fmt,
...
);
XSmallstr *sconst char *fmtparamNumber of units written or measured (excluding the final terminator, if any).
Copies a null-terminated C-string into a small string.
X_STRING_API size_t x_smallstr_from_cstr(
XSmallstr *s,
const char *cstr
);
XSmallstr *sconst char *cstrNumber of units written or measured (excluding the final terminator, if any).
Copies a string view into a small string.
X_STRING_API size_t x_smallstr_from_slice(
XSlice sv,
XSmallstr *out
);
Number of units written or measured (excluding the final terminator, if any).
Creates a non-owning view over a small string's contents.
X_STRING_API XSlice x_smallstr_to_slice(const XSmallstr *s);
const XSmallstr *sResulting view after the operation.
Returns the current byte length of the small string.
X_STRING_API size_t x_smallstr_length(const XSmallstr *s);
const XSmallstr *sNumber of elements in the string (excluding the final terminator).
Clears the small string to empty.
X_STRING_API void x_smallstr_clear(XSmallstr *s);
XSmallstr *sNo return value.
Appends a null-terminated C-string if space allows.
X_STRING_API size_t x_smallstr_append_cstr(
XSmallstr *s,
const char *cstr
);
XSmallstr *sconst char *cstrNumber of units written or measured (excluding the final terminator, if any).
Appends a single character if space allows.
X_STRING_API size_t x_smallstr_append_char(
XSmallstr *s,
char c
);
XSmallstr *schar cNumber of units written or measured (excluding the final terminator, if any).
Copies a byte-range substring into a small string.
X_STRING_API size_t x_smallstr_substring(
const XSmallstr *s,
size_t start,
size_t len,
XSmallstr *out
);
Number of units written or measured (excluding the final terminator, if any).
Trims leading ASCII whitespace in a small string.
X_STRING_API void x_smallstr_trim_left(XSmallstr *s);
XSmallstr *sResulting view after the operation.
Trims trailing ASCII whitespace in a small string.
X_STRING_API void x_smallstr_trim_right(XSmallstr *s);
XSmallstr *sNo return value.
Trims leading and trailing ASCII whitespace in a small string.
X_STRING_API void x_smallstr_trim(XSmallstr *s);
XSmallstr *sResulting view after the operation.
Compares two small strings case-insensitively.
X_STRING_API int32_t x_smallstr_cmp_ci(
const XSmallstr *a,
const XSmallstr *b
);
Negative, zero, or positive according to lexicographic order.
Replaces all occurrences of a substring in a small string.
X_STRING_API int32_t x_smallstr_replace_all(
XSmallstr *s,
const char *find,
const char *replace
);
XSmallstr *sconst char *findconst char *replaceFunction-specific integer result.
Counts UTF-8 codepoints in a small string.
X_STRING_API size_t x_smallstr_utf8_len(const XSmallstr *s);
const XSmallstr *sNumber of units written or measured (excluding the final terminator, if any).
Compares two small strings.
X_STRING_API int32_t x_smallstr_cmp(
const XSmallstr *a,
const XSmallstr *b
);
Negative, zero, or positive according to lexicographic order.
Compares a small string with a null-terminated C-string.
X_STRING_API int32_t x_smallstr_cmp_cstr(
const XSmallstr *a,
const char *b
);
const XSmallstr *aconst char *bNegative, zero, or positive according to lexicographic order.
Returns a pointer to the small string's null-terminated buffer.
X_STRING_API const char * x_smallstr_cstr(const XSmallstr *s);
const XSmallstr *sFunction-specific result.
Copies a UTF-8 codepoint-range substring into a small string.
X_STRING_API int32_t x_smallstr_utf8_substring(
const XSmallstr *s,
size_t start_cp,
size_t len_cp,
XSmallstr *out
);
Function-specific integer result.
Trims leading Unicode whitespace (by UTF-8 codepoints).
X_STRING_API void x_smallstr_utf8_trim_left(XSmallstr *s);
XSmallstr *sResulting view after the operation.
Trims trailing Unicode whitespace (by UTF-8 codepoints).
X_STRING_API void x_smallstr_utf8_trim_right(XSmallstr *s);
XSmallstr *sNo return value.
Trims leading and trailing Unicode whitespace (by UTF-8 codepoints).
X_STRING_API void x_smallstr_utf8_trim(XSmallstr *s);
XSmallstr *sResulting view after the operation.
Initializes a wide-character small string from a wide C-string.
X_STRING_API int32_t x_wsmallstr_init(
XWSmallstr *s,
const wchar_t *src
);
XWSmallstr *sconst wchar_t *srcFunction-specific integer result.
Compares two wide small strings.
X_STRING_API int32_t x_wsmallstr_cmp(
const XWSmallstr *a,
const XWSmallstr *b
);
const XWSmallstr *aconst XWSmallstr *bNegative, zero, or positive according to lexicographic order.
Compares a wide small string with a wide C-string.
X_STRING_API int32_t x_wsmallstr_cmp_cstr(
const XWSmallstr *a,
const wchar_t *b
);
const XWSmallstr *aconst wchar_t *bFunction-specific integer result.
Compares two wide small strings case-insensitively.
X_STRING_API int32_t x_wsmallstr_cmp_ci(
const XWSmallstr *a,
const XWSmallstr *b
);
const XWSmallstr *aconst XWSmallstr *bNegative, zero, or positive according to lexicographic order.
Returns the current length (in wide characters).
X_STRING_API size_t x_wsmallstr_length(const XWSmallstr *s);
const XWSmallstr *sNumber of elements in the string (excluding the final terminator).
Clears the wide small string to empty.
X_STRING_API void x_wsmallstr_clear(XWSmallstr *s);
XWSmallstr *sNo return value.
Appends a wide C-string if space allows.
X_STRING_API int32_t x_wsmallstr_append(
XWSmallstr *s,
const wchar_t *tail
);
XWSmallstr *sconst wchar_t *tailFunction-specific integer result.
Copies a wide-character substring into a destination small string.
X_STRING_API int32_t x_wsmallstr_substring(
const XWSmallstr *s,
size_t start,
size_t len,
XWSmallstr *out
);
const XWSmallstr *ssize_t startsize_t lenXWSmallstr *outFunction-specific integer result.
Trims leading and trailing whitespace in a wide small string.
X_STRING_API void x_wsmallstr_trim(XWSmallstr *s);
XWSmallstr *sNo return value.
Finds a wide character within a wide small string.
X_STRING_API int32_t x_wsmallstr_find(
const XWSmallstr *s,
wchar_t c
);
const XWSmallstr *swchar_t cIndex on success, or -1 if not found.
#define x_slice_empty() ((XSlice){.ptr=0, .length=0})
#define x_slice_is_empty(sv) ((sv).length==0)
#define x_slice_init(cstr, len) ((XSlice){.ptr=(cstr), .length=(len)})
#define x_slice(cstr) (x_slice_init((cstr), strlen(cstr)))
Compares two string views for equality.
X_STRING_API bool x_slice_eq(
XSlice a,
XSlice b
);
true if the condition holds; false otherwise.
Compares a string view with a C-string for equality.
X_STRING_API bool x_slice_eq_cstr(
XSlice a,
const char *b
);
XSlice aconst char *bFunction-specific result.
Compares two string views case-insensitively.
X_STRING_API bool x_slice_eq_ci(
XSlice a,
XSlice b
);
true if the condition holds; false otherwise.
Lexicographically compares two string views.
X_STRING_API int32_t x_slice_cmp(
XSlice a,
XSlice b
);
Negative, zero, or positive according to lexicographic order.
Lexicographically compares two string views case-insensitively.
X_STRING_API int32_t x_slice_cmp_ci(
XSlice a,
XSlice b
);
Function-specific integer result.
Creates a sub-view by byte range from a string view.
X_STRING_API XSlice x_slice_substr(
XSlice sv,
size_t start,
size_t len
);
XSlice svsize_t startsize_t lenResulting view after the operation.
Removes leading ASCII whitespace from a string view.
X_STRING_API XSlice x_slice_trim_left(XSlice sv);
XSlice svFunction-specific result.
Removes trailing ASCII whitespace from a string view.
X_STRING_API XSlice x_slice_trim_right(XSlice sv);
XSlice svResulting view after the operation.
Removes leading and trailing ASCII whitespace from a string view.
X_STRING_API XSlice x_slice_trim(XSlice sv);
XSlice svFunction-specific result.
Finds the first occurrence of a character in a string view.
X_STRING_API int32_t x_slice_find(
XSlice sv,
char c
);
XSlice svchar cIndex on success, or -1 if not found.
Finds the next ASCII whitespace in a string view.
X_STRING_API int32_t x_slice_find_white_space(XSlice sv);
XSlice svIndex on success, or -1 if not found.
Finds the last occurrence of a character in a string view.
X_STRING_API int32_t x_slice_rfind(
XSlice sv,
char c
);
XSlice svchar cIndex on success, or -1 if not found.
Splits a string view at the first occurrence of a delimiter.
X_STRING_API bool x_slice_split_at(
XSlice sv,
char delim,
XSlice *left,
XSlice *right
);
Function-specific result.
Splits a string view at the first ASCII whitespace.
X_STRING_API bool x_slice_split_at_white_space(
XSlice sv,
XSlice *left,
XSlice *right
);
Function-specific result.
Extracts the next token delimited by ASCII whitespace.
X_STRING_API bool x_slice_next_token_white_space(
XSlice *input,
XSlice *token
);
Function-specific result.
Extracts the next token delimited by a character.
X_STRING_API bool x_slice_next_token(
XSlice *input,
char delim,
XSlice *token
);
Function-specific result.
Checks whether a string view starts with the given C-string prefix.
X_STRING_API bool x_slice_starts_with_cstr(
XSlice sv,
const char *prefix
);
XSlice svconst char *prefixtrue if the condition holds; false otherwise.
Checks whether a string view ends with the given C-string suffix.
X_STRING_API bool x_slice_ends_with_cstr(
XSlice sv,
const char *prefix
);
XSlice svconst char *prefixtrue if the condition holds; false otherwise.
Creates a sub-view by byte range from a string view.
X_STRING_API XSlice x_slice_utf8_substr(
XSlice sv,
size_t char_start,
size_t char_len
);
XSlice svsize_t char_startsize_t char_lenFunction-specific result.
Removes leading Unicode whitespace (by UTF-8 codepoints).
X_STRING_API XSlice x_slice_utf8_trim_left(XSlice sv);
XSlice svResulting view after the operation.
Removes trailing ASCII whitespace from a string view.
X_STRING_API XSlice x_slice_utf8_trim_right(XSlice sv);
XSlice svFunction-specific result.
Removes leading and trailing Unicode whitespace (by UTF-8 codepoints).
X_STRING_API XSlice x_slice_utf8_trim(XSlice sv);
XSlice svResulting view after the operation.
Finds the first occurrence of a character in a string view.
X_STRING_API int32_t x_slice_utf8_find(
XSlice sv,
uint32_t codepoint
);
XSlice svuint32_t codepointIndex on success, or -1 if not found.
Finds the last occurrence of a UTF-8 codepoint in a string view.
X_STRING_API int32_t x_slice_utf8_rfind(
XSlice sv,
uint32_t codepoint
);
XSlice svuint32_t codepointIndex on success, or -1 if not found.
Splits a string view at the first occurrence of a delimiter.
X_STRING_API bool x_slice_utf8_split_at(
XSlice sv,
uint32_t delim,
XSlice *left,
XSlice *right
);
Function-specific result.
Extracts the next token delimited by a UTF-8 codepoint.
X_STRING_API bool x_slice_utf8_next_token(
XSlice *input,
uint32_t codepoint,
XSlice *token
);
Function-specific result.
Checks whether a string view starts with the given C-string prefix.
X_STRING_API bool x_slice_utf8_starts_with_cstr(
XSlice sv,
const char *prefix
);
XSlice svconst char *prefixtrue if the condition holds; false otherwise.
Checks whether a string view ends with the given C-string suffix.
X_STRING_API bool x_slice_utf8_ends_with_cstr(
XSlice sv,
const char *prefix
);
XSlice svconst char *prefixtrue if the condition holds; false otherwise.
#define x_wslice_init(cstr, len) ((XWStrview){.ptr=(cstr), .length=(len)})
#define x_wslice(cstr) (x_wslice_init((cstr), wcslen(cstr)))
Checks whether the wide string view is empty.
X_STRING_API bool x_wslice_empty(XWStrview sv);
XWStrview svFunction-specific result.
Compares two wide string views for equality.
X_STRING_API bool x_wslice_eq(
XWStrview a,
XWStrview b
);
true if the condition holds; false otherwise.
Lexicographically compares two wide string views.
X_STRING_API int32_t x_wslice_cmp(
XWStrview a,
XWStrview b
);
Function-specific integer result.
Creates a sub-view by range from a wide string view.
X_STRING_API XWStrview x_wslice_substr(
XWStrview sv,
size_t start,
size_t len
);
XWStrview svsize_t startsize_t lenResulting view after the operation.
Removes leading whitespace from a wide string view.
X_STRING_API XWStrview x_wslice_trim_left(XWStrview sv);
XWStrview svFunction-specific result.
Removes trailing whitespace from a wide string view.
X_STRING_API XWStrview x_wslice_trim_right(XWStrview sv);
XWStrview svResulting view after the operation.
Removes leading and trailing whitespace from a wide string view.
X_STRING_API XWStrview x_wslice_trim(XWStrview sv);
XWStrview svFunction-specific result.
Splits a wide string view at the first occurrence of a delimiter.
X_STRING_API bool x_wslice_split_at(
XWStrview sv,
uint32_t delim,
XWStrview *left,
XWStrview *right
);
Function-specific result.
Extracts the next token from a string view using the given delimiter.
X_STRING_API bool x_wslice_next_token(
XWStrview *input,
wchar_t delim,
XWStrview *token
);
Function-specific result.
Creates a non-owning string view from a null-terminated C-string.
X_STRING_API XSlice x_slice_from_cstr(const char *s);
const char *sXSlice pointing to the provided string, or an empty view if s is NULL.
Creates a non-owning string view from a fixed-capacity small string.
X_STRING_API XSlice x_slice_from_smallstr(const XSmallstr *s);
const XSmallstr *sXSlice referencing the contents of the small string, or an empty view if s is NULL.
Appends the contents of a string view to a small string.
X_STRING_API size_t x_smallstr_append_slice(
XSmallstr *s,
XSlice sv
);
New length of the small string in bytes after the operation (null-terminated).
Appends a fixed-length portion of a C-string to a small string.
X_STRING_API size_t x_smallstr_append_n(
XSmallstr *s,
const char *cstr,
size_t n
);
XSmallstr *sconst char *cstrsize_t nNew length of the small string in bytes after the operation.
Appends formatted text to a small string using a va_list.
X_STRING_API size_t x_smallstr_vappendf(
XSmallstr *s,
const char *fmt,
va_list args
);
XSmallstr *sconst char *fmtva_list argsNew length of the small string in bytes after the operation.
Appends formatted text to a small string.
X_STRING_API size_t x_smallstr_appendf(
XSmallstr *s,
const char *fmt,
...
);
XSmallstr *sconst char *fmt...New length of the small string in bytes after the operation.
Checks whether a string view contains the specified character.
X_STRING_API bool x_slice_contains_char(
XSlice sv,
char c
);
XSlice svchar ctrue if the character is found; false otherwise.
Checks whether a string view contains the specified UTF-8 codepoint.
X_STRING_API bool x_slice_contains_utf8(
XSlice sv,
uint32_t codepoint
);
XSlice svuint32_t codepointtrue if the codepoint is found; false otherwise.
Checks whether a small string contains the specified character.
X_STRING_API bool x_smallstr_contains_char(
const XSmallstr *s,
char c
);
const XSmallstr *schar ctrue if the character is found; false otherwise.
Concatenates multiple string views into a small string, inserting a separator between them.
X_STRING_API size_t x_smallstr_join(
XSmallstr *dst,
const XSlice *parts,
size_t count,
XSlice sep
);
New length of the small string after concatenation.
Returns the maximum number of bytes (excluding terminator) a small string can hold.
X_STRING_API size_t x_smallstr_capacity(void);
The compile-time capacity constant X_SMALLSTR_MAX_LENGTH.
Tests whether a small string is empty.
X_STRING_API bool x_smallstr_is_empty(const XSmallstr *s);
const XSmallstr *strue if the small string has zero length or is NULL; false otherwise.
Attempts to append a C-string to a small string up to its capacity.
X_STRING_API bool x_smallstr_try_append_cstr(
XSmallstr *s,
const char *cstr,
size_t *out_appended
);
XSmallstr *sconst char *cstrsize_t *out_appendedtrue if any bytes were appended; false if none or on error.
Creates a compile-time string view from a string literal.
#define X_STRVIEW(s) ((XSlice){(s), sizeof(s)-1u})
sXSlice representing the literal contents, excluding the null terminator.
#define strncasecmp _strnicmp
#define strnicmp _strnicmp
X_STRING_API size_t x_wide_to_utf8(
const wchar_t *wide,
char *utf8,
size_t max
);
X_STRING_API int32_t x_smallstr_find(
const XSmallstr *s,
char c
);
X_STRING_API int32_t x_smallstr_rfind(
const XSmallstr *s,
char c
);
X_STRING_API int32_t x_smallstr_split_at(
const XSmallstr *s,
char delim,
XSmallstr *left,
XSmallstr *right
);
X_STRING_API int32_t x_wsmallstr_from_wcstr(
XWSmallstr *s,
const wchar_t *src
);
X_STRING_API int32_t x_wsmallstr_next_token(
XWSmallstr *input,
wchar_t delim,
XWSmallstr *token
);