Commits

Gabriel Gritsch committed cf36da1

- Added possibility to specify the lenght of the input buffer (if not null-terminated).
- Added "extern C" to use it with CPP.

  • Participants
  • Parent commits 9d62107

Comments (0)

Files changed (2)

 #include <stdlib.h>
-#include <limits.h>
+#include <stdint.h>		//	for SIZE_MAX
+//#include <limits.h>	//	for UINT_MAX
 
 #include "jsmn.h"
 
 	return JSMN_ERROR_PART;
 }
 
+static inline jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens, unsigned int num_tokens)
+{
+	return jsmn_parseV2(parser, js, SIZE_MAX /*UINT_MAX*/, tokens, num_tokens);
+}
+
 /**
  * Parse JSON string and fill tokens.
  */
-jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, int js_length, jsmntok_t *tokens, 
+jsmnerr_t jsmn_parseV2(jsmn_parser *parser, const char *js, size_t js_length, jsmntok_t *tokens, 
 		unsigned int num_tokens) {
+	if (js==NULL || js_length==0)
+	{
+		return JSMN_ERROR_PART;
+	}
+	
 	jsmnerr_t r;
 	int i;
 	jsmntok_t *token;
-	
-	if (js_length <= 0)
-	{
-		js_length = INT_MAX;
-	}
 
 	for (; parser->pos < js_length && js[parser->pos] != '\0'; parser->pos++) {
 		char c;
 void jsmn_init(jsmn_parser *parser);
 
 /**
- * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
+ * Run JSON parser. It parses a JSON data null-terminated-string into and array of tokens, each describing
  * a single JSON object.
  */
-jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, int js_length,
+static inline jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js,
+		jsmntok_t *tokens, unsigned int num_tokens);
+
+/**
+ * Run JSON parser. It parses a JSON data buffer with specified length into and array of tokens, each describing
+ * a single JSON object.
+ */
+jsmnerr_t jsmn_parseV2(jsmn_parser *parser, const char *js, size_t js_length,
 		jsmntok_t *tokens, unsigned int num_tokens);
 
 #ifdef __cplusplus