Browse Source

frozen: return -1 when array element is missing

frozen: add unit test

PUBLISHED_FROM=100a75b9a8fb0801441b518d8e056b05f75b388c
Serge A. Zaitsev 7 years ago
parent
commit
09e024c9ea
3 changed files with 14 additions and 2 deletions
  1. 4 1
      frozen.c
  2. 1 1
      frozen.h
  3. 9 0
      unit_test.c

+ 4 - 1
frozen.c

@@ -801,6 +801,7 @@ int json_walk(const char *json_string, int json_string_length,
 }
 }
 
 
 struct scan_array_info {
 struct scan_array_info {
+  int found;
   char path[JSON_MAX_PATH_LEN];
   char path[JSON_MAX_PATH_LEN];
   struct json_token *token;
   struct json_token *token;
 };
 };
@@ -815,6 +816,7 @@ static void json_scanf_array_elem_cb(void *callback_data, const char *name,
 
 
   if (strcmp(path, info->path) == 0) {
   if (strcmp(path, info->path) == 0) {
     *info->token = *token;
     *info->token = *token;
+    info->found = 1;
   }
   }
 }
 }
 
 
@@ -824,10 +826,11 @@ int json_scanf_array_elem(const char *s, int len, const char *path, int idx,
                           struct json_token *token) {
                           struct json_token *token) {
   struct scan_array_info info;
   struct scan_array_info info;
   info.token = token;
   info.token = token;
+  info.found = 0;
   memset(token, 0, sizeof(*token));
   memset(token, 0, sizeof(*token));
   snprintf(info.path, sizeof(info.path), "%s[%d]", path, idx);
   snprintf(info.path, sizeof(info.path), "%s[%d]", path, idx);
   json_walk(s, len, json_scanf_array_elem_cb, &info);
   json_walk(s, len, json_scanf_array_elem_cb, &info);
-  return token->len;
+  return info.found ? token->len : -1;
 }
 }
 
 
 struct json_scanf_info {
 struct json_scanf_info {

+ 1 - 1
frozen.h

@@ -209,7 +209,7 @@ typedef void (*json_scanner_t)(const char *str, int len, void *user_data);
 /*
 /*
  * Helper function to scan array item with given path and index.
  * Helper function to scan array item with given path and index.
  * Fills `token` with the matched JSON token.
  * Fills `token` with the matched JSON token.
- * Return 0 if no array element found, otherwise non-0.
+ * Return -1 if no array element found, otherwise non-negative token length.
  */
  */
 int json_scanf_array_elem(const char *s, int len, const char *path, int index,
 int json_scanf_array_elem(const char *s, int len, const char *path, int index,
                           struct json_token *token);
                           struct json_token *token);

+ 9 - 0
unit_test.c

@@ -552,6 +552,15 @@ static const char *test_scanf(void) {
     ASSERT(i == 2);
     ASSERT(i == 2);
   }
   }
 
 
+  {
+    const char *str = "{a : [\"foo\", \"\", \"a\"] }";
+    struct json_token t;
+    ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 0, &t) == 3);
+    ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 1, &t) == 0);
+    ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 2, &t) == 1);
+    ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 3, &t) == -1);
+  }
+
   {
   {
     const char *str = "{a : \"foo\\b\\f\\n\\r\\t\\\\\" }";
     const char *str = "{a : \"foo\\b\\f\\n\\r\\t\\\\\" }";
     char *result;
     char *result;