CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Manipulation

Macros

#define strNConcat(o, ...)   _strNConcat(o, count_macro_args(__VA_ARGS__), (strref[]) { __VA_ARGS__ })
 
#define strNConcatC(o, ...)    _strNConcatC(o, count_macro_args(__VA_ARGS__), (string*[]) { __VA_ARGS__ })
 

Functions

bool strAppend (strhandle io, strref s)
 
bool strPrepend (strref s, strhandle io)
 
bool strConcat (strhandle o, strref s1, strref s2)
 
bool strConcatC (strhandle o, strhandle sc1, strhandle sc2)
 
bool strSubStr (strhandle o, strref s, int32 b, int32 e)
 
bool strSubStrC (strhandle o, strhandle sc, int32 b, int32 e)
 
bool strSubStrI (strhandle io, int32 b, int32 e)
 
void strUpper (strhandle io)
 
void strLower (strhandle io)
 
int32 strSplit (sa_string *out, strref s, strref sep, bool empty)
 
bool strJoin (strhandle out, sa_string arr, strref sep)
 
uint8 strGetChar (strref str, int32 i)
 
void strSetChar (strhandle str, int32 i, uint8 ch)
 

Detailed Description

String manipulation operations for modifying, combining, and extracting portions of strings. Many operations have multiple variants optimized for different use cases.

Naming convention for function parameters

Consuming variants (functions with 'C' suffix)

Functions ending in 'C' (like strConcatC, strSubStrC) take ownership of their input strings and destroy them after use. This allows for more efficient memory reuse when you no longer need the source strings:

string s1 = _S"hello";
string s2 = _S" world";
string result = 0;
strConcatC(&result, &s1, &s2); // s1 and s2 are now NULL
#define _S
Creates a static ASCII string from a string literal.
Definition strbase.h:392
bool strConcatC(strhandle o, strhandle sc1, strhandle sc2)

In-place variants (functions with 'I' suffix)

Functions ending in 'I' modify the string in-place, efficiently reusing the existing buffer when possible:

string s = _S"hello world";
strSubStrI(&s, 0, 5); // s is now "hello"
bool strSubStrI(strhandle io, int32 b, int32 e)

Negative indices

Most functions accept negative indices to count from the end of the string: -1 refers to the last byte, -2 to second-to-last, etc.

Rope optimization

For large string operations, the library may use rope data structures internally to avoid copying. This is transparent to the caller but affects performance characteristics - very large concatenations and substrings are much faster.

Macro Definition Documentation

◆ strNConcat

#define strNConcat (   o,
  ... 
)    _strNConcat(o, count_macro_args(__VA_ARGS__), (strref[]) { __VA_ARGS__ })

bool strNConcat(string *o, ...)

Concatenates multiple strings into an output string

Combines any number of strings into a single result. This is more efficient than calling strConcat() repeatedly. For very large results, may create a rope structure.

The macro accepts a variable number of string arguments and automatically counts them.

Parameters
oOutput string (existing content destroyed)
...Variable number of string arguments to concatenate
Returns
true on success, false on error

Example:

string result = 0;
strNConcat(&result, _S"Hello", _S" ", _S"World", _S"!");
// result is "Hello World!"
strDestroy(&result);
void strDestroy(strhandle ps)
#define strNConcat(o,...)
Definition strmanip.h:161

Definition at line 161 of file strmanip.h.

◆ strNConcatC

#define strNConcatC (   o,
  ... 
)     _strNConcatC(o, count_macro_args(__VA_ARGS__), (string*[]) { __VA_ARGS__ })

bool strNConcatC(string *o, string *s1, string *s2, ...)

Concatenates multiple strings, consuming all inputs

Like strNConcat(), but takes ownership of all input strings and destroys them after use. All input string handles will be NULL after this call. This is the most efficient way to combine many temporary strings.

The macro accepts a variable number of string handle pointers.

Parameters
oOutput string (existing content destroyed)
...Variable number of string handle pointers (destroyed after use)
Returns
true on success, false on error

Example:

string s1 = 0, s2 = 0, s3 = 0, result = 0;
strDup(&s1, _S"Hello");
strDup(&s2, _S" ");
strDup(&s3, _S"World");
strNConcatC(&result, &s1, &s2, &s3);
// result is "Hello World", s1/s2/s3 are now NULL
strDestroy(&result);
void strDup(strhandle o, strref s)
#define strNConcatC(o,...)
Definition strmanip.h:188

Definition at line 188 of file strmanip.h.

Function Documentation

◆ strAppend()

bool strAppend ( strhandle  io,
strref  s 
)

