Kleidos API Search

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.

char buf[32];
kleidos::platform::str::copy( buf, "hello" );
kleidos::platform::str::append( buf, " world" );
See also
Fixed-Capacity Containers

Files

file src/util/safe_string.cpp
Bounded, always-NUL-terminating C-string helper implementations.
file src/util/safe_string.h
Bounded, always-NUL-terminating C-string helpers.

Functions

size_tkleidos::util::str::copy (char *dest, size_t destSize, const char *src)
Copy a C string into a fixed-size buffer, always NUL-terminating.
template<size_t N>
size_tkleidos::util::str::copy (char(&dest)[N], const char *src)
Capacity-deducing overload of copy for fixed-size arrays.
template<size_t N>
size_tkleidos::util::str::copy (std::array< char, N > &dest, const char *src)
Capacity-deducing overload of copy for std::array.
size_tkleidos::util::str::append (char *dest, size_t destSize, const char *src)
Append a C string to a NUL-terminated buffer, always NUL-terminating.
template<size_t N>
size_tkleidos::util::str::append (char(&dest)[N], const char *src)
Capacity-deducing overload of append for fixed-size arrays.
template<size_t N>
size_tkleidos::util::str::append (std::array< char, N > &dest, const char *src)
Capacity-deducing overload of append for std::array.
size_tkleidos::util::str::length (const char *str, size_t maxLen)
Bounded string length: never reads past maxLen bytes.
size_tkleidos::util::str::length (const char *str)
Null-safe length of a NUL-terminated C string.
template<size_t N>
size_tkleidos::util::str::length (const char(&str)[N])
Capacity-deducing overload of length for fixed-size arrays.
template<size_t N>
size_tkleidos::util::str::length (const std::array< char, N > &str)
Capacity-deducing overload of length for std::array.
size_tkleidos::util::str::format (char *dest, size_t destSize, const char *fmt,...)
Bounded, always-NUL-terminating printf-style formatter.
size_tkleidos::util::str::formatV (char *dest, size_t destSize, const char *fmt, va_list args)
va_list overload of format for forwarding variadic arguments.
boolkleidos::util::str::equals (const char *a, const char *b)
Null-safe equality test for two NUL-terminated C strings.
boolkleidos::util::str::equals (const char *a, const char *b, size_t maxLen)
Bounded null-safe equality test: never reads past maxLen bytes.
boolkleidos::util::str::equalsIgnoreCase (const char *a, const char *b)
Null-safe, ASCII case-insensitive equality test.
boolkleidos::util::str::startsWith (const char *str, const char *prefix)
Test whether str begins with prefix (null-safe, bounded).
boolkleidos::util::str::startsWithIgnoreCase (const char *str, const char *prefix)
ASCII case-insensitive variant of startsWith.
const char *kleidos::util::str::findChar (const char *str, char ch)
Null-safe search for the first occurrence of a character.
const char *kleidos::util::str::findLastChar (const char *str, char ch)
Null-safe search for the last occurrence of a character.
const char *kleidos::util::str::findSubstring (const char *haystack, const char *needle)
Null-safe search for the first occurrence of a substring.
char *kleidos::util::str::findChar (char *str, char ch)
Mutable overload of findChar for non-const buffers.
char *kleidos::util::str::findLastChar (char *str, char ch)
Mutable overload of findLastChar for non-const buffers.

Back to the top

Function Documentation

append() [1/3] #

size_t kleidos::util::str::append(char * dest, size_t destSize, const char * src)

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
NameMeaning
destDestination buffer holding a NUL-terminated string.
destSizeCapacity of dest in bytes (including the NUL slot).
srcSource C string; treated as empty if nullptr.
Returns
Total length of the resulting string, excluding the terminating NUL.

append() [2/3] #

template<size_t N>
size_t kleidos::util::str::append(char(&) dest[N], const char * src)
inline

Capacity-deducing overload of append for fixed-size arrays.

Template Parameters
NameMeaning
NDestination array length, deduced by the compiler.
Parameters
NameMeaning
destDestination array holding a NUL-terminated string.
srcSource C string; treated as empty if nullptr.
Returns
Total length of the resulting string, excluding the terminating NUL.

append() [3/3] #

template<size_t N>
size_t kleidos::util::str::append(std::array< char, N > & dest, const char * src)
inline

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
NameMeaning
NDestination array length, deduced by the compiler.
Parameters
NameMeaning
destDestination std::array.
srcSource C string; treated as empty if nullptr.
Returns
Total length of the resulting string, excluding the terminating NUL.

copy() [1/3] #

size_t kleidos::util::str::copy(char * dest, size_t destSize, const char * src)

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
NameMeaning
destDestination buffer; ignored if nullptr.
destSizeCapacity of dest in bytes (including the NUL slot).
srcSource C string; treated as empty if nullptr.
Returns
Number of characters written, excluding the terminating NUL.

copy() [2/3] #

template<size_t N>
size_t kleidos::util::str::copy(char(&) dest[N], const char * src)
inline

Capacity-deducing overload of copy for fixed-size arrays.

Template Parameters
NameMeaning
NDestination array length, deduced by the compiler.
Parameters
NameMeaning
destDestination array.
srcSource C string; treated as empty if nullptr.
Returns
Number of characters written, excluding the terminating NUL.

copy() [3/3] #

