StrSplit
Description
Split s into a vector of new Str objects delimited by key. Each returned Str owns its own storage and is independently modifiable. Use when callers will further mutate the pieces.
Two call shapes via _Generic on key: StrSplit(s, key) – key is Str * or Zstr.
Parameters
| Name | Direction | Description |
|---|---|---|
s |
in | Str to split. |
key |
in | Delimiter (Str * / Zstr). |
Success
Returns a non-empty Strs vector. Caller owns the vector and must VecDeinit it; that releases each contained Str as well. s is not modified.
Failure
Returns a zero-length Strs vector. s is not modified.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Ops.c:203:
// Test string split functions
bool test_str_split(void) {
WriteFmt("Testing StrSplit and StrSplitToIters\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Ops.c:209:
// Test StrSplit
Str s = StrInitFromZstr("Hello,World,Test", &alloc);
Strs split = StrSplit(&s, ",");
bool result = (VecLen(&split) == 3);- In
Ops.c:509:
// result to 42 pushes it -> 1 element.
static bool test_split_drops_prefix_of_key(void) {
WriteFmt("Testing StrSplit drops a remaining proper-prefix-of-key segment\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Ops.c:513:
Str s = StrInitFromZstr("ab", &alloc);
Strs split = StrSplit(&s, "abc");
bool result = (VecLen(&split) == 0);- In
Ops.c:532:
// the segment is wrongly pushed.
static bool test_split_prefix_compare_length(void) {
WriteFmt("Testing StrSplit prefix-of-key compare uses remaining length\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Ops.c:536:
Str s = StrInitFromZstr("ab", &alloc);
Strs split = StrSplit(&s, "abc");
bool result = (VecLen(&split) == 0);- In
Ops.c:931:
Str s = StrInitFromZstr("a,b,c", &alloc);
Strs strs = StrSplit(&s, ",");
GENERIC_VEC(VecPtrAt(&strs, 0))->__magic ^= 0x1;
ValidateStrs(&strs);- In
Ops.c:945:
// (Str.Mutants7).
static bool test_split_validates(void) {
WriteFmt("Testing StrSplit validates its Str (deadend)\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Ops.c:950:
Str s = StrInitFromZstr("a,b", &alloc);
s.__magic = 0; // corrupt the magic so ValidateStr aborts
Strs split = StrSplit(&s, ",");
(void)split;- In
Init.c:287:
size before = DebugAllocatorLiveCount(&dbg);
Strs parts = StrSplit(&s, ",");
// Sanity: the split really produced multiple heap-backed elements,
// otherwise the leak would be unobservable.
Last updated on