Appends a string to another string in-place

Adds the content of string s to the end of string io. The operation is performed in-place when possible for efficiency. For large strings, may create a rope structure instead of copying.

If io is NULL or empty, this is equivalent to strDup().

Parameters
ioString to append to (modified in-place)
sString to append (not modified)
Returns
true on success, false on error

Example:

string s = 0;
strDup(&s, _S"Hello");
strAppend(&s, _S" World"); // s is now "Hello World"
bool strAppend(strhandle io, strref s)

◆ strConcat()

bool strConcat ( strhandle  o,
strref  s1,
strref  s2 
)

Concatenates two strings into an output string

Combines s1 and s2 into a new string stored in o. Any existing content in o is destroyed. For large strings, may create a rope structure for efficiency.

If o points to the same string as s1, this is optimized to behave like strAppend().

Parameters
oOutput string (existing content destroyed)
s1First string (not modified)
s2Second string (not modified)
Returns
true on success, false on error

Example:

string result = 0;
strConcat(&result, _S"Hello", _S" World");
// result is "Hello World"
strDestroy(&result);
bool strConcat(strhandle o, strref s1, strref s2)

◆ strConcatC()

bool strConcatC ( strhandle  o,
strhandle  sc1,
strhandle  sc2 
)

Concatenates two strings, consuming the inputs

Like strConcat(), but takes ownership of sc1 and sc2, destroying them after use. This allows for more efficient memory reuse when the source strings are no longer needed. Both sc1 and sc2 will be NULL after this call.

Parameters
oOutput string (existing content destroyed)
sc1First string (destroyed after use)
sc2Second string (destroyed after use)
Returns
true on success, false on error

Example:

string s1 = 0, s2 = 0, result = 0;
strDup(&s1, _S"Hello");
strDup(&s2, _S" World");
strConcatC(&result, &s1, &s2);
// result is "Hello World", s1 and s2 are now NULL
strDestroy(&result);

◆ strGetChar()

uint8 strGetChar ( strref  str,
int32  i 
)

Retrieves a single byte from a string

Gets the byte at position i in the string. Negative indices count from the end. Returns 0 if the index is out of bounds.

Note: This operates on bytes, not UTF-8 characters. For multi-byte encodings, use a string iterator instead.

Parameters
strString to read from
iIndex of byte to retrieve (negative = from end)
Returns
The byte at position i, or 0 if out of bounds

Example:

uint8 ch = strGetChar(_S"Hello", 0); // 'H'
ch = strGetChar(_S"Hello", -1); // 'o' (last char)
uint8 strGetChar(strref str, int32 i)

◆ strJoin()

bool strJoin ( strhandle  out,
sa_string  arr,
strref  sep 
)

Joins an array of strings into a single string with a separator

Combines all strings in the array into one string, inserting the separator between each element. The separator is not added before the first element or after the last element.

Parameters
outOutput string (existing content destroyed)
arrArray of strings to join
sepSeparator to insert between elements
Returns
true on success, false if array is empty

Example:

sa_string parts = {0};
saPush(&parts, string, _S"Hello");
saPush(&parts, string, _S"World");
string result = 0;
strJoin(&result, parts, _S" ");
// result is "Hello World"
strDestroy(&result);
saDestroy(&parts);
#define saDestroy(handle)
Definition sarray.h:348
#define saPush(handle, type, elem,...)
Definition sarray.h:467
bool strJoin(strhandle out, sa_string arr, strref sep)

◆ strLower()

void strLower ( strhandle  io)

Converts a string to lowercase (ASCII only)

Modifies the string in-place, converting all uppercase ASCII letters (A-Z) to lowercase (a-z). This is ASCII-only and does not properly handle multi-byte UTF-8 characters or locale-specific case rules.

The string is flattened and made unique before modification.

Parameters
ioString to convert in-place

Example:

string s = 0;
strDup(&s, _S"HELLO WORLD");
strLower(&s); // s is now "hello world"
void strLower(strhandle io)

◆ strPrepend()

bool strPrepend ( strref  s,
strhandle  io 
)

Prepends a string to another string in-place

Adds the content of string s to the beginning of string io. This is less efficient than strAppend() because the entire string must be reconstructed.

Parameters
sString to prepend (not modified)
ioString to prepend to (modified in-place)
Returns
true on success, false on error

Example:

string s = 0;
strDup(&s, _S"World");
strPrepend(_S"Hello ", &s); // s is now "Hello World"
bool strPrepend(strref s, strhandle io)

◆ strSetChar()

void strSetChar ( strhandle  str,
int32  i,
uint8  ch 
)

