StrForeachReverseIdx
- Macro
- October 8, 2025
Table of Contents
StrForeachReverseIdx
StrForeachReverseIdx
Description
Iterate over each character chr
of the given Str str
in reverse order at each index idx
. This macro is a direct alias for VecForeachReverseIdx
specialized for Str. The variables chr
and idx
are declared and defined by the underlying macro.
Parameters
Name | Direction | Description |
---|---|---|
str | in,out | Str to iterate over. |
chr | in | Name of the variable to be used which will contain the character at the iterated index idx . The type of chr will likely be 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)
- In
Str.Foreach.c:54
:
// Test StrForeachReverseIdx macro
bool test_str_foreach_reverse_idx(void) {
WriteFmt("Testing StrForeachReverseIdx\n");
Str s = StrInitFromZstr("Hello");
- In
Str.Foreach.c:61
:
Str result = StrInit();
StrForeachReverseIdx(&s, chr, idx) {
// Append the character and its index to the result string
StrWriteFmt(&result, "{c}{}", chr, idx);
// Make idx go out of bounds in StrForeachReverseIdx by modifying string during iteration
bool test_str_foreach_reverse_idx_out_of_bounds_access(void) {
WriteFmt("Testing StrForeachReverseIdx where idx goes out of bounds\n");
Str s = StrInitFromZstr("Beautiful Weather"); // 17 characters
// StrForeachReverseIdx (VecForeachReverseIdx) has explicit bounds checking: if ((idx) >= (v)->length) LOG_FATAL(...)
StrForeachReverseIdx(&s, chr, idx) {
WriteFmt("Accessing idx {} (s.length={}): '{c}'\n", idx, s.length, chr);
- In
Str.c:517
:
if (VecLen(str) > 0) {
size_t total_len = 0;
StrForeachReverseIdx(str, ch, idx) {
total_len += 1 + idx;
}