From 37964b40ee94fdfdc9a8408a9ade9d7f3cb94683 Mon Sep 17 00:00:00 2001 From: "Serge A. Zaitsev" Date: Wed, 26 Sep 2012 20:19:21 +0300 Subject: [PATCH] added optional parent token pointer support. Increases parsing speed --- jsmn.c | 33 ++++++++++++++++++++++++++++++++- jsmn.h | 3 +++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/jsmn.c b/jsmn.c index ceb6122..ef704f5 100644 --- a/jsmn.c +++ b/jsmn.c @@ -14,6 +14,9 @@ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, tok = &tokens[parser->toknext++]; tok->start = tok->end = -1; tok->size = 0; +#ifdef JSMN_PARENT_LINKS + tok->parent = -1; +#endif return tok; } @@ -65,6 +68,9 @@ found: return JSMN_ERROR_NOMEM; } jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos); +#ifdef JSMN_PARENT_LINKS + token->parent = parser->toksuper; +#endif parser->pos--; return JSMN_SUCCESS; } @@ -92,6 +98,9 @@ static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js, return JSMN_ERROR_NOMEM; } jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos); +#ifdef JSMN_PARENT_LINKS + token->parent = parser->toksuper; +#endif return JSMN_SUCCESS; } @@ -137,14 +146,35 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens, token = jsmn_alloc_token(parser, tokens, num_tokens); if (token == NULL) return JSMN_ERROR_NOMEM; - if (parser->toksuper != -1) + if (parser->toksuper != -1) { tokens[parser->toksuper].size++; +#ifdef JSMN_PARENT_LINKS + token->parent = parser->toksuper; +#endif + } token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY); token->start = parser->pos; parser->toksuper = parser->toknext - 1; break; case '}': case ']': type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY); +#ifdef JSMN_PARENT_LINKS + if (parser->toknext < 1) { + return JSMN_ERROR_INVAL; + } + token = &tokens[parser->toknext - 1]; + for (;;) { + if (token->start != -1 && token->end == -1) { + token->end = parser->pos + 1; + parser->toksuper = token->parent; + break; + } + if (token->parent == -1) { + break; + } + token = &tokens[token->parent]; + } +#else for (i = parser->toknext - 1; i >= 0; i--) { token = &tokens[i]; if (token->start != -1 && token->end == -1) { @@ -165,6 +195,7 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens, break; } } +#endif break; case '\"': r = jsmn_parse_string(parser, js, tokens, num_tokens); diff --git a/jsmn.h b/jsmn.h index 91e285d..5914239 100644 --- a/jsmn.h +++ b/jsmn.h @@ -37,6 +37,9 @@ typedef struct { int start; int end; int size; +#ifdef JSMN_PARENT_LINKS + int parent; +#endif } jsmntok_t; /**