MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / json_next_string_token

Function json_next_string_token

deps/lua/src/lua_cjson.c:885–943  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

883}
884
885static void json_next_string_token(json_parse_t *json, json_token_t *token)
886{
887 char *escape2char = json->cfg->escape2char;
888 char ch;
889
890 /* Caller must ensure a string is next */
891 assert(*json->ptr == '"');
892
893 /* Skip " */
894 json->ptr++;
895
896 /* json->tmp is the temporary strbuf used to accumulate the
897 * decoded string value.
898 * json->tmp is sized to handle JSON containing only a string value.
899 */
900 strbuf_reset(json->tmp);
901
902 while ((ch = *json->ptr) != '"') {
903 if (!ch) {
904 /* Premature end of the string */
905 json_set_token_error(token, json, "unexpected end of string");
906 return;
907 }
908
909 /* Handle escapes */
910 if (ch == '\\') {
911 /* Fetch escape character */
912 ch = *(json->ptr + 1);
913
914 /* Translate escape code and append to tmp string */
915 ch = escape2char[(unsigned char)ch];
916 if (ch == 'u') {
917 if (json_append_unicode_escape(json) == 0)
918 continue;
919
920 json_set_token_error(token, json,
921 "invalid unicode escape code");
922 return;
923 }
924 if (!ch) {
925 json_set_token_error(token, json, "invalid escape code");
926 return;
927 }
928
929 /* Skip '\' */
930 json->ptr++;
931 }
932 /* Append normal character or translated single character
933 * Unicode escapes are handled above */
934 strbuf_append_char_unsafe(json->tmp, ch);
935 json->ptr++;
936 }
937 json->ptr++; /* Eat final quote (") */
938
939 strbuf_ensure_null(json->tmp);
940
941 token->type = T_STRING;
942 token->value.string = strbuf_string(json->tmp, &token->string_len);

Callers 1

json_next_tokenFunction · 0.85

Calls 6

strbuf_resetFunction · 0.85
json_set_token_errorFunction · 0.85
strbuf_ensure_nullFunction · 0.85
strbuf_stringFunction · 0.85

Tested by

no test coverage detected