From 0abe55f237eafe9ef3520f22ec8454297ecb2919 Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Mon, 13 Jul 2026 21:18:46 +0000 Subject: [PATCH v1 2/3] Extract whitespace skip code in json_lex() Signed-off-by: Tristan Partin --- src/common/jsonapi.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/common/jsonapi.c b/src/common/jsonapi.c index 70bf4687c71..36675d97e0b 100644 --- a/src/common/jsonapi.c +++ b/src/common/jsonapi.c @@ -273,6 +273,9 @@ static char JSON_PROD_GOAL[] = {JSON_TOKEN_END, JSON_NT_JSON, 0}; static void reset_partial_state(JsonLexContext *lex); static JsonParseErrorType continue_partial_lex(JsonLexContext *lex); +static inline const char *skip_leading_whitespace(JsonLexContext *lex, + const char *s, + const char *end); static inline JsonParseErrorType json_lex_string(JsonLexContext *lex); static inline JsonParseErrorType json_lex_number(JsonLexContext *lex, const char *s, bool *num_err, size_t *total_len); @@ -1822,6 +1825,28 @@ continue_partial_lex(JsonLexContext *lex) return partial_result; } +/* + * Skip leading whitespace by pointing JsonLexContext::token_start past it + */ +static inline const char * +skip_leading_whitespace(JsonLexContext *lex, + const char *s, + const char *end) +{ + while (s < end && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')) + { + if (*s++ == '\n') + { + ++lex->line_number; + lex->line_start = s; + } + } + + lex->token_start = s; + + return s; +} + /* * Lex one token from the input stream. * @@ -1868,16 +1893,7 @@ json_lex(JsonLexContext *lex) s = lex->token_terminator; - /* Skip leading whitespace. */ - while (s < end && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')) - { - if (*s++ == '\n') - { - ++lex->line_number; - lex->line_start = s; - } - } - lex->token_start = s; + s = skip_leading_whitespace(lex, s, end); /* Determine token type. */ if (s >= end) -- Tristan Partin https://tristan.partin.io