C Collections

cstring · Section 3

cstring_replace

cstring_replace - replace every non-overlapping occurrence of needle

C Collections (libccollections, -lccollections)

#include <ccollections/cstring.h>
 
ccol_retval cstring_replace(cstr s, const char *needle,
                              const char *replacement);

This function replaces every non-overlapping occurrence of needle in s
with replacement. It builds the result in a fresh buffer and swaps that
buffer in, so replacement may contain needle without any risk of an
endless loop. When needle is not found the call succeeds and s does not
change.

s              String to change
needle         Substring to search for (must not be NULL or empty)
replacement    Substitute string (must not be NULL)

ccol_success              - The replace succeeded, also when needle was
                            not found
ccol_invalid_args         - needle is NULL or empty, or replacement is NULL
ccol_container_full       - The result would overflow a size_t
ccol_not_enough_memory    - The buffer allocation failed

Matches never overlap, and the search goes from left to right
Asserts if s is NULL

cstring_find(3), cstring_set(3)