Skip to content
StrForeachPtrIdx

StrForeachPtrIdx

StrForeachPtrIdx

Description

Iterate over each character pointer chrptr of the given Str str at each index idx. This macro is a direct alias for VecForeachPtrIdx 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 the pointer variable to be used which will point to the character at the iterated index idx. The type of chrptr will likely be a pointer to the character type used by the Str implementation (e.g., char*).
idx in Name of the variable to be used for iterating over indices (i64).

Usage example (Cross-references)

Usage examples (Cross-references)
                if (VecLen(str) > 0) {
                    size_t total_len = 0;
                    StrForeachPtrIdx(str, ch_ptr, idx) {
                        total_len += 1 + idx;
                    }
    // Test StrForeachPtrIdx macro
    bool test_str_foreach_ptr_idx(void) {
        WriteFmt("Testing StrForeachPtrIdx\n");
    
        Str s = StrInitFromZstr("Hello");
        // Build a new string by iterating through each character pointer with its index
        Str result = StrInit();
        StrForeachPtrIdx(&s, chrptr, idx) {
            // Append the character (via pointer) and its index to the result string
            StrWriteFmt(&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");
    
        Str s = StrInitFromZstr("Programming Test"); // 16 characters
    
        // 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, s.length, *chr_ptr);
Last updated on