Tue, 10 Dec 2024 00:52:15 +0100
json: enable multiple subsequent fills - relates to #431
src/json.c | file | annotate | diff | comparison | revisions |
--- a/src/json.c Tue Dec 10 00:19:45 2024 +0100 +++ b/src/json.c Tue Dec 10 00:52:15 2024 +0100 @@ -188,6 +188,7 @@ CxJsonTokenType ctype = char2ttype(c); // start of new token? if (ttype == CX_JSON_NO_TOKEN) { if (ctype == CX_JSON_TOKEN_SPACE) { + json->buffer.pos++; continue; } else if (ctype == CX_JSON_TOKEN_STRING) { // begin string @@ -237,13 +238,13 @@ if (ttype != CX_JSON_NO_TOKEN) { // uncompleted token - size_t uncompeted_len = json->buffer.size - token_start; + size_t uncompleted_len = json->buffer.size - token_start; if (json->uncompleted.tokentype == CX_JSON_NO_TOKEN) { // current token is uncompleted // save current token content CxJsonToken uncompleted = { ttype, true, - cx_strdup(cx_strn(json->buffer.space + token_start, uncompeted_len)) + cx_strdup(cx_strn(json->buffer.space + token_start, uncompleted_len)) }; if (uncompleted.content.ptr == NULL) { return CX_JSON_BUFFER_ALLOC_FAILED; @@ -254,12 +255,14 @@ // combine the uncompleted token with the current token assert(json->uncompleted.allocated); cxmutstr str = cx_strcat_m(json->uncompleted.content, 1, - cx_strn(json->buffer.space + token_start, uncompeted_len)); + cx_strn(json->buffer.space + token_start, uncompleted_len)); if (str.ptr == NULL) { return CX_JSON_BUFFER_ALLOC_FAILED; } json->uncompleted.content = str; } + // advance the buffer position - we saved the stuff in the uncompleted token + json->buffer.pos += uncompleted_len; } return CX_JSON_INCOMPLETE_DATA; @@ -614,6 +617,7 @@ CxJsonStatus result; do { result = json_parse(json); + cxBufferShiftLeft(&json->buffer, json->buffer.pos); if (result == CX_JSON_NO_ERROR && json->states_size == 1) { // final state reached assert(json->states[0] == JP_STATE_VALUE_END);