StrForeachInRange

Table of Contents

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

NameDirectionDescription
strin,outStr to iterate over.
chrinName of variable to be used which’ll contain character of the current element.
startinStarting index (inclusive).
endinEnding index (exclusive).

Usage example (Cross-references)

    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);
    if (start < end) {
    size_t total_len = 0;
    StrForeachInRange(str, ch, start, end) {
    total_len += 1;
    }

Share :