Skip to content
StrForeachPtrInRangeIdx

StrForeachPtrInRangeIdx

Description

Walk characters of str in the half-open range [start, end), binding chrptr to a pointer to the character at index idx. Both chrptr and idx are stamped by the macro. See VecForeachPtrInRangeIdx for the full SUCCESS/FAILURE contract.

Usage example (Cross-references)

Usage examples (Cross-references)
    // Test StrForeachPtrInRangeIdx macro
    bool test_str_foreach_ptr_in_range_idx(void) {
        WriteFmt("Testing StrForeachPtrInRangeIdx\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        // Build a new string by iterating through a range of character pointers with indices
        Str result = StrInit(&alloc);
        StrForeachPtrInRangeIdx(&s, chrptr, idx, 6, 11) {
            // Append the character and its index to the result string
            StrAppendFmt(&result, "{c}{}", *chrptr, idx);
    // Make idx go out of bounds in StrForeachPtrInRangeIdx by modifying string during iteration
    bool test_str_foreach_ptr_in_range_idx_out_of_bounds_access(void) {
        WriteFmt("Testing StrForeachPtrInRangeIdx where idx goes out of bounds\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        // Use StrForeachPtrInRangeIdx with a fixed range that becomes invalid when we modify the string
        size original_length = StrLen(&s); // Capture this as 32
        StrForeachPtrInRangeIdx(&s, chr_ptr, idx, 0, original_length) {
            WriteFmt("Accessing idx {} (s.length={}): '{c}'\n", idx, StrLen(&s), *chr_ptr);
Last updated on