Sergey Lyubka 11 rokov pred
rodič
commit
724a676d81
2 zmenil súbory, kde vykonal 24 pridanie a 3 odobranie
  1. 16 1
      frozen.c
  2. 8 2
      unit_test.c

+ 16 - 1
frozen.c

@@ -131,12 +131,27 @@ static int parse_string(struct frozen *f) {
 }
 
 // number = [ '-' ] digit { digit }
-// TODO(lsm): add support for floats
 static int parse_number(struct frozen *f) {
   int ch = cur(f);
   TRY(capture_ptr(f, f->cur, JSON_TYPE_NUMBER));
   if (ch == '-') f->cur++;
+  EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE);
+  EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID);
   while (f->cur < f->end && is_digit(f->cur[0])) f->cur++;
+  if (f->cur < f->end && f->cur[0] == '.') {
+    f->cur++;
+    EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE);
+    EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID);
+    while (f->cur < f->end && is_digit(f->cur[0])) f->cur++;
+  }
+  if (f->cur < f->end && (f->cur[0] == 'e' || f->cur[0] == 'E')) {
+    f->cur++;
+    EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE);
+    if ((f->cur[0] == '+' || f->cur[0] == '-')) f->cur++;
+    EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE);
+    EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID);
+    while (f->cur < f->end && is_digit(f->cur[0])) f->cur++;
+  }
   capture_len(f, f->num_tokens - 1, f->cur);
   return 0;
 }

+ 8 - 2
unit_test.c

@@ -49,7 +49,8 @@ static const char *test_errors(void) {
   struct json_token ar[100];
   int size = ARRAY_SIZE(ar);
   static const char *invalid_tests[] = {
-    "1", "a:3", "\x01", "{:", " { 1", "{a:\"\n\"}",
+    "1", "a:3", "\x01", "{:", " { 1", "{a:\"\n\"}", "{a:1x}", "{a:1e}",
+    "{a:.1}", "{a:0.}", "{a:0.e}", "{a:0.e1}", "{a:0.1e}",
     NULL
   };
   static const char *incomplete_tests[] = {
@@ -60,7 +61,12 @@ static const char *test_errors(void) {
     { "{}", 2 },
     { " { } ", 4 },
     { "{a:1}", 5 },
-    { "{a:1}", 5 },
+    { "{a:1.23}", 8 },
+    { "{a:1e23}", 8 },
+    { "{a:1.23e2}", 10 },
+    { "{a:-123}", 8 },
+    { "{a:-1.3}", 8 },
+    { "{a:-1.3e-2}", 11},
     { "{a:\"\"}", 6 },
     { "{a:\" \\n\\t\\r\"}", 13 },
     { " {a:[1]} 123456", 8 },