paul_string.h
IntroductionUnified ascii+wide string type for C/C++. DiscussionImplementation is included when PAUL_STRING_IMPLEMENTATION or PAUL_IMPLEMENTATION is defined.
Functions
str_append_charAppend a single ASCII character to an ASCII `str_t`. void str_append_char( str_t *s, char c); Parametersstr_char_atGet the ASCII character at the specified index. char str_char_at( const str_t s, size_t index); ParametersReturn ValueThe character, or 0 if out of bounds or not ASCII. str_cmpCompare two `str_t` values. Returns <0, 0, >0 similar to strcmp/wcscmp. Parametersstr_concatConcatenate src to the end of *dest (dest is resized as needed). void str_concat( str_t *dest, const str_t src); Parametersstr_copyCopy the contents of src into dest. Both must be the same underlying type. Parametersstr_dupDuplicate a string handle. ParametersReturn ValueA new `str_t` owning a copy of the string, or NULL on allocation failure. str_ends_withReturn true when s ends with suffix. bool str_ends_with( const str_t s, const str_t suffix); Parametersstr_equalEquality test between two `str_t` values. ParametersReturn Valuetrue when equal, false otherwise. str_eraseErase len characters from *s starting at pos. Parametersstr_findFind the first occurrence of substr in s. ParametersReturn ValueIndex of first match or (size_t)-1 if not found. str_find_charFind the first occurrence of ASCII character c in s (ASCII only). size_t str_find_char( const str_t s, char c); ParametersReturn ValueIndex or (size_t)-1 if not found. str_from_cstrCreate a `str_t` from a NUL-terminated C string (ASCII). str_t str_from_cstr( const char *cstr); ParametersReturn ValueA new `str_t` representing the same contents. DiscussionThis function is the target of the `str_from` / `str` helper macros when called with a `const char *` (see the macro documentation above). Use the macros when convenient; you may call this function directly when generics are not available. str_from_wcstrCreate a `str_t` from a NUL-terminated wide string (wchar_t). str_t str_from_wcstr( const wchar_t *cstr); ParametersReturn ValueA new `str_t` representing the same contents as wide characters. DiscussionThis function is the wide-string variant targeted by the `str_from` / `str` helper macros when called with `const wchar_t *`. str_insertInsert substr into *s at position pos. void str_insert( str_t *s, size_t pos, const str_t substr); Parametersstr_is_asciiQuery whether the `str_t` stores ASCII (narrow) characters. bool str_is_ascii( const str_t s); ParametersReturn Valuetrue when ASCII, false otherwise. str_is_utf16Query whether the `str_t` stores wide (wchar_t) characters. bool str_is_utf16( const str_t s); ParametersReturn Valuetrue when wide, false otherwise. str_lengthGet the length of the string in characters. size_t str_length( const str_t s); ParametersReturn ValueThe number of characters (not including NUL). str_make_asciiConvert a `str_t` to an ASCII representation. str_t str_make_ascii( str_t s); ParametersReturn ValueA new ASCII `str_t` or NULL on failure. DiscussionIf the string is already ASCII this returns a duplicate. If the source contains non-ASCII characters the function will return NULL. str_make_utf16Convert a `str_t` to a wide (wchar_t) representation. str_t str_make_utf16( str_t s); ParametersReturn ValueA new wide `str_t` or NULL on failure. DiscussionIf the string is already wide this returns a duplicate. str_raw_cstrReturn a raw NUL-terminated C string pointer for ASCII `str_t`. const char* str_raw_cstr( const str_t s); ParametersReturn ValuePointer to NUL-terminated char data when ASCII, otherwise NULL. str_raw_wcstrReturn a raw NUL-terminated wide string pointer for wide `str_t`. const wchar_t* str_raw_wcstr( const str_t s); ParametersReturn ValuePointer to NUL-terminated wchar_t data when wide, otherwise NULL. str_replaceReplace the first occurrence of old_sub in *s with new_sub. void str_replace( str_t *s, const str_t old_sub, const str_t new_sub); Parametersstr_resizeResize a `str_t` in-place to new length (truncates or extends with NUL). void str_resize( str_t *s, size_t new_len); Parametersstr_starts_withReturn true when s starts with prefix. bool str_starts_with( const str_t s, const str_t prefix); Parametersstr_to_lowerConvert the string in-place to lower-case. void str_to_lower( str_t *s); ParametersDiscussionWorks for ASCII and wide strings. str_to_upperConvert the string in-place to upper-case. void str_to_upper( str_t *s); ParametersDiscussionWorks for ASCII and wide strings. str_trimTrim leading and trailing whitespace (ASCII only) from *s. Parametersstr_wchar_atGet the wide character at the specified index. wchar_t str_wchar_at( const str_t s, size_t index); ParametersReturn ValueThe wide character, or 0 if out of bounds or not wide. str_wildcard_asciiMatch ASCII `str_t` against a glob-style pattern using '*', '?', and character classes like [a-z] or [^aeiou]. bool str_wildcard_ascii( const str_t s, const char *pattern); ParametersReturn Valuetrue on match, false otherwise. DiscussionThis is the ASCII helper invoked by the `str_wildcard` macro when `_Generic` is available and the argument is an ASCII `const char *`. str_wildcard_wideMatch wide `str_t` against a wide glob-style pattern (wchar_t pattern). bool str_wildcard_wide( const str_t s, const wchar_t *pattern); ParametersReturn Valuetrue on match, false otherwise. DiscussionThis is the wide helper invoked by the `str_wildcard` macro when `_Generic` is available and the argument is a `const wchar_t *`. Typedefs
str_tOpaque string handle used by the paul string API. typedef void* str_t; DiscussionA `str_t` points to an internal buffer allocated by the paul library that stores either an ASCII (char) or wide (wchar_t) string. The implementation stores a small header placed immediately before the returned data pointer to track the stored type and the character length. Callers must treat `str_t` as an opaque handle and use the provided API functions to manipulate strings. Macro Definitions
strShortcut wrapper that calls `str_from` and constructs a `str_t` from a literal or pointer. #define str(S) \ _Generic((S), \ const char *: str_from_cstr(S), \ const wchar_t *: str_from_wcstr(S), \ char *: str_from_cstr(S), \ wchar_t *: str_from_wcstr(S), \ default: NULL)(S) DiscussionSee `str_from` for details. This macro is handy when passing string literals directly (it will pick the correct constructor based on the literal's type). str_fromConvenience generic constructor that selects `str_from_cstr` or `str_from_wcstr`. #define str_from(S) \ _Generic((S), \ const char *: str_from_cstr, \ const wchar_t *: str_from_wcstr, \ char *: str_from_cstr, \ wchar_t *: str_from_wcstr, \ default: NULL)(S) DiscussionWhen the compiler supports C11 `_Generic` selections this macro dispatches at compile-time based on the type of the argument. It expands to `str_from_cstr(const char*)` for narrow C strings and to `str_from_wcstr(const wchar_t*)` for wide strings. If `_Generic` is not available then `PAUL_NO_GENERICS` is defined and these macros are not provided. str_wildcardGeneric wildcard matcher dispatcher. #define str_wildcard(S, P) \ _Generic((S), \ const char *: str_wildcard_ascii, \ char *: str_wildcard_ascii, \ const wchar_t *: str_wildcard_wide, \ wchar_t *: str_wildcard_wide, \ default: NULL)(S, P) DiscussionWhen `_Generic` is available this macro selects `str_wildcard_ascii` or `str_wildcard_wide` based on the type of the first argument. If `_Generic` is not available the macro is omitted and callers must call the correct helper directly. |