Skip to content
StrForeachInRange

StrForeachInRange

StrForeachInRange

Description

Iterate over characters in a specific range of the given Str str. This is a convenience macro that iterates over a range using an internally managed index. The variable chr is declared and defined by the underlying VecForeachInRange macro.

Parameters

Name Direction Description
str in,out Str to iterate over.
chr in Name of variable to be used which’ll contain character of the current element.
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;
                        StrForeachInRange(str, ch, start, end) {
                            total_len += 1;
                        }
                    write_char_internal(o, fmt_info->flags, (const char *)s->data, len);
                } else {
                    StrForeachInRange(s, c, 0, len) {
                        if (IS_PRINTABLE(c)) {
                            StrPushBack(o, c);
    // Test StrForeachInRange macro
    bool test_str_foreach_in_range(void) {
        WriteFmt("Testing StrForeachInRange\n");
    
        Str s = StrInitFromZstr("Hello World");
        // Build a new string by iterating through a range of characters
        Str result = StrInit();
        StrForeachInRange(&s, chr, 0, 5) {
            // Append the character to the result string
            StrPushBack(&result, chr);
        // Test with range at the end of the string
        Str end_result = StrInit();
        StrForeachInRange(&s, chr, 6, 11) {
            // Append the character to the result string
            StrPushBack(&end_result, chr);
Last updated on