Skip to content
StrForeachPtrInRangeIdx

StrForeachPtrInRangeIdx

StrForeachPtrInRangeIdx

Description

Iterate over characters in a specific range of the given Str str at each index idx (as pointers). This macro is a direct alias for VecForeachPtrInRangeIdx specialized for Str. The variables chrptr and idx are declared and defined by the underlying macro.

Parameters

Name Direction Description
str in,out Str to iterate over.
chrptr in Name of pointer variable to be used which’ll point to character at iterated index idx.
idx in Name of variable to be used for iterating over indices.
start in Starting index (inclusive).
end in Ending index (exclusive).

Usage example (Cross-references)

Usage examples (Cross-references)
                    if (start < end) {
                        size_t total_len = 0;
                        StrForeachPtrInRangeIdx(str, ch_ptr, idx, start, end) {
                            total_len += 1 + idx;
                        }
    // Test StrForeachPtrInRangeIdx macro
    bool test_str_foreach_ptr_in_range_idx(void) {
        WriteFmt("Testing StrForeachPtrInRangeIdx\n");
    
        Str s = StrInitFromZstr("Hello World");
        // Build a new string by iterating through a range of character pointers with indices
        Str result = StrInit();
        StrForeachPtrInRangeIdx(&s, chrptr, idx, 6, 11) {
            // Append the character and its index to the result string
            StrWriteFmt(&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");
    
        Str s = StrInitFromZstr("Comprehensive Testing Framework"); // 31 characters
        // Use StrForeachPtrInRangeIdx with a fixed range that becomes invalid when we modify the string
        size original_length = s.length; // Capture this as 32
        StrForeachPtrInRangeIdx(&s, chr_ptr, idx, 0, original_length) {
            WriteFmt("Accessing idx {} (s.length={}): '{c}'\n", idx, s.length, *chr_ptr);
Last updated on