Browse Source

Apply clang-format

Marko Mikulicic 9 years ago
parent
commit
d406553a61
3 changed files with 206 additions and 122 deletions
  1. 122 43
      frozen.c
  2. 15 15
      frozen.h
  3. 69 64
      unit_test.c

+ 122 - 43
frozen.c

@@ -49,8 +49,15 @@ struct frozen {
 static int parse_object(struct frozen *f);
 static int parse_value(struct frozen *f);
 
-#define EXPECT(cond, err_code) do { if (!(cond)) return (err_code); } while (0)
-#define TRY(expr) do { int _n = expr; if (_n < 0) return _n; } while (0)
+#define EXPECT(cond, err_code)      \
+  do {                              \
+    if (!(cond)) return (err_code); \
+  } while (0)
+#define TRY(expr)          \
+  do {                     \
+    int _n = expr;         \
+    if (_n < 0) return _n; \
+  } while (0)
 #define END_OF_STRING (-1)
 
 static int left(const struct frozen *f) {
@@ -67,18 +74,23 @@ static void skip_whitespaces(struct frozen *f) {
 
 static int cur(struct frozen *f) {
   skip_whitespaces(f);
-  return f->cur >= f->end ? END_OF_STRING : * (unsigned char *) f->cur;
+  return f->cur >= f->end ? END_OF_STRING : *(unsigned char *) f->cur;
 }
 
 static int test_and_skip(struct frozen *f, int expected) {
   int ch = cur(f);
-  if (ch == expected) { f->cur++; return 0; }
+  if (ch == expected) {
+    f->cur++;
+    return 0;
+  }
   return ch == END_OF_STRING ? JSON_STRING_INCOMPLETE : JSON_STRING_INVALID;
 }
 
 static int test_no_skip(struct frozen *f, int expected) {
   int ch = cur(f);
-  if (ch == expected) { return 0; }
+  if (ch == expected) {
+    return 0;
+  }
   return ch == END_OF_STRING ? JSON_STRING_INCOMPLETE : JSON_STRING_INVALID;
 }
 
@@ -97,11 +109,19 @@ static int is_hex_digit(int ch) {
 static int get_escape_len(const char *s, int len) {
   switch (*s) {
     case 'u':
-      return len < 6 ? JSON_STRING_INCOMPLETE :
-        is_hex_digit(s[1]) && is_hex_digit(s[2]) &&
-        is_hex_digit(s[3]) && is_hex_digit(s[4]) ? 5 : JSON_STRING_INVALID;
-    case '"': case '\\': case '/': case 'b':
-    case 'f': case 'n': case 'r': case 't':
+      return len < 6 ? JSON_STRING_INCOMPLETE
+                     : is_hex_digit(s[1]) && is_hex_digit(s[2]) &&
+                               is_hex_digit(s[3]) && is_hex_digit(s[4])
+                           ? 5
+                           : JSON_STRING_INVALID;
+    case '"':
+    case '\\':
+    case '/':
+    case 'b':
+    case 'f':
+    case 'n':
+    case 'r':
+    case 't':
       return len < 2 ? JSON_STRING_INCOMPLETE : 1;
     default:
       return JSON_STRING_INVALID;
@@ -147,9 +167,12 @@ static int parse_identifier(struct frozen *f) {
 static int get_utf8_char_len(unsigned char ch) {
   if ((ch & 0x80) == 0) return 1;
   switch (ch & 0xf0) {
-    case 0xf0: return 4;
-    case 0xe0: return 3;
-    default: return 2;
+    case 0xf0:
+      return 4;
+    case 0xe0:
+      return 3;
+    default:
+      return 2;
   }
 }
 
@@ -159,9 +182,9 @@ static int parse_string(struct frozen *f) {
   TRY(test_and_skip(f, '"'));
   TRY(capture_ptr(f, f->cur, JSON_TYPE_STRING));
   for (; f->cur < f->end; f->cur += len) {
-    ch = * (unsigned char *) f->cur;
+    ch = *(unsigned char *) f->cur;
     len = get_utf8_char_len((unsigned char) ch);
-    EXPECT(ch >= 32 && len > 0, JSON_STRING_INVALID);  /* No control chars */
+    EXPECT(ch >= 32 && len > 0, JSON_STRING_INVALID); /* No control chars */
     EXPECT(len < left(f), JSON_STRING_INCOMPLETE);
     if (ch == '\\') {
       EXPECT((n = get_escape_len(f->cur + 1, left(f))) > 0, n);
@@ -241,14 +264,35 @@ static int parse_value(struct frozen *f) {
   int ch = cur(f);
 
   switch (ch) {
-    case '"': TRY(parse_string(f)); break;
-    case '{': TRY(parse_object(f)); break;
-    case '[': TRY(parse_array(f)); break;
-    case 'n': TRY(expect(f, "null", 4, JSON_TYPE_NULL)); break;
-    case 't': TRY(expect(f, "true", 4, JSON_TYPE_TRUE)); break;
-    case 'f': TRY(expect(f, "false", 5, JSON_TYPE_FALSE)); break;
-    case '-': case '0': case '1': case '2': case '3': case '4':
-    case '5': case '6': case '7': case '8': case '9':
+    case '"':
+      TRY(parse_string(f));
+      break;
+    case '{':
+      TRY(parse_object(f));
+      break;
+    case '[':
+      TRY(parse_array(f));
+      break;
+    case 'n':
+      TRY(expect(f, "null", 4, JSON_TYPE_NULL));
+      break;
+    case 't':
+      TRY(expect(f, "true", 4, JSON_TYPE_TRUE));
+      break;
+    case 'f':
+      TRY(expect(f, "false", 5, JSON_TYPE_FALSE));
+      break;
+    case '-':
+    case '0':
+    case '1':
+    case '2':
+    case '3':
+    case '4':
+    case '5':
+    case '6':
+    case '7':
+    case '8':
+    case '9':
       TRY(parse_number(f));
       break;
     default:
@@ -299,18 +343,18 @@ static int parse_object(struct frozen *f) {
 
 static int doit(struct frozen *f) {
   int ret = 0;
-    
+
   if (f->cur == 0 || f->end < f->cur) return JSON_STRING_INVALID;
   if (f->end == f->cur) return JSON_STRING_INCOMPLETE;
 
   if (0 == (ret = test_no_skip(f, '{'))) {
-      TRY(parse_object(f));
+    TRY(parse_object(f));
   } else if (0 == (ret = test_no_skip(f, '['))) {
-      TRY(parse_array(f));
+    TRY(parse_array(f));
   } else {
-      return ret;
+    return ret;
   }
-  
+
   TRY(capture_ptr(f, f->cur, JSON_TYPE_EOF));
   capture_len(f, f->num_tokens, f->cur);
   return 0;
@@ -363,8 +407,9 @@ struct json_token *find_json_token(struct json_token *toks, const char *path) {
         ind += path[n] - '0';
       }
       if (path[n++] != ']') return 0;
-      skip = 1;  /* In objects, we skip 2 elems while iterating, in arrays 1. */
-    } else if (toks->type != JSON_TYPE_OBJECT) return 0;
+      skip = 1; /* In objects, we skip 2 elems while iterating, in arrays 1. */
+    } else if (toks->type != JSON_TYPE_OBJECT)
+      return 0;
     toks++;
     for (i = 0; i < toks[-1].num_desc; i += skip, ind2++) {
       /* ind == -1 indicated that we're iterating an array, not object */
@@ -406,20 +451,46 @@ int json_emit_quoted_str(char *s, int s_len, const char *str, int len) {
   const char *begin = s, *end = s + s_len, *str_end = str + len;
   char ch;
 
-#define EMIT(x) do { if (s < end) *s = x; s++; } while (0)
+#define EMIT(x)          \
+  do {                   \
+    if (s < end) *s = x; \
+    s++;                 \
+  } while (0)
 
   EMIT('"');
   while (str < str_end) {
     ch = *str++;
     switch (ch) {
-      case '"':  EMIT('\\'); EMIT('"'); break;
-      case '\\': EMIT('\\'); EMIT('\\'); break;
-      case '\b': EMIT('\\'); EMIT('b'); break;
-      case '\f': EMIT('\\'); EMIT('f'); break;
-      case '\n': EMIT('\\'); EMIT('n'); break;
-      case '\r': EMIT('\\'); EMIT('r'); break;
-      case '\t': EMIT('\\'); EMIT('t'); break;
-      default: EMIT(ch);
+      case '"':
+        EMIT('\\');
+        EMIT('"');
+        break;
+      case '\\':
+        EMIT('\\');
+        EMIT('\\');
+        break;
+      case '\b':
+        EMIT('\\');
+        EMIT('b');
+        break;
+      case '\f':
+        EMIT('\\');
+        EMIT('f');
+        break;
+      case '\n':
+        EMIT('\\');
+        EMIT('n');
+        break;
+      case '\r':
+        EMIT('\\');
+        EMIT('r');
+        break;
+      case '\t':
+        EMIT('\\');
+        EMIT('t');
+        break;
+      default:
+        EMIT(ch);
     }
   }
   EMIT('"');
@@ -447,18 +518,26 @@ int json_emit_va(char *s, int s_len, const char *fmt, va_list ap) {
 
   while (*fmt != '\0') {
     switch (*fmt) {
-      case '[': case ']': case '{': case '}': case ',': case ':':
-      case ' ': case '\r': case '\n': case '\t':
+      case '[':
+      case ']':
+      case '{':
+      case '}':
+      case ',':
+      case ':':
+      case ' ':
+      case '\r':
+      case '\n':
+      case '\t':
         if (s < end) {
           *s = *fmt;
         }
         s++;
         break;
       case 'i':
-        s += json_emit_long(s, end - s, va_arg(ap, long));
+        s += json_emit_long(s, end - s, va_arg(ap, long) );
         break;
       case 'f':
-        s += json_emit_double(s, end - s, va_arg(ap, double));
+        s += json_emit_double(s, end - s, va_arg(ap, double) );
         break;
       case 'v':
         str = va_arg(ap, char *);

+ 15 - 15
frozen.h

@@ -27,27 +27,27 @@ extern "C" {
 #include <stdarg.h>
 
 enum json_type {
-  JSON_TYPE_EOF     = 0,      /* End of parsed tokens marker */
-  JSON_TYPE_STRING  = 1,
-  JSON_TYPE_NUMBER  = 2,
-  JSON_TYPE_OBJECT  = 3,
-  JSON_TYPE_TRUE    = 4,
-  JSON_TYPE_FALSE   = 5,
-  JSON_TYPE_NULL    = 6,
-  JSON_TYPE_ARRAY   = 7
+  JSON_TYPE_EOF = 0, /* End of parsed tokens marker */
+  JSON_TYPE_STRING = 1,
+  JSON_TYPE_NUMBER = 2,
+  JSON_TYPE_OBJECT = 3,
+  JSON_TYPE_TRUE = 4,
+  JSON_TYPE_FALSE = 5,
+  JSON_TYPE_NULL = 6,
+  JSON_TYPE_ARRAY = 7
 };
 
 struct json_token {
-  const char *ptr;      /* Points to the beginning of the token */
-  int len;              /* Token length */
-  int num_desc;         /* For arrays and object, total number of descendants */
-  enum json_type type;  /* Type of the token, possible values above */
+  const char *ptr;     /* Points to the beginning of the token */
+  int len;             /* Token length */
+  int num_desc;        /* For arrays and object, total number of descendants */
+  enum json_type type; /* Type of the token, possible values above */
 };
 
 /* Error codes */
-#define JSON_STRING_INVALID           -1
-#define JSON_STRING_INCOMPLETE        -2
-#define JSON_TOKEN_ARRAY_TOO_SMALL    -3
+#define JSON_STRING_INVALID -1
+#define JSON_STRING_INCOMPLETE -2
+#define JSON_TOKEN_ARRAY_TOO_SMALL -3
 
 int parse_json(const char *json_string, int json_string_length,
                struct json_token *tokens_array, int size_of_tokens_array);

+ 69 - 64
unit_test.c

@@ -32,91 +32,97 @@
 #include <stdio.h>
 #include <string.h>
 
-#define FAIL(str, line) do {                    \
-  printf("Fail on line %d: [%s]\n", line, str); \
-  return str;                                   \
-} while (0)
-
-#define ASSERT(expr) do {                       \
-  static_num_tests++;                           \
-  if (!(expr)) FAIL(#expr, __LINE__);           \
-} while (0)
+#define FAIL(str, line)                           \
+  do {                                            \
+    printf("Fail on line %d: [%s]\n", line, str); \
+    return str;                                   \
+  } while (0)
+
+#define ASSERT(expr)                    \
+  do {                                  \
+    static_num_tests++;                 \
+    if (!(expr)) FAIL(#expr, __LINE__); \
+  } while (0)
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-#define RUN_TEST(test) do { const char *msg = test(); \
-  if (msg) return msg; } while (0)
+#define RUN_TEST(test)        \
+  do {                        \
+    const char *msg = test(); \
+    if (msg) return msg;      \
+  } while (0)
 
 static int static_num_tests = 0;
 
-static int cmp_token(const struct json_token *tok, const char *str, enum json_type type) {
+static int cmp_token(const struct json_token *tok, const char *str,
+                     enum json_type type) {
 #if 0
   printf("[%.*s] [%s]\n", tok->len, tok->ptr, str);
 #endif
   return tok->type == type && (int) strlen(str) == tok->len &&
-    memcmp(tok->ptr, str, tok->len) == 0;
+         memcmp(tok->ptr, str, tok->len) == 0;
 }
 
 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\"}", "{a:1x}", "{a:1e}",
-    "{a:.1}", "{a:0.}", "{a:0.e}", "{a:0.e1}", "{a:0.1e}", "{a:\"\\u\" } ",
-    "{a:\"\\yx\"}", "{a:\"\\u111r\"}",
-    NULL
-  };
+      "1", "a:3", "\x01", "{:", " { 1", "{a:\"\n\"}", "{a:1x}", "{a:1e}",
+      "{a:.1}", "{a:0.}", "{a:0.e}", "{a:0.e1}", "{a:0.1e}", "{a:\"\\u\" } ",
+      "{a:\"\\yx\"}", "{a:\"\\u111r\"}", NULL};
   static const char *incomplete_tests[] = {
-    "", " \r\n\t", "{", " { a", "{a:", "{a:\"", " { a : \"xx", "{a:12",
-    "{a:\"\\uf", "{a:\"\\uff", "{a:\"\\ufff", "{a:\"\\uffff",
-    "{a:\"\\uffff\"", "{a:\"\\uffff\" ,", "{a:n", "{a:nu", "{a:nul", "{a:null",
-    NULL
-  };
-  static const struct { const char *str; int expected_len; } success_tests[] = {
-    { "{}", 2 },
-    /* 2, 3, 4 byte utf-8 chars */
-    { "{a:\"\xd0\xb1\xe3\x81\xaf\xf0\xa2\xb3\x82\"}", 15 },
-    { "{a:\"\\u0006\"}", 12 },
-    { " { } ", 4 },
-    { "{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 },
-    { " {a:[]} 123456", 7 },
-    { " {a:[1,2]} 123456", 10 },
-    { "{a:1,b:2} xxxx", 9 },
-    { "{a:1,b:{},c:[{}]} xxxx", 17 },
-    { "{a:true,b:[false,null]} xxxx", 23 },
-    { "[1.23, 3, 5]", 12 },
-    { "[13, {\"a\":\"hi there\"}, 5]", 25 },
-    { NULL, 0 }
-  };
-  const char *s1 = " { a: 1, b: \"hi there\", c: true, d: false, "
-    " e : null, f: [ 1, -2, 3], g: { \"1\": [], h: [ 7 ] } } ";
-  const char *s2 = "{ a: 1, b: \"hi there\", c: true, d: false, "
-    " e : null, f: [ 1, -2, 3], g: { \"1\": [], h: [ 7 ] } }";
+      "", " \r\n\t", "{", " { a", "{a:", "{a:\"", " { a : \"xx", "{a:12",
+      "{a:\"\\uf", "{a:\"\\uff", "{a:\"\\ufff", "{a:\"\\uffff",
+      "{a:\"\\uffff\"", "{a:\"\\uffff\" ,", "{a:n", "{a:nu", "{a:nul",
+      "{a:null", NULL};
+  static const struct {
+    const char *str;
+    int expected_len;
+  } success_tests[] = {{"{}", 2},
+                       /* 2, 3, 4 byte utf-8 chars */
+                       {"{a:\"\xd0\xb1\xe3\x81\xaf\xf0\xa2\xb3\x82\"}", 15},
+                       {"{a:\"\\u0006\"}", 12},
+                       {" { } ", 4},
+                       {"{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},
+                       {" {a:[]} 123456", 7},
+                       {" {a:[1,2]} 123456", 10},
+                       {"{a:1,b:2} xxxx", 9},
+                       {"{a:1,b:{},c:[{}]} xxxx", 17},
+                       {"{a:true,b:[false,null]} xxxx", 23},
+                       {"[1.23, 3, 5]", 12},
+                       {"[13, {\"a\":\"hi there\"}, 5]", 25},
+                       {NULL, 0}};
+  const char *s1 =
+      " { a: 1, b: \"hi there\", c: true, d: false, "
+      " e : null, f: [ 1, -2, 3], g: { \"1\": [], h: [ 7 ] } } ";
+  const char *s2 =
+      "{ a: 1, b: \"hi there\", c: true, d: false, "
+      " e : null, f: [ 1, -2, 3], g: { \"1\": [], h: [ 7 ] } }";
   const char *s3 = "{ \"1\": [], h: [ 7 ] }";
   int i;
 
   ASSERT(parse_json(NULL, 0, NULL, 0) == JSON_STRING_INVALID);
   for (i = 0; invalid_tests[i] != NULL; i++) {
-    ASSERT(parse_json(invalid_tests[i], strlen(invalid_tests[i]),
-                      ar, size) == JSON_STRING_INVALID);
+    ASSERT(parse_json(invalid_tests[i], strlen(invalid_tests[i]), ar, size) ==
+           JSON_STRING_INVALID);
   }
 
   for (i = 0; incomplete_tests[i] != NULL; i++) {
-    ASSERT(parse_json(incomplete_tests[i], strlen(incomplete_tests[i]),
-                      ar, size) == JSON_STRING_INCOMPLETE);
+    ASSERT(parse_json(incomplete_tests[i], strlen(incomplete_tests[i]), ar,
+                      size) == JSON_STRING_INCOMPLETE);
   }
 
   for (i = 0; success_tests[i].str != NULL; i++) {
-    ASSERT(parse_json(success_tests[i].str, strlen(success_tests[i].str),
-                      ar, size) == success_tests[i].expected_len);
+    ASSERT(parse_json(success_tests[i].str, strlen(success_tests[i].str), ar,
+                      size) == success_tests[i].expected_len);
   }
 
   ASSERT(parse_json("{}", 2, ar, 1) == JSON_TOKEN_ARRAY_TOO_SMALL);
@@ -223,8 +229,8 @@ static const char *test_emit(void) {
   ASSERT(strcmp(buf, s5) == 0);
   ASSERT(p < &buf[sizeof(buf)]);
 
-  ASSERT(json_emit(buf, sizeof(buf), "{v:[i,f,V]}",
-         "foo", 3, (long) -123, 1.23, "true", 4) > 0);
+  ASSERT(json_emit(buf, sizeof(buf), "{v:[i,f,V]}", "foo", 3, (long) -123, 1.23,
+                   "true", 4) > 0);
   ASSERT(strcmp(buf, s5) == 0);
 
   ASSERT(json_emit(buf, 4, "{S:i}", "a", 12345) > 4);
@@ -240,10 +246,9 @@ static const char *test_nested(void) {
   struct json_token ar[100];
   const char *s = "{ a : [ [1, 2, { b : 2 } ] ] }";
   enum json_type types[] = {
-    JSON_TYPE_OBJECT, JSON_TYPE_STRING, JSON_TYPE_ARRAY, JSON_TYPE_ARRAY,
-    JSON_TYPE_NUMBER, JSON_TYPE_NUMBER, JSON_TYPE_OBJECT, JSON_TYPE_STRING,
-    JSON_TYPE_NUMBER, JSON_TYPE_EOF
-  };
+      JSON_TYPE_OBJECT, JSON_TYPE_STRING, JSON_TYPE_ARRAY,  JSON_TYPE_ARRAY,
+      JSON_TYPE_NUMBER, JSON_TYPE_NUMBER, JSON_TYPE_OBJECT, JSON_TYPE_STRING,
+      JSON_TYPE_NUMBER, JSON_TYPE_EOF};
   int i, ar_size = ARRAY_SIZE(ar), types_size = ARRAY_SIZE(types);
 
   ASSERT(parse_json(s, strlen(s), ar, ar_size) == (int) strlen(s));