Skip to content

StrIterIndex

Description

Absolute cursor index within the StrIter’s backing region. Alias- reframe of IterIndex in string vocabulary. See IterIndex for the full contract.

Usage example (Cross-references)

Usage examples (Cross-references)
            LOG_ERROR("Failed to parse f64: empty input");
            DefaultAllocatorDeinit(&scratch);
            return StrIterDataAt(&si, StrIterIndex(&si));
        }
        if ((c == 'i' || c == 'I' || c == 'n' || c == 'N') || (c == '-' && (c1 == 'i' || c1 == 'I'))) {
            StrIter saved = si;
            Zstr    start = StrIterDataAt(&si, StrIterIndex(&si));
            while (StrIterPeek(&si, &c) && !IS_SPACE(c)) {
                StrIterMustNext(&si);
            }
    
            Str temp = StrInitFromCstr(start, (size)(StrIterIndex(&si) - StrIterIndex(&saved)), &scratch);
    
            // Special-value tokens (`inf`, `nan`, ...) carry through the
                StrDeinit(&temp);
                DefaultAllocatorDeinit(&scratch);
                return StrIterDataAt(&si, StrIterIndex(&si));
            }
            StrDeinit(&temp);
            // least one digit, otherwise rewind so the 'e' is left to the
            // caller's tail (e.g. a unit suffix).
            if ((c == 'e' || c == 'E') && StrIterIndex(&si) > StrIterIndex(&saved)) {
                StrIterMustNext(&si);
            }
    
            if (!is_valid_number_char(c, StrIterIndex(&si) == StrIterIndex(&saved), true)) {
                break;
            }
        }
    
        Zstr start = StrIterDataAt(&si, StrIterIndex(&saved));
        size pos   = StrIterIndex(&si) - StrIterIndex(&saved);
    
        Zstr start = StrIterDataAt(&si, StrIterIndex(&saved));
        size pos   = StrIterIndex(&si) - StrIterIndex(&saved);
    
        Str temp = StrInitFromCstr(start, pos, &scratch);
            LOG_ERROR("Failed to parse u8: empty input");
            DefaultAllocatorDeinit(&scratch);
            return StrIterDataAt(&si, StrIterIndex(&si));
        }
    
        if (fmt_info && (fmt_info->flags & FMT_FLAG_CHAR)) {
            Zstr next = read_chars_internal(StrIterDataAt(&si, StrIterIndex(&si)), (u8 *)v, sizeof(*v), fmt_info);
            DefaultAllocatorDeinit(&scratch);
            return next;
    
        while (StrIterPeek(&si, &c)) {
            if (!is_valid_number_char(c, StrIterIndex(&si) == StrIterIndex(&saved), false)) {
                break;
            }
        }
    
        Zstr start = StrIterDataAt(&si, StrIterIndex(&saved));
        size pos   = StrIterIndex(&si) - StrIterIndex(&saved);
    
        Zstr start = StrIterDataAt(&si, StrIterIndex(&saved));
        size pos   = StrIterIndex(&si) - StrIterIndex(&saved);
    
        Str temp = StrInitFromCstr(start, pos, &scratch);
                LOG_ERROR("Failed to parse " #NAME ": empty input");                                                       \
                DefaultAllocatorDeinit(&scratch);                                                                          \
                return StrIterDataAt(&si, StrIterIndex(&si));                                                              \
            }                                                                                                              \
                                                                                                                           \
            StrIter saved = si;                                                                                            \
            while (StrIterPeek(&si, &c)) {                                                                                 \
                if (!is_valid_number_char(c, StrIterIndex(&si) == StrIterIndex(&saved), false)) {                          \
                    break;                                                                                                 \
                }                                                                                                          \
            }                                                                                                              \
                                                                                                                           \
            Zstr start = StrIterDataAt(&si, StrIterIndex(&saved));                                                         \
            size pos   = StrIterIndex(&si) - StrIterIndex(&saved);                                                         \
            Str  temp  = StrInitFromCstr(start, pos, &scratch);                                                            \
                                                                                                                           \
            Zstr start = StrIterDataAt(&si, StrIterIndex(&saved));                                                         \
            size pos   = StrIterIndex(&si) - StrIterIndex(&saved);                                                         \
            Str  temp  = StrInitFromCstr(start, pos, &scratch);                                                            \
                                                                                                                           \
        if (!StrIterRemainingLength(&si)) {
            LOG_ERROR("Empty input string");
            return StrIterDataAt(&si, StrIterIndex(&si));
        }
        }
    
        Zstr start = StrIterDataAt(&si, StrIterIndex(&si));
    
        char c0 = 0;
            }
    
            if (StrIterIndex(&si) == StrIterIndex(&hex_saved)) {
                LOG_ERROR("Invalid hex format - no digits after 0x");
                return start;
    
            Str hex_str = StrInitFromCstr(
                StrIterDataAt(&si, StrIterIndex(&hex_saved)),
                StrIterIndex(&si) - StrIterIndex(&hex_saved),
                BitVecAllocator(bv)
            Str hex_str = StrInitFromCstr(
                StrIterDataAt(&si, StrIterIndex(&hex_saved)),
                StrIterIndex(&si) - StrIterIndex(&hex_saved),
                BitVecAllocator(bv)
            );
            *bv = BitVecFromInteger(value, bit_len, BitVecAllocator(bv));
            StrDeinit(&hex_str);
            return StrIterDataAt(&si, StrIterIndex(&si));
        }
            }
    
            if (StrIterIndex(&si) == StrIterIndex(&oct_saved)) {
                LOG_ERROR("Invalid octal format - no digits after 0o");
                return start;
    
            Str oct_str = StrInitFromCstr(
                StrIterDataAt(&si, StrIterIndex(&oct_saved)),
                StrIterIndex(&si) - StrIterIndex(&oct_saved),
                BitVecAllocator(bv)
            Str oct_str = StrInitFromCstr(
                StrIterDataAt(&si, StrIterIndex(&oct_saved)),
                StrIterIndex(&si) - StrIterIndex(&oct_saved),
                BitVecAllocator(bv)
            );
            *bv = BitVecFromInteger(value, bit_len, BitVecAllocator(bv));
            StrDeinit(&oct_str);
            return StrIterDataAt(&si, StrIterIndex(&si));
        }
        }
    
        if (StrIterIndex(&si) == StrIterIndex(&bin_saved)) {
            LOG_ERROR("Invalid binary format - expected 0s and 1s");
            return start;
    
        Str bin_str = StrInitFromCstr(
            StrIterDataAt(&si, StrIterIndex(&bin_saved)),
            StrIterIndex(&si) - StrIterIndex(&bin_saved),
            BitVecAllocator(bv)
        Str bin_str = StrInitFromCstr(
            StrIterDataAt(&si, StrIterIndex(&bin_saved)),
            StrIterIndex(&si) - StrIterIndex(&bin_saved),
            BitVecAllocator(bv)
        );
    
        StrDeinit(&bin_str);
        return StrIterDataAt(&si, StrIterIndex(&si));
    }
    #endif // FEATURE_BITVEC
        if (!StrIterRemainingLength(&si)) {
            LOG_ERROR("Failed to parse Int: empty input");
            return StrIterDataAt(&si, StrIterIndex(&si));
        }
        StrIter saved        = si;
        StrIter digits_saved = si;
        Zstr    start        = StrIterDataAt(&si, StrIterIndex(&saved));
        u8      radix        = int_fmt_radix_from_flags(fmt_info);
        }
    
        if (StrIterIndex(&si) == StrIterIndex(&digits_saved)) {
            LOG_ERROR("Failed to parse Int");
            return start;
        }
    
        Str  temp   = StrInitFromCstr(start, StrIterIndex(&si) - StrIterIndex(&saved), IntAllocator(value));
        Int  parsed = IntInit(IntAllocator(value));
        bool ok     = IntTryFromStrRadix(&parsed, StrBegin(&temp), radix);
    
        StrDeinit(&temp);
        return StrIterDataAt(&si, StrIterIndex(&si));
    }
    #endif // FEATURE_INT
            StrDeinit(&temp);
            FloatDeinit(&parsed);
            return StrIterDataAt(&si, StrIterIndex(&si));
        }
        }
    
        start     = StrIterDataAt(&si, StrIterIndex(&si));
        token_len = float_fmt_token_length(start);
            LOG_ERROR("Failed to parse f32: empty input");
            DefaultAllocatorDeinit(&scratch);
            return StrIterDataAt(&si, StrIterIndex(&si));
        }
        if ((c == 'i' || c == 'I' || c == 'n' || c == 'N') || (c == '-' && (c1 == 'i' || c1 == 'I'))) {
            StrIter saved = si;
            Zstr    start = StrIterDataAt(&si, StrIterIndex(&si));
            while (StrIterPeek(&si, &c) && !IS_SPACE(c)) {
                StrIterMustNext(&si);
            }
    
            Str temp = StrInitFromCstr(start, (size)(StrIterIndex(&si) - StrIterIndex(&saved)), &scratch);
    
            // Special-value tokens (`inf`, `nan`, ...) carry through the
                StrDeinit(&temp);
                DefaultAllocatorDeinit(&scratch);
                return StrIterDataAt(&si, StrIterIndex(&si));
            }
            StrDeinit(&temp);
            // least one digit, otherwise rewind so the 'e' is left to the
            // caller's tail.
            if ((c == 'e' || c == 'E') && StrIterIndex(&si) > StrIterIndex(&saved)) {
                StrIterMustNext(&si);
            }
    
            if (!is_valid_number_char(c, StrIterIndex(&si) == StrIterIndex(&saved), true)) {
                break;
            }
        }
    
        Zstr start = StrIterDataAt(&si, StrIterIndex(&saved));
        size pos   = StrIterIndex(&si) - StrIterIndex(&saved);
    
        Zstr start = StrIterDataAt(&si, StrIterIndex(&saved));
        size pos   = StrIterIndex(&si) - StrIterIndex(&saved);
    
        Str temp = StrInitFromCstr(start, pos, &scratch);
        // with \0 in place so the path is a usable C string aliasing the
        // iter's backing buffer.
        size path_start_pos = StrIterIndex(si);
        char c;
        while (StrIterPeek(si, &c) && c != '\n') {
            StrIterMustNext(si);
        }
        size line_terminator_pos = StrIterIndex(si);
    
        out->start       = start;
    
            // First token: IP literal.
            size ip_start = StrIterIndex(&si);
            while (StrIterPeek(&si, &c) && !is_hspace(c) && c != '\n') {
                StrIterMustNext(&si);
                StrIterMustNext(&si);
            }
            u64 ip_len = (u64)(StrIterIndex(&si) - ip_start);
    
            bool got_v4 = false;
                    break;
                }
                size nm_start = StrIterIndex(&si);
                while (StrIterPeek(&si, &c) && !is_hspace(c) && c != '\n' && c != '#') {
                    StrIterMustNext(&si);
                    StrIterMustNext(&si);
                }
                u64 nm_len = (u64)(StrIterIndex(&si) - nm_start);
                if (nm_len == 0) {
                    break;
            char sep;
            if (StrIterRemainingLength(&si) > kw_len &&
                MemCompare(StrIterDataAt(&si, StrIterIndex(&si)), NS_KEYWORD, kw_len) == 0 &&
                StrIterPeekAt(&si, (i64)kw_len, &sep) && (sep == ' ' || sep == '\t')) {
                StrIterMustMove(&si, (i64)kw_len);
                skip_hspace_iter(&si);
    
                size ip_start = StrIterIndex(&si);
                while (StrIterPeek(&si, &c) && !is_hspace(c) && c != '\n' && c != '#') {
                    StrIterMustNext(&si);
                    StrIterMustNext(&si);
                }
                u64 ip_len = (u64)(StrIterIndex(&si) - ip_start);
                if (ip_len > 0 && ip_len < 64) {
                    StrInitStack(ip_buf, 64) {
    
        si = KvConfigReadKey(si, key);
        if (StrIterIndex(&si) == StrIterIndex(&saved_si)) {
            return saved_si;
        }
        si = KvConfigReadValue(si, value);
    
        if (StrIterIndex(&si) == StrIterIndex(&saved_si)) {
            StrClear(key);
            StrClear(value);
    
            read_si = KvConfigReadPair(si, &key, &value);
            if (StrIterIndex(&read_si) == StrIterIndex(&si)) {
                StrDeinit(&key);
                StrDeinit(&value);
            // key start
            read_si = JReadString(si, &key);
            if (StrIterIndex(&read_si) == StrIterIndex(&si)) {
                LOG_ERROR("Failed to read string key in object. Invalid JSON");
                StrDeinit(&key);
    
            // if still no advancement in read position
            if (StrIterIndex(&read_si) == StrIterIndex(&si)) {
                LOG_ERROR("Failed to parse value. Invalid JSON.");
                StrDeinit(&key);
    
            // if no advancement in read position
            if (StrIterIndex(&read_si) == StrIterIndex(&si)) {
                LOG_ERROR("Failed to parse value. Invalid JSON.");
                return saved_si;
                                LOG_ERROR(
                                    "No unicode support '{.6}'. Unicode sequence will be skipped.",
                                    LVAL(StrIterDataAt(&si, StrIterIndex(&si) - 1))
                                );
                                StrIterMustMove(&si, 5);
    
        if (!StrLen(&ns)) {
            LOG_ERROR("Failed to parse number. '{.8}'", LVAL(StrIterDataAt(&saved_si, StrIterIndex(&saved_si))));
            StrDeinit(&ns);
            DefaultAllocatorDeinit(&scratch);
        si = JReadNumber(si, &num);
    
        if (StrIterIndex(&si) == StrIterIndex(&saved_si)) {
            LOG_ERROR("Failed to parse integer number.");
            return saved_si;
        si = JReadNumber(si, &num);
    
        if (StrIterIndex(&si) == StrIterIndex(&saved_si)) {
            LOG_ERROR("Failed to parse floating point number");
            return saved_si;
            si = JReadBool(si, &b);
    
            if (StrIterIndex(&si) == StrIterIndex(&before_si)) {
                LOG_ERROR(
                    "Failed to read boolean value. Expected true/false. Invalid "
            si = JReadNull(si, &n);
    
            if (StrIterIndex(&si) == StrIterIndex(&before_si)) {
                LOG_ERROR(
                    "Failed to read boolean value. Expected true/false. Invalid "
            DefaultAllocatorDeinit(&scratch);
    
            if (StrIterIndex(&si) == StrIterIndex(&before_si)) {
                LOG_ERROR("Failed to read string value. Expected string. Invalid JSON.");
                return saved_si;
            si = JReadNumber(si, &num);
    
            if (StrIterIndex(&si) == StrIterIndex(&before_si)) {
                LOG_ERROR("Failed to read number value. Expected a number. Invalid JSON.");
                return saved_si;
            si                = JSkipObject(si);
    
            if (StrIterIndex(&si) == StrIterIndex(&before_si)) {
                LOG_ERROR("Failed to read object. Expected an object. Invalid JSON.");
                return saved_si;
            si                = JSkipArray(si);
    
            if (StrIterIndex(&si) == StrIterIndex(&before_si)) {
                LOG_ERROR("Failed to read array. Expected an array. Invalid JSON.");
                return saved_si;
        while (StrIterPeek(&si, &c)) {
            // Find next dot or end-of-string.
            size seg_start = StrIterIndex(&si);
            while (StrIterPeek(&si, &c) && c != '.') {
                StrIterMustNext(&si);
                StrIterMustNext(&si);
            }
            u64 seg_len = (u64)(StrIterIndex(&si) - seg_start);
            if (seg_len == 0) {
                // Trailing dot at the end is valid (means root); leading or
                Str key       = StrInit(&alloc);                                                                           \
                UNPL(read_si) = JReadString(si, &key);                                                                     \
                if (StrIterIndex(&UNPL(read_si)) == StrIterIndex(&si)) {                                                   \
                    LOG_ERROR("Failed to read string key in object. Invalid JSON");                                        \
                    StrDeinit(&key);                                                                                       \
                StrIter UNPL(si_before_read) = si;                                                                         \
                { reader }                                                                                                 \
                if (StrIterIndex(&UNPL(si_before_read)) == StrIterIndex(&si)) {                                            \
                    StrIter UNPL(skip_si) = JSkipValue(si);                                                                \
                    if (StrIterIndex(&UNPL(skip_si)) == StrIterIndex(&si)) {                                               \
                if (StrIterIndex(&UNPL(si_before_read)) == StrIterIndex(&si)) {                                            \
                    StrIter UNPL(skip_si) = JSkipValue(si);                                                                \
                    if (StrIterIndex(&UNPL(skip_si)) == StrIterIndex(&si)) {                                               \
                        LOG_ERROR("Failed to parse value. Invalid JSON.");                                                 \
                        StrDeinit(&key);                                                                                   \
        bool    result = true;
    
        result = result && (StrIterIndex(&si) == StrIterLength(&si));
        result = result && (MapPairCount(&cfg) == 3);
        result = result && KvConfigContains(&cfg, "host");
        bool    result = true;
    
        result = result && (StrIterIndex(&si) == StrIterLength(&si));
        result = result && (MapPairCount(&cfg) == 4);
        result = result && path && StrCmp(path, "/srv/my app") == 0;
        bool    result  = true;
    
        result = result && (StrIterIndex(&si) == 0);
        result = result && KvConfigGetBool(&cfg, "valid", &enabled) && enabled;
        result = result && !KvConfigContains(&cfg, "later");
                                                                                                                           \
                /* if no advancement in read position */                                                                   \
                if (StrIterIndex(&UNPL(si_before_read)) == StrIterIndex(&si)) {                                            \
                    /* skip the value */                                                                                   \
                    StrIter UNPL(read_si) = JSkipValue(si);                                                                \
                                                                                                                           \
                    /* if still no advancement in read position */                                                         \
                    if (StrIterIndex(&UNPL(read_si)) == StrIterIndex(&si)) {                                               \
                        LOG_ERROR("Failed to parse value. Invalid JSON.");                                                 \
                        UNPL(failed) = true;                                                                               \
                /* key start */                                                                                            \
                UNPL(read_si) = JReadString(si, &key);                                                                     \
                if (StrIterIndex(&UNPL(read_si)) == StrIterIndex(&si)) {                                                   \
                    LOG_ERROR("Failed to read string key in object. Invalid JSON");                                        \
                    StrDeinit(&key);                                                                                       \
                                                                                                                           \
                /* if no advancement in read position */                                                                   \
                if (StrIterIndex(&UNPL(si_before_read)) == StrIterIndex(&si)) {                                            \
                    /* skip the value */                                                                                   \
                    UNPL(read_si) = JSkipValue(si);                                                                        \
                                                                                                                           \
                    /* if still no advancement in read position */                                                         \
                    if (StrIterIndex(&UNPL(read_si)) == StrIterIndex(&si)) {                                               \
                        LOG_ERROR("Failed to parse value. Invalid JSON.");                                                 \
                        StrDeinit(&key);                                                                                   \
Last updated on