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).

Success

The body is executed for each character of the Str str from the start index to the end-1 index.

Failure

If the Str str is NULL, its length is zero, or the range is invalid, the loop body will not be executed. Any failures within the VecForeachInRangeIdx macro will result in a fatal log message and program termination.

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) {
    printf("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);

Share :