Commits

Anonymous committed dfb2c0c

removed JSON string and token array from parser internals, because they must not be saved between parse() calls

Comments (0)

Files changed (3)

 /**
  * 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) {
 	unsigned int i;
-	jsmntok_t *tokens = parser->tokens;
-	for (i = parser->curtoken; i<parser->num_tokens; i++) {
+	for (i = parser->curtoken; i < num_tokens; i++) {
 		if (tokens[i].start == -1 && tokens[i].end == -1) {
 			parser->curtoken = i;
 			tokens[i].size = 0;
 /**
  * Fills next available token with JSON primitive.
  */
-static int jsmn_parse_primitive(jsmn_parser *parser) {
-	const char *js;
+static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
+		jsmntok_t *tokens, size_t num_tokens) {
 	jsmntok_t *token;
 	int start;
 
 	start = parser->pos;
 
-	js = parser->js;
-
 	for (; js[parser->pos] != '\0'; parser->pos++) {
 		switch (js[parser->pos]) {
 			case '\t' : case '\r' : case '\n' : case ' ' :
 			case ','  : case ']'  : case '}' :
-				token = jsmn_alloc_token(parser);
+				token = jsmn_alloc_token(parser, tokens, num_tokens);
 				if (token == NULL)
 					return JSMN_ERROR_NOMEM;
 				jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
 /**
  * Filsl next token with JSON string.
  */
-static int jsmn_parse_string(jsmn_parser *parser) {
-	const char *js;
+static int jsmn_parse_string(jsmn_parser *parser, const char *js,
+		jsmntok_t *tokens, size_t num_tokens) {
 	jsmntok_t *token;
 
 	int start = parser->pos;
 
-	js = parser->js;
-
 	parser->pos++;
 
 	/* Skip starting quote */
 
 		/* Quote: end of string */
 		if (c == '\"') {
-			token = jsmn_alloc_token(parser);
+			token = jsmn_alloc_token(parser, tokens, num_tokens);
 			if (token == NULL)
 				return JSMN_ERROR_NOMEM;
 			jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
 	jsmntok_t *token;
 
 	/* initialize the rest of tokens (they could be reallocated) */
-	parser->num_tokens = num_tokens;
-	parser->tokens = tokens;
-	parser->js = js;
-	for (i = parser->curtoken; i < parser->num_tokens; i++) {
-		jsmn_fill_token(&parser->tokens[i], JSMN_PRIMITIVE, -1, -1);
+	for (i = parser->curtoken; i < num_tokens; i++) {
+		jsmn_fill_token(&tokens[i], JSMN_PRIMITIVE, -1, -1);
 	}
 
 	for (; js[parser->pos] != '\0'; parser->pos++) {
 		c = js[parser->pos];
 		switch (c) {
 			case '{': case '[':
-				token = jsmn_alloc_token(parser);
+				token = jsmn_alloc_token(parser, tokens, num_tokens);
 				if (token == NULL)
 					return JSMN_ERROR_NOMEM;
 				if (parser->cursize != NULL)
 			case '}': case ']':
 				type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
 				for (i = parser->curtoken; i >= 0; i--) {
-					token = &parser->tokens[i];
+					token = &tokens[i];
 					if (token->start != -1 && token->end == -1) {
 						if (token->type != type) {
 							return JSMN_ERROR_INVAL;
 					}
 				}
 				for (; i >= 0; i--) {
-					token = &parser->tokens[i];
+					token = &tokens[i];
 					if (token->start != -1 && token->end == -1) {
 						parser->cursize = &token->size;
 						break;
 			case '-': case '0': case '1' : case '2': case '3' : case '4':
 			case '5': case '6': case '7' : case '8': case '9':
 			case 't': case 'f': case 'n' :
-				r = jsmn_parse_primitive(parser);
+				r = jsmn_parse_primitive(parser, js, tokens, num_tokens);
 				if (r < 0) return r;
 				if (parser->cursize != NULL)
 					(*parser->cursize)++;
 				break;
 			case '\"':
-				r = jsmn_parse_string(parser);
+				r = jsmn_parse_string(parser, js, tokens, num_tokens);
 				if (r < 0) return r;
 				if (parser->cursize != NULL)
 					(*parser->cursize)++;
  * the string being parsed now and current position in that string
  */
 typedef struct {
-	const char *js;
 	unsigned int pos;
-	unsigned int num_tokens;
 	int curtoken;
 	int *cursize;
-	jsmntok_t *tokens;
 } jsmn_parser;
 
 /**
 	jsmn_parser p;
 	jsmntok_t tokens[10];
 	const char *js;
+	return 0;
 }
 
 int main() {
 	test(test_simple, "general test for a simple JSON string");
+	test(test_primitive, "test primitive JSON data types");
 	return 0;
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.