Sets a single byte in a string

Modifies the byte at position i in the string. Negative indices count from the end. Use strEnd for i to append a byte to the end of the string.

If the index is beyond the current length, the string is grown and zero-padded. The string is flattened and made unique before modification.

Note: This operates on bytes, not UTF-8 characters. Be careful when modifying multi-byte UTF-8 sequences as you can create invalid encodings.

Parameters
strString to modify
iIndex of byte to set (negative = from end, strEnd = append)
chByte value to set

Example:

string s = 0;
strDup(&s, _S"Hello");
strSetChar(&s, 0, 'h'); // s is now "hello"
strSetChar(&s, strEnd, '!'); // s is now "hello!"
void strSetChar(strhandle str, int32 i, uint8 ch)

◆ strSplit()

int32 strSplit ( sa_string *  out,
strref  s,
strref  sep,
bool  empty 
)

Splits a string into pieces separated by a delimiter

Divides the string s into segments at each occurrence of the separator string, storing the results in a dynamic array. The output array is cleared first.

Parameters
outPointer to string array to store results (cleared first)
sString to split
sepSeparator string to split on
emptyIf true, empty segments are preserved; if false, they are skipped
Returns
Number of segments created

Example:

sa_string parts = {0};
strSplit(&parts, _S"a,b,c", _S",", false);
// parts contains ["a", "b", "c"]
for (int i = 0; i < saSize(parts); i++)
strDestroy(&parts.a[i]);
saDestroy(&parts);
strSplit(&parts, _S"a,,b", _S",", true);
// parts contains ["a", "", "b"] (empty segment preserved)
saDestroy(&parts);
#define saSize(ref)
Definition sarray.h:251
int32 strSplit(sa_string *out, strref s, strref sep, bool empty)

◆ strSubStr()

bool strSubStr ( strhandle  o,
strref  s,
int32  b,
int32  e 
)

Extracts a substring from a string

Creates a new string containing bytes from position b (inclusive) to position e (exclusive). Negative indices count from the end. Use strEnd for e to extract to the end of the string.

For large substrings, may create a rope reference instead of copying the data.

Parameters
oOutput string (existing content destroyed)
sSource string (not modified)
bStarting position (negative = from end)
eEnding position (negative = from end, strEnd = end of string)
Returns
true on success, false on error

Example:

string sub = 0;
strSubStr(&sub, _S"Hello World", 0, 5); // "Hello"
strSubStr(&sub, _S"Hello World", 6, strEnd); // "World"
strSubStr(&sub, _S"Hello World", -5, strEnd); // "World" (last 5 chars)
strDestroy(&sub);
bool strSubStr(strhandle o, strref s, int32 b, int32 e)

◆ strSubStrC()

bool strSubStrC ( strhandle  o,
strhandle  sc,
int32  b,
int32  e 
)

Extracts a substring, consuming the source string

Like strSubStr(), but takes ownership of sc and destroys it after use. The sc handle will be NULL after this call. More efficient when the source is no longer needed.

Parameters
oOutput string (existing content destroyed)
scSource string (destroyed after use)
bStarting position (negative = from end)
eEnding position (negative = from end, strEnd = end of string)
Returns
true on success, false on error

Example:

string s = 0, sub = 0;
strDup(&s, _S"Hello World");
strSubStrC(&sub, &s, 0, 5); // sub is "Hello", s is now NULL
strDestroy(&sub);
bool strSubStrC(strhandle o, strhandle sc, int32 b, int32 e)

◆ strSubStrI()

bool strSubStrI ( strhandle  io,
int32  b,
int32  e 
)

Extracts a substring in-place

Modifies the string to contain only the specified range. This is the most efficient way to truncate or extract from a string when you don't need the original.

Parameters
ioString to modify in-place
bStarting position (negative = from end)
eEnding position (negative = from end, strEnd = end of string)
Returns
true on success, false on error

Example:

string s = 0;
strDup(&s, _S"Hello World");
strSubStrI(&s, 0, 5); // s is now "Hello"

◆ strUpper()

void strUpper ( strhandle  io)

Converts a string to uppercase (ASCII only)

Modifies the string in-place, converting all lowercase ASCII letters (a-z) to uppercase (A-Z). This is ASCII-only and does not properly handle multi-byte UTF-8 characters or locale-specific case rules.

The string is flattened and made unique before modification.

Parameters
ioString to convert in-place

Example:

string s = 0;
strDup(&s, _S"hello world");
strUpper(&s); // s is now "HELLO WORLD"
void strUpper(strhandle io)