StrForeachReversePtrIdx
- Macro
- October 8, 2025
Table of Contents
StrForeachReversePtrIdx
StrForeachReversePtrIdx
Description
Iterate over each character pointer chrptr
of the given Str str
in reverse order at each index idx
. This macro is a direct alias for VecForeachPtrReverseIdx
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)
// Test StrForeachReversePtrIdx macro
bool test_str_foreach_reverse_ptr_idx(void) {
WriteFmt("Testing StrForeachReversePtrIdx\n");
Str s = StrInitFromZstr("Hello");
Str result = StrInit();
StrForeachReversePtrIdx(&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 StrForeachReversePtrIdx by modifying string during iteration
bool test_str_foreach_reverse_ptr_idx_out_of_bounds_access(void) {
WriteFmt("Testing StrForeachReversePtrIdx where idx goes out of bounds\n");
Str s = StrInitFromZstr("Excellent Example"); // 17 characters
// StrForeachReversePtrIdx (VecForeachPtrReverseIdx) has explicit bounds checking: if ((idx) >= (v)->length) LOG_FATAL(...)
StrForeachReversePtrIdx(&s, chr_ptr, idx) {
WriteFmt("Accessing idx {} (s.length={}): '{c}'\n", idx, s.length, *chr_ptr);
- In
Str.c:539
:
if (VecLen(str) > 0) {
size_t total_len = 0;
StrForeachReversePtrIdx(str, ch_ptr, idx) {
total_len += 1 + idx;
}