Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3de359f

Browse files
committed
Simplify json lexing state
Instead of updating the length as we go, use a const pointer to end of the input, which we know already at the start. This simplifies the coding and may improve performance slightly, but the real motivation for doing this is to make further changes in this area easier to reason about. Discussion: https://www.postgresql.org/message-id/CAFBsxsGhaR2KQ5eisaK%3D6Vm60t%3DaxhD8Ckj1qFoCH1pktZi%2B2w%40mail.gmail.com
1 parent 3140f08 commit 3de359f

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

src/common/jsonapi.c

+8-15
Original file line numberDiff line numberDiff line change
@@ -519,26 +519,23 @@ JsonParseErrorType
519519
json_lex(JsonLexContext *lex)
520520
{
521521
char *s;
522-
int len;
522+
char *const end = lex->input + lex->input_length;
523523
JsonParseErrorType result;
524524

525525
/* Skip leading whitespace. */
526526
s = lex->token_terminator;
527-
len = s - lex->input;
528-
while (len < lex->input_length &&
529-
(*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
527+
while (s < end && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
530528
{
531529
if (*s++ == '\n')
532530
{
533531
++lex->line_number;
534532
lex->line_start = s;
535533
}
536-
len++;
537534
}
538535
lex->token_start = s;
539536

540537
/* Determine token type. */
541-
if (len >= lex->input_length)
538+
if (s >= end)
542539
{
543540
lex->token_start = NULL;
544541
lex->prev_token_terminator = lex->token_terminator;
@@ -623,7 +620,7 @@ json_lex(JsonLexContext *lex)
623620
* the whole word as an unexpected token, rather than just
624621
* some unintuitive prefix thereof.
625622
*/
626-
for (p = s; p - s < lex->input_length - len && JSON_ALPHANUMERIC_CHAR(*p); p++)
623+
for (p = s; p < end && JSON_ALPHANUMERIC_CHAR(*p); p++)
627624
/* skip */ ;
628625

629626
/*
@@ -672,21 +669,19 @@ static inline JsonParseErrorType
672669
json_lex_string(JsonLexContext *lex)
673670
{
674671
char *s;
675-
int len;
672+
char *const end = lex->input + lex->input_length;
676673
int hi_surrogate = -1;
677674

678675
if (lex->strval != NULL)
679676
resetStringInfo(lex->strval);
680677

681678
Assert(lex->input_length > 0);
682679
s = lex->token_start;
683-
len = lex->token_start - lex->input;
684680
for (;;)
685681
{
686682
s++;
687-
len++;
688683
/* Premature end of the string. */
689-
if (len >= lex->input_length)
684+
if (s >= end)
690685
{
691686
lex->token_terminator = s;
692687
return JSON_INVALID_TOKEN;
@@ -704,8 +699,7 @@ json_lex_string(JsonLexContext *lex)
704699
{
705700
/* OK, we have an escape character. */
706701
s++;
707-
len++;
708-
if (len >= lex->input_length)
702+
if (s >= end)
709703
{
710704
lex->token_terminator = s;
711705
return JSON_INVALID_TOKEN;
@@ -718,8 +712,7 @@ json_lex_string(JsonLexContext *lex)
718712
for (i = 1; i <= 4; i++)
719713
{
720714
s++;
721-
len++;
722-
if (len >= lex->input_length)
715+
if (s >= end)
723716
{
724717
lex->token_terminator = s;
725718
return JSON_INVALID_TOKEN;

0 commit comments

Comments
 (0)