Skip to content

StrIterPeek

StrIterPeek

Description

Peek current character without advancing

Parameters

Name Direction Description
mi in StrIter object

Success

Returns current character

Failure

Returns null character if exhausted

Usage example (Cross-references)

Usage examples (Cross-references)
    
    static StrIter kvconfig_consume_line_end(StrIter si) {
        if (StrIterPeek(&si) == '\r') {
            StrIterNext(&si);
        }
        }
    
        if (StrIterPeek(&si) == '\n') {
            StrIterNext(&si);
        }
    
    StrIter KvConfigSkipWhitespace(StrIter si) {
        while (StrIterRemainingLength(&si) && kvconfig_is_space(StrIterPeek(&si))) {
            StrIterNext(&si);
        }
    
    StrIter KvConfigSkipLine(StrIter si) {
        while (StrIterRemainingLength(&si) && !kvconfig_is_line_end(StrIterPeek(&si))) {
            StrIterNext(&si);
        }
    
        while (StrIterRemainingLength(&si)) {
            char c = StrIterPeek(&si);
    
            if (c == '=' || c == ':' || kvconfig_is_space(c) || kvconfig_is_line_end(c) || kvconfig_is_comment_start(c)) {
        si = KvConfigSkipWhitespace(si);
    
        if (!StrIterRemainingLength(&si) || kvconfig_is_line_end(StrIterPeek(&si)) ||
            kvconfig_is_comment_start(StrIterPeek(&si))) {
            return si;
    
        if (!StrIterRemainingLength(&si) || kvconfig_is_line_end(StrIterPeek(&si)) ||
            kvconfig_is_comment_start(StrIterPeek(&si))) {
            return si;
        }
        }
    
        quote = StrIterPeek(&si);
        if (quote == '"' || quote == '\'') {
            StrIterNext(&si);
    
            while (StrIterRemainingLength(&si)) {
                char c = StrIterPeek(&si);
    
                if (c == quote) {
                    }
    
                    c = StrIterPeek(&si);
                    switch (c) {
                        case 'n' :
        }
    
        while (StrIterRemainingLength(&si) && !kvconfig_is_line_end(StrIterPeek(&si))) {
            char c = StrIterPeek(&si);
    
        while (StrIterRemainingLength(&si) && !kvconfig_is_line_end(StrIterPeek(&si))) {
            char c = StrIterPeek(&si);
    
            if (kvconfig_is_comment_start(c) && value->length > 0 && kvconfig_is_space(value->data[value->length - 1])) {
        si = KvConfigSkipWhitespace(si);
    
        if (StrIterPeek(&si) != '=' && StrIterPeek(&si) != ':') {
            LOG_ERROR("Expected '=' or ':' after config key");
            StrClear(key);
    
        si = KvConfigSkipWhitespace(si);
        if (kvconfig_is_comment_start(StrIterPeek(&si))) {
            si = KvConfigSkipLine(si);
        } else if (!kvconfig_is_line_end(StrIterPeek(&si))) {
        if (kvconfig_is_comment_start(StrIterPeek(&si))) {
            si = KvConfigSkipLine(si);
        } else if (!kvconfig_is_line_end(StrIterPeek(&si))) {
            LOG_ERROR("Unexpected trailing characters after config value");
            StrClear(key);
    
            while (StrIterRemainingLength(&si)) {
                char c = StrIterPeek(&si);
    
                if (c == '\n') {
                if (kvconfig_is_space(c)) {
                    si = KvConfigSkipWhitespace(si);
                    if (kvconfig_is_line_end(StrIterPeek(&si))) {
                        si = kvconfig_consume_line_end(si);
                        continue;
            }
    
            if (kvconfig_is_comment_start(StrIterPeek(&si))) {
                si = KvConfigSkipLine(si);
                continue;
    
        // starting of an object
        if (StrIterPeek(&si) != '{') {
            LOG_ERROR("Invalid object start. Expected '{'.");
            return saved_si;
    
        // while not at the end of object.
        while (StrIterPeek(&si) && StrIterPeek(&si) != '}') {
            if (expect_comma) {
                if (StrIterPeek(&si) != ',') {
        while (StrIterPeek(&si) && StrIterPeek(&si) != '}') {
            if (expect_comma) {
                if (StrIterPeek(&si) != ',') {
                    LOG_ERROR(
                        "Expected ',' between key/value pairs in object. Invalid "
            si = JSkipWhitespace(si);
    
            if (StrIterPeek(&si) != ':') {
                LOG_ERROR("Expected ':' after key string. Failed to read JSON");
                StrDeinit(&key);
        }
    
        char c = StrIterPeek(&si);
        if (c != '}') {
            LOG_ERROR("Expected end of object '}' but found '{c}'", c);
    
        // starting of an object
        if (StrIterPeek(&si) != '[') {
            LOG_ERROR("Invalid array start. Expected '['.");
            return saved_si;
    
        // while not at the end of array.
        while (StrIterPeek(&si) && StrIterPeek(&si) != ']') {
            if (expect_comma) {
                if (StrIterPeek(&si) != ',') {
        while (StrIterPeek(&si) && StrIterPeek(&si) != ']') {
            if (expect_comma) {
                if (StrIterPeek(&si) != ',') {
                    LOG_ERROR("Expected ',' between values in array. Invalid JSON array.");
                    return saved_si;
    
        // end of array
        if (StrIterPeek(&si) != ']') {
            LOG_ERROR("Invalid end of array. Expected ']'.");
            return saved_si;
        }
    
        while (StrIterRemainingLength(&si) && StrIterPeek(&si)) {
            switch (StrIterPeek(&si)) {
                case ' ' :
    
        while (StrIterRemainingLength(&si) && StrIterPeek(&si)) {
            switch (StrIterPeek(&si)) {
                case ' ' :
                case '\t' :
    
        // string start
        if (StrIterRemainingLength(&si) && StrIterPeek(&si) == '"') {
            StrIterNext(&si);
    
            // while a printable character
            while (StrIterRemainingLength(&si) && StrIterPeek(&si)) {
                // three cases
                // - end of string (return)
                // - an escape sequence (processed and appended)
                // - acceptable string character (appended)
                switch (StrIterPeek(&si)) {
                    // end of string
                    case '"' :
                        }
    
                        switch (StrIterPeek(&si)) {
                            // escape sequence
                            case '\\' :
                    // default allowed characters
                    default :
                        StrPushBack(str, StrIterPeek(&si));
                        StrIterNext(&si);
                        break;
    
        bool is_neg = false;
        if (StrIterPeek(&si) == '-') {
            is_neg = true;
            StrIterNext(&si);
        bool is_parsing         = true;
    
        while (is_parsing && StrIterRemainingLength(&si) && StrIterPeek(&si)) {
            switch (StrIterPeek(&si)) {
                case 'E' :
    
        while (is_parsing && StrIterRemainingLength(&si) && StrIterPeek(&si)) {
            switch (StrIterPeek(&si)) {
                case 'E' :
                case 'e' :
                    has_exp = true;
                    is_flt  = true;
                    StrPushBack(&ns, StrIterPeek(&si));
                    StrIterNext(&si);
                    break;
                    }
                    is_flt = true;
                    StrPushBack(&ns, StrIterPeek(&si));
                    StrIterNext(&si);
                    break;
                case '8' :
                case '9' :
                    StrPushBack(&ns, StrIterPeek(&si));
                    StrIterNext(&si);
                    break;
                    }
                    has_exp_plus_minus = true;
                    StrPushBack(&ns, StrIterPeek(&si));
                    StrIterNext(&si);
                    break;
    
        if (StrIterRemainingLength(&si) >= 4) {
            if (StrIterPeek(&si) == 't') {
                const char *pos = StrIterPos(&si);
                if (pos && ZstrCompareN(pos, "true", 4) == 0) {
    
            if (StrIterRemainingLength(&si) >= 5) {
                if (StrIterPeek(&si) == 'f') {
                    const char *pos = StrIterPos(&si);
                    if (pos && ZstrCompareN(pos, "false", 5) == 0) {
        *is_null = false;
        if (StrIterRemainingLength(&si) >= 4) {
            if (StrIterPeek(&si) == 'n') {
                const char *pos = StrIterPos(&si);
                if (pos && ZstrCompareN(pos, "null", 4) == 0) {
    
        // check for true/false
        if (StrIterPeek(&si) == 't' || StrIterPeek(&si) == 'f') {
            StrIter before_si = si;
            bool    b;
    
        // check for null
        if (StrIterPeek(&si) == 'n') {
            StrIter before_si = si;
            bool    n;
    
        // expecting a string
        if (StrIterPeek(&si) == '"') {
            StrIter before_si = si;
            Str     s         = StrInit();
    
        // looks like starting of a number?
        if (StrIterPeek(&si) == '-' || (StrIterPeek(&si) >= '0' && StrIterPeek(&si) <= '9')) {
            StrIter before_si = si;
            Number  num;
    
        // looks like starting of an object
        if (StrIterPeek(&si) == '{') {
            StrIter before_si = si;
            si                = JSkipObject(si);
    
        // looks like starting of an array
        if (StrIterPeek(&si) == '[') {
            StrIter before_si = si;
            si                = JSkipArray(si);
        }
    
        if (StrIterPeek(&si) != '{') {
            WriteFmt("[DEBUG] Peek check failed: expected '{', got '{c}'\n", StrIterPeek(&si));
            success = false;
    
        if (StrIterPeek(&si) != '{') {
            WriteFmt("[DEBUG] Peek check failed: expected '{', got '{c}'\n", StrIterPeek(&si));
            success = false;
        }
                                                                                                                           \
            /* starting of an object */                                                                                    \
            if (StrIterPeek(&si) != '[') {                                                                                 \
                LOG_ERROR("Invalid array start. Expected '['.");                                                           \
                si = saved_si;                                                                                             \
                                                                                                                           \
            /* while not at the end of array. */                                                                           \
            while (StrIterPeek(&si) && StrIterPeek(&si) != ']') {                                                          \
                if (expect_comma) {                                                                                        \
                    if (StrIterPeek(&si) != ',') {                                                                         \
            while (StrIterPeek(&si) && StrIterPeek(&si) != ']') {                                                          \
                if (expect_comma) {                                                                                        \
                    if (StrIterPeek(&si) != ',') {                                                                         \
                        LOG_ERROR("Expected ',' between values in array. Invalid JSON array.");                            \
                        failed = true;                                                                                     \
            /* end of array */                                                                                             \
            if (!failed) {                                                                                                 \
                if (StrIterPeek(&si) != ']') {                                                                             \
                    LOG_ERROR("Invalid end of array. Expected ']'.");                                                      \
                    failed = true;                                                                                         \
                                                                                                                           \
            /* starting of an object */                                                                                    \
            if (StrIterPeek(&si) != '{') {                                                                                 \
                LOG_ERROR("Invalid object start. Expected '{'.");                                                          \
                si = saved_si;                                                                                             \
                                                                                                                           \
            /* while not at the end of object. */                                                                          \
            while (StrIterPeek(&si) && StrIterPeek(&si) != '}') {                                                          \
                if (expect_comma) {                                                                                        \
                    if (StrIterPeek(&si) != ',') {                                                                         \
            while (StrIterPeek(&si) && StrIterPeek(&si) != '}') {                                                          \
                if (expect_comma) {                                                                                        \
                    if (StrIterPeek(&si) != ',') {                                                                         \
                        LOG_ERROR("Expected ',' after key/value pairs in object. Invalid JSON object.");                   \
                        failed = true;                                                                                     \
                                                                                                                           \
                                                                                                                           \
                if (StrIterPeek(&si) != ':') {                                                                             \
                    LOG_ERROR("Expected ':' after key string. Failed to read JSON");                                       \
                    StrDeinit(&key);                                                                                       \
                                                                                                                           \
            if (!failed) {                                                                                                 \
                char c = StrIterPeek(&si);                                                                                 \
                if (c != '}') {                                                                                            \
                    LOG_ERROR("Expected end of object '}' but found '{c}'", c);                                            \
Last updated on