Skip to content
StrForeachPtrIdx

StrForeachPtrIdx

Description

Walk each character of str forward, binding chrptr to a pointer to the character at index idx. Use this form when the body needs to mutate characters in place. Both chrptr and idx are stamped by the macro. See VecForeachPtrIdx for the full SUCCESS/FAILURE contract.

Usage example (Cross-references)

Usage examples (Cross-references)
    // Test StrForeachPtrIdx macro
    bool test_str_foreach_ptr_idx(void) {
        WriteFmt("Testing StrForeachPtrIdx\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        // Build a new string by iterating through each character pointer with its index
        Str result = StrInit(&alloc);
        StrForeachPtrIdx(&s, chrptr, idx) {
            // Append the character (via pointer) and its index to the result string
            StrAppendFmt(&result, "{c}{}", *chrptr, idx);
    // Make idx go out of bounds in StrForeachPtrIdx by modifying string during iteration
    bool test_str_foreach_ptr_idx_out_of_bounds_access(void) {
        WriteFmt("Testing StrForeachPtrIdx where idx goes out of bounds\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // StrForeachPtrIdx (VecForeachPtrIdx) has explicit bounds checking: if ((idx) >= (v)->length) LOG_FATAL(...)
        StrForeachPtrIdx(&s, chr_ptr, idx) {
            WriteFmt("Accessing idx {} (s.length={}): '{c}'\n", idx, StrLen(&s), *chr_ptr);
Last updated on