diff --git a/jsmn.c b/jsmn.c index 893f23c..e22d8a3 100644 --- a/jsmn.c +++ b/jsmn.c @@ -5,7 +5,7 @@ /** * Allocates a fresh unused token from the token pull. */ -static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, +static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, jsmntok_t *tokens, size_t num_tokens) { jsmntok_t *tok; if (parser->toknext >= num_tokens) { @@ -23,7 +23,7 @@ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, /** * Fills token type and boundaries. */ -static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type, +static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type, int start, int end) { token->type = type; token->start = start; @@ -234,7 +234,7 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, if (parser->toksuper != -1 && tokens != NULL) tokens[parser->toksuper].size++; break; - case '\t' : case '\r' : case '\n' : case ' ': + case '\t' : case '\r' : case '\n' : case ' ': break; case ':': parser->toksuper = parser->toknext - 1; @@ -292,7 +292,7 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, } /** - * Creates a new parser based over a given buffer with an array of tokens + * Creates a new parser based over a given buffer with an array of tokens * available. */ void jsmn_init(jsmn_parser *parser) { diff --git a/jsmn_test.c b/jsmn_test.c index c9c47db..7fbcee1 100644 --- a/jsmn_test.c +++ b/jsmn_test.c @@ -120,7 +120,7 @@ int test_primitive() { js = "\"boolVar\" : true"; jsmn_init(&p); r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r >= 0 && tok[0].type == JSMN_STRING + check(r >= 0 && tok[0].type == JSMN_STRING && tok[1].type == JSMN_PRIMITIVE); check(TOKEN_STRING(js, tok[0], "boolVar")); check(TOKEN_STRING(js, tok[1], "true")); @@ -128,7 +128,7 @@ int test_primitive() { js = "\"boolVar\" : false"; jsmn_init(&p); r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r >= 0 && tok[0].type == JSMN_STRING + check(r >= 0 && tok[0].type == JSMN_STRING && tok[1].type == JSMN_PRIMITIVE); check(TOKEN_STRING(js, tok[0], "boolVar")); check(TOKEN_STRING(js, tok[1], "false")); @@ -136,7 +136,7 @@ int test_primitive() { js = "\"intVar\" : 12345"; jsmn_init(&p); r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r >= 0 && tok[0].type == JSMN_STRING + check(r >= 0 && tok[0].type == JSMN_STRING && tok[1].type == JSMN_PRIMITIVE); check(TOKEN_STRING(js, tok[0], "intVar")); check(TOKEN_STRING(js, tok[1], "12345")); @@ -144,7 +144,7 @@ int test_primitive() { js = "\"floatVar\" : 12.345"; jsmn_init(&p); r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r >= 0 && tok[0].type == JSMN_STRING + check(r >= 0 && tok[0].type == JSMN_STRING && tok[1].type == JSMN_PRIMITIVE); check(TOKEN_STRING(js, tok[0], "floatVar")); check(TOKEN_STRING(js, tok[1], "12.345")); @@ -152,7 +152,7 @@ int test_primitive() { js = "\"nullVar\" : null"; jsmn_init(&p); r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r >= 0 && tok[0].type == JSMN_STRING + check(r >= 0 && tok[0].type == JSMN_STRING && tok[1].type == JSMN_PRIMITIVE); check(TOKEN_STRING(js, tok[0], "nullVar")); check(TOKEN_STRING(js, tok[1], "null")); @@ -169,7 +169,7 @@ int test_string() { js = "\"strVar\" : \"hello world\""; jsmn_init(&p); r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r >= 0 && tok[0].type == JSMN_STRING + check(r >= 0 && tok[0].type == JSMN_STRING && tok[1].type == JSMN_STRING); check(TOKEN_STRING(js, tok[0], "strVar")); check(TOKEN_STRING(js, tok[1], "hello world")); @@ -177,7 +177,7 @@ int test_string() { js = "\"strVar\" : \"escapes: \\/\\r\\n\\t\\b\\f\\\"\\\\\""; jsmn_init(&p); r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r >= 0 && tok[0].type == JSMN_STRING + check(r >= 0 && tok[0].type == JSMN_STRING && tok[1].type == JSMN_STRING); check(TOKEN_STRING(js, tok[0], "strVar")); check(TOKEN_STRING(js, tok[1], "escapes: \\/\\r\\n\\t\\b\\f\\\"\\\\")); @@ -185,7 +185,7 @@ int test_string() { js = "\"strVar\" : \"\""; jsmn_init(&p); r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r >= 0 && tok[0].type == JSMN_STRING + check(r >= 0 && tok[0].type == JSMN_STRING && tok[1].type == JSMN_STRING); check(TOKEN_STRING(js, tok[0], "strVar")); check(TOKEN_STRING(js, tok[1], "")); @@ -263,18 +263,18 @@ int test_partial_array() { jsmn_init(&p); js = " [ 1, true, "; r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY + check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE); js = " [ 1, true, [123, \"hello"; r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY + check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE && tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE); js = " [ 1, true, [123, \"hello\"]"; r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY + check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE && tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE && tok[5].type == JSMN_STRING); @@ -283,7 +283,7 @@ int test_partial_array() { js = " [ 1, true, [123, \"hello\"]]"; r = jsmn_parse(&p, js, strlen(js), tok, 10); - check(r >= 0 && tok[0].type == JSMN_ARRAY + check(r >= 0 && tok[0].type == JSMN_ARRAY && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE && tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE && tok[5].type == JSMN_STRING); @@ -470,7 +470,7 @@ int test_count() { js = "[[], [[]], [[], []]]"; jsmn_init(&p); check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 7); - + js = "[\"a\", [[], []]]"; jsmn_init(&p); check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 5); @@ -486,7 +486,7 @@ int test_count() { js = "[1, 2, [3, \"a\"], null]"; jsmn_init(&p); check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 7); - + return 0; }