Browse Source

Support utf8 keys in json_scanf fmt string

PUBLISHED_FROM=7d057e66c9c974bff56f72c1a6f6db5c69854fcb
Sergey Lyubka 9 years ago
parent
commit
ebbfdde30d
3 changed files with 19 additions and 4 deletions
  1. 2 2
      frozen.c
  2. 0 1
      frozen.h
  3. 17 1
      unit_test.c

+ 2 - 2
frozen.c

@@ -402,7 +402,7 @@ static int parse_value(struct frozen *f) {
 static int parse_key(struct frozen *f) {
 static int parse_key(struct frozen *f) {
   int ch = cur(f);
   int ch = cur(f);
 #if 0
 #if 0
-  printf("%s 1 [%.*s]\n", __func__, (int) (f->end - f->cur), f->cur);
+  printf("%s [%.*s]\n", __func__, (int) (f->end - f->cur), f->cur);
 #endif
 #endif
   if (is_alpha(ch)) {
   if (is_alpha(ch)) {
     TRY(parse_identifier(f));
     TRY(parse_identifier(f));
@@ -878,7 +878,7 @@ int json_vscanf(const char *s, int len, const char *fmt, va_list ap) {
           break;
           break;
         }
         }
       }
       }
-    } else if (is_alpha(fmt[i])) {
+    } else if (is_alpha(fmt[i]) || get_utf8_char_len(fmt[i]) > 1) {
       const char *delims = ": \r\n\t";
       const char *delims = ": \r\n\t";
       int key_len = strcspn(&fmt[i], delims);
       int key_len = strcspn(&fmt[i], delims);
       if ((p = strrchr(path, '.')) != NULL) p[1] = '\0';
       if ((p = strrchr(path, '.')) != NULL) p[1] = '\0';

+ 0 - 1
frozen.h

@@ -50,7 +50,6 @@ struct json_token {
 #define JSON_STRING_INVALID -1
 #define JSON_STRING_INVALID -1
 #define JSON_STRING_INCOMPLETE -2
 #define JSON_STRING_INCOMPLETE -2
 #define JSON_TOKEN_ARRAY_TOO_SMALL -3
 #define JSON_TOKEN_ARRAY_TOO_SMALL -3
-#define JSON_INVALID_FORMAT_STRING -4
 
 
 int parse_json(const char *json_string, int json_string_length,
 int parse_json(const char *json_string, int json_string_length,
                struct json_token *tokens_array, int size_of_tokens_array);
                struct json_token *tokens_array, int size_of_tokens_array);

+ 17 - 1
unit_test.c

@@ -399,7 +399,6 @@ static void scan_array(const char *str, int len, void *user_data) {
   struct json_token t;
   struct json_token t;
   int i;
   int i;
   char *buf = (char *) user_data;
   char *buf = (char *) user_data;
-  printf("Parsing array: %.*s\n", len, str);
   for (i = 0; json_scanf_array_elem(str, len, ".x", i, &t) > 0; i++) {
   for (i = 0; json_scanf_array_elem(str, len, ".x", i, &t) > 0; i++) {
     sprintf(buf + strlen(buf), "%d[%.*s] ", i, t.len, t.ptr);
     sprintf(buf + strlen(buf), "%d[%.*s] ", i, t.len, t.ptr);
   }
   }
@@ -421,6 +420,23 @@ static const char *test_scanf(void) {
   ASSERT(strcmp(d, "hi%20there") == 0);
   ASSERT(strcmp(d, "hi%20there") == 0);
   free(d);
   free(d);
 
 
+  {
+    /* Test errors */
+    const char *str = "{foo:1, bar:[2,3,4]}";
+    ASSERT(json_parse(str, strlen(str), NULL, NULL) == (int) strlen(str));
+    for (size_t i = 1; i < strlen(str); i++) {
+      ASSERT(json_parse(str, i, NULL, NULL) == JSON_STRING_INCOMPLETE);
+    }
+  }
+
+  {
+    /* Test that paths are utf8 */
+    const char *str = "{\"ы\": 123}";
+    int x = 0;
+    ASSERT(json_scanf(str, strlen(str), "{ы: %d}", &x) == 1);
+    ASSERT(x == 123);
+  }
+
   return NULL;
   return NULL;
 }
 }