Skip to content
StrSplitToIters

StrSplitToIters

Description

Split s into a vector of StrIter views over the original s buffer, delimited by key. No new strings are allocated – each iterator borrows a slice of s. Best used when the caller only needs to iterate the pieces without mutating them.

Two call shapes via _Generic on key: StrSplitToIters(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 StrIters vector whose entries view slices of s. Caller owns the vector and must VecDeinit it. s is not modified.

Failure

Returns a zero-length StrIters vector. s is not modified.

Usage example (Cross-references)

Usage examples (Cross-references)
    // Test string split functions
    bool test_str_split(void) {
        WriteFmt("Testing StrSplit and StrSplitToIters\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Test StrSplitToIters
        StrIters iters = StrSplitToIters(&s, ",");
        result         = result && (VecLen(&iters) == 3);
    // the intact result has 3 iters (last length 0); the mutant drops it (2).
    static bool test_split_to_iters_trailing_empty_field(void) {
        WriteFmt("Testing StrSplitToIters trailing empty field\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str      s     = StrInitFromZstr("a,b,", &alloc);
        StrIters iters = StrSplitToIters(&s, ",");
    
        bool result = (VecLen(&iters) == 3);
    // a corrupt-magic Str must abort before its data is read (Str.Mutants6).
    static bool test_split_to_iters_corrupt_magic_aborts(void) {
        WriteFmt("Testing StrSplitToIters aborts on corrupt magic\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str s                     = StrInitFromZstr("a,b,c", &alloc);
        GENERIC_VEC(&s)->__magic ^= 0x1;
        StrIters iters            = StrSplitToIters(&s, ",");
    
        VecDeinit(&iters);
Last updated on