enh(JSON): upgrade pdjson (#4686)

This commit is contained in:
Aleksandar Fabijanic 2024-09-13 17:49:22 -05:00 committed by GitHub
parent cefab15f9f
commit af104884df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -79,6 +79,7 @@ push(json_stream *json, enum json_type type)
return type; return type;
} }
/* Note: c is assumed not to be EOF. */
static enum json_type static enum json_type
pop(json_stream *json, int c, enum json_type expected) pop(json_stream *json, int c, enum json_type expected)
{ {
@ -101,14 +102,17 @@ static int buffer_peek(struct json_source *source)
static int buffer_get(struct json_source *source) static int buffer_get(struct json_source *source)
{ {
int c = source->peek(source); int c = source->peek(source);
if (c != EOF)
source->position++; source->position++;
return c; return c;
} }
static int stream_get(struct json_source *source) static int stream_get(struct json_source *source)
{ {
int c = fgetc(source->source.stream.stream);
if (c != EOF)
source->position++; source->position++;
return fgetc(source->source.stream.stream); return c;
} }
static int stream_peek(struct json_source *source) static int stream_peek(struct json_source *source)
@ -146,7 +150,11 @@ is_match(json_stream *json, const char *pattern, enum json_type type)
int c; int c;
for (const char *p = pattern; *p; p++) { for (const char *p = pattern; *p; p++) {
if (*p != (c = json->source.get(&json->source))) { if (*p != (c = json->source.get(&json->source))) {
if (c != EOF) {
json_error(json, "expected '%c' instead of byte '%c'", *p, c); json_error(json, "expected '%c' instead of byte '%c'", *p, c);
} else {
json_error(json, "expected '%c' instead of end of text", *p);
}
return JSON_ERROR; return JSON_ERROR;
} }
} }
@ -465,7 +473,7 @@ read_utf8(json_stream* json, int next_char)
int i; int i;
for (i = 1; i < count; ++i) for (i = 1; i < count; ++i)
{ {
buffer[i] = json->source.get(&json->source);; buffer[i] = json->source.get(&json->source);
} }
if (!is_legal_utf8((unsigned char*) buffer, count)) if (!is_legal_utf8((unsigned char*) buffer, count))
@ -535,7 +543,11 @@ read_digits(json_stream *json)
} }
if (nread == 0) { if (nread == 0) {
if (c != EOF) {
json_error(json, "expected digit instead of byte '%c'", c); json_error(json, "expected digit instead of byte '%c'", c);
} else {
json_error(json, "%s", "expected digit instead of end of text");
}
return -1; return -1;
} }
@ -552,10 +564,14 @@ read_number(json_stream *json, int c)
if (is_digit(c)) { if (is_digit(c)) {
return read_number(json, c); return read_number(json, c);
} else { } else {
if (c != EOF) {
json_error(json, "unexpected byte '%c' in number", c); json_error(json, "unexpected byte '%c' in number", c);
} else {
json_error(json, "%s", "unexpected end of text in number");
}
return JSON_ERROR; return JSON_ERROR;
} }
} else if (c >= '1' && c <= '9') { } else if (strchr("123456789", c) != NULL) {
c = json->source.peek(&json->source); c = json->source.peek(&json->source);
if (is_digit(c)) { if (is_digit(c)) {
if (read_digits(json) != 0) if (read_digits(json) != 0)
@ -594,7 +610,11 @@ read_number(json_stream *json, int c)
if (read_digits(json) != 0) if (read_digits(json) != 0)
return JSON_ERROR; return JSON_ERROR;
} else { } else {
if (c != EOF) {
json_error(json, "unexpected byte '%c' in number", c); json_error(json, "unexpected byte '%c' in number", c);
} else {
json_error(json, "%s", "unexpected end of text in number");
}
return JSON_ERROR; return JSON_ERROR;
} }
} }
@ -732,7 +752,11 @@ enum json_type json_next(json_stream *json)
} else if (c == ']') { } else if (c == ']') {
return pop(json, c, JSON_ARRAY); return pop(json, c, JSON_ARRAY);
} else { } else {
if (c != EOF) {
json_error(json, "unexpected byte '%c'", c); json_error(json, "unexpected byte '%c'", c);
} else {
json_error(json, "%s", "unexpected end of text");
}
return JSON_ERROR; return JSON_ERROR;
} }
} else if (json->stack[json->stack_top].type == JSON_OBJECT) { } else if (json->stack[json->stack_top].type == JSON_OBJECT) {
@ -927,7 +951,10 @@ void json_open_stream(json_stream *json, FILE * stream)
static int user_get(struct json_source *json) static int user_get(struct json_source *json)
{ {
return json->source.user.get(json->source.user.ptr); int c = json->source.user.get(json->source.user.ptr);
if (c != EOF)
json->position++;
return c;
} }
static int user_peek(struct json_source *json) static int user_peek(struct json_source *json)