Commits

Anonymous committed 0af8582

fixed: superior node size is now an index, not a pointer (safe to realloc)

Comments (0)

Files changed (3)

 	for (i = parser->toknext; i < num_tokens; i++) {
 		if (tokens[i].start == -1 && tokens[i].end == -1) {
 			parser->toknext = i + 1;
-			tokens[i].size = 0;
 			return &tokens[i];
 		}
 	}
 	token->type = type;
 	token->start = start;
 	token->end = end;
+	token->size = 0;
 }
 
 /**
 				token = jsmn_alloc_token(parser, tokens, num_tokens);
 				if (token == NULL)
 					return JSMN_ERROR_NOMEM;
-				if (parser->cursize != NULL)
-					(*parser->cursize)++;
+				if (parser->toksuper != -1)
+					tokens[parser->toksuper].size++;
 				token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
 				token->start = parser->pos;
-				parser->cursize = &token->size;
+				parser->toksuper = parser->toknext - 1;
 				break;
 			case '}': case ']':
 				type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
 						if (token->type != type) {
 							return JSMN_ERROR_INVAL;
 						}
-						parser->cursize = NULL;
+						parser->toksuper = -1;
 						token->end = parser->pos + 1;
 						break;
 					}
 				for (; i >= 0; i--) {
 					token = &tokens[i];
 					if (token->start != -1 && token->end == -1) {
-						parser->cursize = &token->size;
+						parser->toksuper = i;
 						break;
 					}
 				}
 			case '\"':
 				r = jsmn_parse_string(parser, js, tokens, num_tokens);
 				if (r < 0) return r;
-				if (parser->cursize != NULL)
-					(*parser->cursize)++;
+				if (parser->toksuper != -1)
+					tokens[parser->toksuper].size++;
 				break;
 			case '\t' : case '\r' : case '\n' : case ':' : case ',': case ' ': 
 				break;
 #endif
 				r = jsmn_parse_primitive(parser, js, tokens, num_tokens);
 				if (r < 0) return r;
-				if (parser->cursize != NULL)
-					(*parser->cursize)++;
+				if (parser->toksuper != -1)
+					tokens[parser->toksuper].size++;
 				break;
 
 #ifdef JSMN_STRICT
 void jsmn_init(jsmn_parser *parser) {
 	parser->pos = 0;
 	parser->toknext = 0;
-	parser->cursize = NULL;
+	parser->toksuper = -1;
 }
 
  * the string being parsed now and current position in that string
  */
 typedef struct {
-	unsigned int pos;
-	int toknext;
-	int *cursize;
+	unsigned int pos; /* offset in the JSON string */
+	int toknext; /* next token to allocate */
+	int toksuper; /* suporior token node, e.g parent object or array */
 } jsmn_parser;
 
 /**
 	js = "  [ 1, true, [123, \"hello\"]]";
 
 	for (i = 0; i < 6; i++) {
-		printf("i = %d\n", i);
 		jsmn_init(&p);
+		memset(toksmall, 0, sizeof(toksmall));
+		memset(toklarge, 0, sizeof(toklarge));
 		r = jsmn_parse(&p, js, toksmall, i);
 		check(r == JSMN_ERROR_NOMEM);
 
 
 		r = jsmn_parse(&p, js, toklarge, 10);
 		check(r == JSMN_SUCCESS);
+
+		check(toklarge[0].type == JSMN_ARRAY && toklarge[0].size == 3);
+		check(toklarge[3].type == JSMN_ARRAY && toklarge[3].size == 2);
 	}
 	return 0;
 }