template<size_t N>
size_t kleidos::util::str::copy(std::array< char, N > & dest, const char * src)
inline

Capacity-deducing overload of copy for std::array.

Companion to the C-array overload above so call sites can pass std::array<char, N> directly (the size is deduced from the type). Used widely by the vault and popup structs that own fixed-size string fields.

Template Parameters
NameMeaning
NDestination array length, deduced by the compiler.
Parameters
NameMeaning
destDestination std::array.
srcSource C string; treated as empty if nullptr.
Returns
Number of characters written, excluding the terminating NUL.

equals() [1/2] #

bool kleidos::util::str::equals(const char * a, const char * b)

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
NameMeaning
aFirst string; may be nullptr.
bSecond string; may be nullptr.
Return values
ValueMeaning
trueBoth strings are equal (or both nullptr).
falseStrings differ, or exactly one is nullptr.

equals() [2/2] #

bool kleidos::util::str::equals(const char * a, const char * b, size_t maxLen)

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
NameMeaning
aFirst buffer; may be nullptr.
bSecond buffer; may be nullptr.
maxLenMaximum number of bytes to compare.
Return values
ValueMeaning
trueThe first maxLen bytes (up to a NUL) are equal.
falseBuffers differ within the cap, or have different NUL positions.

equalsIgnoreCase() #

bool kleidos::util::str::equalsIgnoreCase(const char * a, const char * b)

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
NameMeaning
aFirst string; may be nullptr.
bSecond string; may be nullptr.
Return values
ValueMeaning
trueStrings are equal ignoring ASCII letter case.
falseStrings differ, or exactly one is nullptr.

findChar() [1/2] #

char * kleidos::util::str::findChar(char * str, char ch)
inline

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
NameMeaning
strString to search; may be nullptr.
chCharacter to find.
Returns
Pointer to the first match, or nullptr if not found / str is null.

findChar() [2/2] #

const char * kleidos::util::str::findChar(const char * str, char ch)

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
NameMeaning
strString to search; may be nullptr.
chCharacter to find.
Returns
Pointer to the first match, or nullptr if not found / str is null.

findLastChar() [1/2] #

char * kleidos::util::str::findLastChar(char * str, char ch)
inline

Mutable overload of findLastChar for non-const buffers.

Mutable counterpart of findLastChar, following the same <cstring> convention as the mutable findChar overload above.

Parameters
NameMeaning
strString to search; may be nullptr.
chCharacter to find.
Returns
Pointer to the last match, or nullptr if not found / str is null.

findLastChar() [2/2] #

const char * kleidos::util::str::findLastChar(const char * str, char ch)

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
NameMeaning
strString to search; may be nullptr.
chCharacter to find.
Returns
Pointer to the last match, or nullptr if not found / str is null.

findSubstring() #

const char * kleidos::util::str::findSubstring(const char * haystack, const char * needle)

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
NameMeaning
haystackString to search; may be nullptr.
needleSubstring to find; may be nullptr.
Returns
Pointer to the first match, or nullptr if not found / either is null.

format() #

size_t kleidos::util::str::format(char * dest, size_t destSize, const char * fmt, ...)

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
NameMeaning
destDestination buffer; ignored if nullptr.
destSizeCapacity of dest in bytes (including the NUL slot).
fmtprintf 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() #

size_t kleidos::util::str::formatV(char * dest, size_t destSize, const char * fmt, va_list args)

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
NameMeaning
destDestination buffer; ignored if nullptr.
destSizeCapacity of dest in bytes (including the NUL slot).
fmtprintf format string; writes an empty string if nullptr.
argsVariadic 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] #

size_t kleidos::util::str::length(const char * str)

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
NameMeaning
strString to measure; treated as length 0 if nullptr.
Returns
Number of characters before the terminating NUL.

length() [2/4] #

size_t kleidos::util::str::length(const char * str, size_t maxLen)

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
NameMeaning
strString to measure; treated as length 0 if nullptr.
maxLenMaximum number of bytes to inspect.
Returns
min(distance to first NUL, maxLen).

length() [3/4] #

template<size_t N>
size_t kleidos::util::str::length(const char(&) str[N])
inline

Capacity-deducing overload of length for fixed-size arrays.

Template Parameters
NameMeaning
NArray length, deduced by the compiler.
Parameters
NameMeaning
strArray to measure.
Returns
min(distance to first NUL, N).

length() [4/4] #

template<size_t N>
size_t kleidos::util::str::length(const std::array< char, N > & str)
inline

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
NameMeaning
NArray length, deduced by the compiler.
Parameters
NameMeaning
strstd::array to measure.
Returns
min(distance to first NUL, N).

startsWith() #

bool kleidos::util::str::startsWith(const char * str, const char * prefix)

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
NameMeaning
strString to inspect; may be nullptr.
prefixPrefix to look for; may be nullptr. An empty prefix matches.
Return values
ValueMeaning
truestr starts with prefix.
falsestr does not start with prefix, or either is nullptr.

startsWithIgnoreCase() #

bool kleidos::util::str::startsWithIgnoreCase(const char * str, const char * prefix)

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
NameMeaning
strString to inspect; may be nullptr.
prefixPrefix to look for; may be nullptr. An empty prefix matches.
Return values
ValueMeaning
truestr starts with prefix ignoring ASCII letter case.
falsestr does not start with prefix, or either is nullptr.

Back to the top