In development. It changes without notice.
Topic
Safe Strings
Copies, comparisons and searches of C text that stay inside the space they are given.
A copy or an append is cut to the space of its destination and always ends in a NUL. A comparison or a search takes a null pointer. Ignoring case means the letters A to Z and no others, so any bytes are safe.
- See also
- Fixed-Capacity Containers
Files | |
| file | src/ |
| Bounded, always-NUL-terminating C-string helper implementations. | |
| file | src/ |
| Bounded, always-NUL-terminating C-string helpers. | |
Functions | |
| size_t | kleidos:: |
| Copy a C string into a fixed-size buffer, always NUL-terminating. | |
| template<size_ | |
| size_t | kleidos:: |
| Capacity-deducing overload of copy for fixed-size arrays. | |
| template<size_ | |
| size_t | kleidos:: |
Capacity-deducing overload of copy for std::array. | |
| size_t | kleidos:: |
| Append a C string to a NUL-terminated buffer, always NUL-terminating. | |
| template<size_ | |
| size_t | kleidos:: |
| Capacity-deducing overload of append for fixed-size arrays. | |
| template<size_ | |
| size_t | kleidos:: |
Capacity-deducing overload of append for std::array. | |
| size_t | kleidos:: |
Bounded string length: never reads past maxLen bytes. | |
| size_t | kleidos:: |
| Null-safe length of a NUL-terminated C string. | |
| template<size_ | |
| size_t | kleidos:: |
| Capacity-deducing overload of length for fixed-size arrays. | |
| template<size_ | |
| size_t | kleidos:: |
Capacity-deducing overload of length for std::array. | |
| size_t | kleidos:: |
| Bounded, always-NUL-terminating printf-style formatter. | |
| size_t | kleidos:: |
| va_list overload of format for forwarding variadic arguments. | |
| bool | kleidos:: |
| Null-safe equality test for two NUL-terminated C strings. | |
| bool | kleidos:: |
Bounded null-safe equality test: never reads past maxLen bytes. | |
| bool | kleidos:: |
| Null-safe, ASCII case-insensitive equality test. | |
| bool | kleidos:: |
Test whether str begins with prefix (null-safe, bounded). | |
| bool | kleidos:: |
| ASCII case-insensitive variant of startsWith. | |
| const char * | kleidos:: |
| Null-safe search for the first occurrence of a character. | |
| const char * | kleidos:: |
| Null-safe search for the last occurrence of a character. | |
| const char * | kleidos:: |
| Null-safe search for the first occurrence of a substring. | |
| char * | kleidos:: |
| Mutable overload of findChar for non-const buffers. | |
| char * | kleidos:: |
| Mutable overload of findLastChar for non-const buffers. | |
Function Documentation
append() [1/3] #
Append a C string to a NUL-terminated buffer, always NUL-terminating.
Appends as many characters from src as fit, leaving room for the NUL. If dest is not NUL-terminated within destSize, it is force-terminated and nothing is appended.
- Parameters
-
Name Meaning dest Destination buffer holding a NUL-terminated string. destSize Capacity of destin bytes (including the NUL slot).src Source C string; treated as empty if nullptr.
- Returns
- Total length of the resulting string, excluding the terminating NUL.
append() [2/3] #
Capacity-deducing overload of append for fixed-size arrays.
- Template Parameters
-
Name Meaning N Destination array length, deduced by the compiler.
- Parameters
-
Name Meaning dest Destination array holding a NUL-terminated string. src Source C string; treated as empty if nullptr.
- Returns
- Total length of the resulting string, excluding the terminating NUL.
append() [3/3] #
Capacity-deducing overload of append for std::array.
Companion to the C-array overload above; see the std::array overload of copy for the rationale and the symmetry with the char[N] case.
- Template Parameters
-
Name Meaning N Destination array length, deduced by the compiler.
- Parameters
-
Name Meaning dest Destination std::array.src Source C string; treated as empty if nullptr.
- Returns
- Total length of the resulting string, excluding the terminating NUL.
copy() [1/3] #
Copy a C string into a fixed-size buffer, always NUL-terminating.
Writes at most destSize - 1 characters from src followed by a NUL. The source is never read beyond the destination capacity, so a non-terminated src cannot cause an out-of-bounds read past what fits in dest.
- Parameters
-
Name Meaning dest Destination buffer; ignored if nullptr. destSize Capacity of destin bytes (including the NUL slot).src Source C string; treated as empty if nullptr.
- Returns
- Number of characters written, excluding the terminating NUL.
copy() [2/3] #
Capacity-deducing overload of copy for fixed-size arrays.
- Template Parameters
-
Name Meaning N Destination array length, deduced by the compiler.
- Parameters
-
Name Meaning dest Destination array. src Source C string; treated as empty if nullptr.
- Returns
- Number of characters written, excluding the terminating NUL.
copy() [3/3] #
Capacity-deducing overload of copy for std::array.
Companion to the C-array overload above so call sites can pass std:: directly (the size is deduced from the type). Used widely by the vault and popup structs that own fixed-size string fields.
- Template Parameters
-
Name Meaning N Destination array length, deduced by the compiler.
- Parameters
-
Name Meaning dest Destination std::array.src Source C string; treated as empty if nullptr.
- Returns
- Number of characters written, excluding the terminating NUL.
equals() [1/2] #
Null-safe equality test for two NUL-terminated C strings.
Replaces the strcmp( a, b ) == 0 idiom and adds the null-safety the raw C function lacks: two nullptr arguments compare equal, and a nullptr versus a non-null string compares unequal (instead of being undefined behavior). When both pointers are non-null they must reference proper NUL-terminated strings.
- Parameters
-
Name Meaning a First string; may be nullptr. b Second string; may be nullptr.
- Return values
-
Value Meaning true Both strings are equal (or both nullptr). false Strings differ, or exactly one is nullptr.
equals() [2/2] #
Bounded null-safe equality test: never reads past maxLen bytes.
Replaces the strncmp( a, b, maxLen ) == 0 idiom for comparing fixed-width buffers that may not be NUL-terminated. Comparison stops at the first NUL or after maxLen bytes, whichever comes first, so neither operand is read out of bounds even when unterminated.
- Parameters
-
Name Meaning a First buffer; may be nullptr. b Second buffer; may be nullptr. maxLen Maximum number of bytes to compare.
- Return values
-
Value Meaning true The first maxLenbytes (up to a NUL) are equal.false Buffers differ within the cap, or have different NUL positions.
equalsIgnoreCase() #
Null-safe, ASCII case-insensitive equality test.
Replaces strcasecmp( a, b ) == 0. Only ASCII letters A–Z are folded to lowercase, so the result is locale-independent and well-defined on arbitrary byte sequences (e.g. UTF-8), unlike strcasecmp whose behavior depends on the current locale and is undefined for negative char values.
- Parameters
-
Name Meaning a First string; may be nullptr. b Second string; may be nullptr.
- Return values
-
Value Meaning true Strings are equal ignoring ASCII letter case. false Strings differ, or exactly one is nullptr.
findChar() [1/2] #
Mutable overload of findChar for non-const buffers.
Mirrors the C++ <cstring> convention of providing a char*-returning overload so callers that own a writable buffer (e.g. to split a field in place by writing a NUL at the delimiter) keep mutable access. The const_cast only re-applies the mutability the caller already held.
- Parameters
-
Name Meaning str String to search; may be nullptr. ch Character to find.
- Returns
- Pointer to the first match, or nullptr if not found /
stris null.
findChar() [2/2] #
Null-safe search for the first occurrence of a character.
Centralizes strchr behind one reviewed implementation and guards against a nullptr argument. As with strchr, searching for '\0' returns a pointer to the terminating NUL.
- Parameters
-
Name Meaning str String to search; may be nullptr. ch Character to find.
- Returns
- Pointer to the first match, or nullptr if not found /
stris null.
findLastChar() [1/2] #
Mutable overload of findLastChar for non-const buffers.
Mutable counterpart of findLastChar, following the same <cstring> convention as the mutable findChar overload above.
- Parameters
-
Name Meaning str String to search; may be nullptr. ch Character to find.
- Returns
- Pointer to the last match, or nullptr if not found /
stris null.
findLastChar() [2/2] #
Null-safe search for the last occurrence of a character.
Centralizes strrchr behind one reviewed implementation and guards against a nullptr argument. As with strrchr, searching for '\0' returns a pointer to the terminating NUL.
- Parameters
-
Name Meaning str String to search; may be nullptr. ch Character to find.
- Returns
- Pointer to the last match, or nullptr if not found /
stris null.
findSubstring() #
Null-safe search for the first occurrence of a substring.
Centralizes strstr behind one reviewed implementation and guards against nullptr arguments. As with strstr, an empty needle matches the start of haystack.
- Parameters
-
Name Meaning haystack String to search; may be nullptr. needle Substring to find; may be nullptr.
- Returns
- Pointer to the first match, or nullptr if not found / either is null.
format() #
Bounded, always-NUL-terminating printf-style formatter.
Drop-in replacement for snprintf at application call sites: formats into a fixed-size buffer, always writes a terminating NUL, and never reports a count larger than the buffer (the raw snprintf "characters that would have been written" footgun is clamped away, so the result is safe to use as an append offset). The format string is validated against the arguments at compile time through the printf function attribute, so the project-wide -Wformat=2 gate keeps type-checking every call site exactly as it does for raw snprintf.
No array-capacity-deducing overload is provided (unlike copy / append / length): the printf attribute requires fixed parameter indices and is silently dropped on a variadic template, which would disable -Wformat checking. Call sites therefore pass the buffer size explicitly (str::format( buf, sizeof( buf ), ... )), a mechanical change from snprintf( buf, sizeof( buf ), ... ).
- Parameters
-
Name Meaning dest Destination buffer; ignored if nullptr. destSize Capacity of destin bytes (including the NUL slot).fmt printf format string; writes an empty string if nullptr.
- Returns
- Number of characters written, excluding the terminating NUL (clamped to
destSize- 1). Returns 0 on nullptr/zero-size/ encoding error.
formatV() #
va_list overload of format for forwarding variadic arguments.
Use this from a module-local variadic helper that has already captured its arguments into a va_list (for example a thin printf-style logging shim).
- Parameters
-
Name Meaning dest Destination buffer; ignored if nullptr. destSize Capacity of destin bytes (including the NUL slot).fmt printf format string; writes an empty string if nullptr. args Variadic arguments captured by the caller.
- Returns
- Number of characters written, excluding the terminating NUL (clamped to
destSize- 1). Returns 0 on nullptr/zero-size/ encoding error.
length() [1/4] #
Null-safe length of a NUL-terminated C string.
Centralizes the single strlen call behind one reviewed implementation and guards against a nullptr argument (which would be undefined behavior for the raw C function). Prefer the bounded length overloads when the buffer capacity is known; use this only when the caller guarantees str is a proper NUL-terminated string of unbounded length (e.g. a string literal or a system-provided path).
- Parameters
-
Name Meaning str String to measure; treated as length 0 if nullptr.
- Returns
- Number of characters before the terminating NUL.
length() [2/4] #
Bounded string length: never reads past maxLen bytes.
Equivalent to strnlen: returns the number of characters before the first NUL, or maxLen if none is found within the cap.
- Parameters
-
Name Meaning str String to measure; treated as length 0 if nullptr. maxLen Maximum number of bytes to inspect.
- Returns
- min(distance to first NUL,
maxLen).
length() [3/4] #
Capacity-deducing overload of length for fixed-size arrays.
- Template Parameters
-
Name Meaning N Array length, deduced by the compiler.
- Parameters
-
Name Meaning str Array to measure.
- Returns
- min(distance to first NUL,
N).
length() [4/4] #
Capacity-deducing overload of length for std::array.
Companion to the C-array overload above; mirrors the std::array overloads of copy and append so the safe_string facade treats the two fixed-size containers uniformly.
- Template Parameters
-
Name Meaning N Array length, deduced by the compiler.
- Parameters
-
Name Meaning str std::arrayto measure.
- Returns
- min(distance to first NUL,
N).
startsWith() #
Test whether str begins with prefix (null-safe, bounded).
Replaces the strncmp( str, prefix, strlen( prefix ) ) == 0 idiom without the redundant length computation and without reading str past its NUL.
- Parameters
-
Name Meaning str String to inspect; may be nullptr. prefix Prefix to look for; may be nullptr. An empty prefix matches.
- Return values
-
Value Meaning true strstarts withprefix.false strdoes not start withprefix, or either is nullptr.
startsWithIgnoreCase() #
ASCII case-insensitive variant of startsWith.
Replaces the strncasecmp( str, prefix, strlen( prefix ) ) == 0 idiom. Folds only ASCII A–Z, matching the locale-independent guarantee of equalsIgnoreCase.
- Parameters
-
Name Meaning str String to inspect; may be nullptr. prefix Prefix to look for; may be nullptr. An empty prefix matches.
- Return values
-
Value Meaning true strstarts withprefixignoring ASCII letter case.false strdoes not start withprefix, or either is nullptr.