]> git.kaiwu.me - klib.git/commitdiff
simplified the struct
authorHeng Li <lh3@me.com>
Sun, 30 Nov 2014 01:06:53 +0000 (20:06 -0500)
committerHeng Li <lh3@me.com>
Sun, 30 Nov 2014 01:06:53 +0000 (20:06 -0500)
kson.c
kson.h

diff --git a/kson.c b/kson.c
index bdfbf645a1be9f3f7e280454dbb39253a12991a4..c43b07354bfb649b2ad10236b3904e50ab2189b0 100644 (file)
--- a/kson.c
+++ b/kson.c
@@ -14,6 +14,7 @@ kson_node_t *kson_parse_core(const char *json, long *_n, int *error, long *parse
        long *stack = 0, top = 0, max = 0, n_a = 0, m_a = 0, i, j;
        kson_node_t *a = 0, *u;
        const char *p, *q;
+       intptr_t *tmp;
 
 #define __push_back(y) do { \
                if (top == max) { \
@@ -56,9 +57,10 @@ kson_node_t *kson_parse_core(const char *json, long *_n, int *error, long *parse
                        u = &a[stack[start-1]];
                        u->key = u->v.str;
                        u->n = top - 1 - start;
-                       u->v.tmp = (size_t*)malloc(u->n * sizeof(size_t));
+                       u->v.child = (kson_node_t**)malloc(u->n * sizeof(kson_node_t*));
+                       tmp = (intptr_t*)u->v.child;
                        for (i = start + 1; i < top; ++i)
-                               u->v.tmp[i - start - 1] = stack[i];
+                               tmp[i - start - 1] = stack[i];
                        u->type = *p == ']'? KSON_TYPE_BRACKET : KSON_TYPE_BRACE;
                        if ((top = start) == 1) break; // completed one object; remaining characters discarded
                } else if (*p == ':') {
@@ -95,11 +97,9 @@ kson_node_t *kson_parse_core(const char *json, long *_n, int *error, long *parse
        if (parsed_len) *parsed_len = p - json;
        if (top != 1) *error = KSON_ERR_EXTRA_LEFT;
 
-       for (i = 0; i < n_a; ++i) {
-               u = &a[i];
-               for (j = 0; j < (long)u->n; ++j)
-                       u->v.child[j] = &a[u->v.tmp[j]];
-       }
+       for (i = 0; i < n_a; ++i)
+               for (j = 0, u = &a[i], tmp = (intptr_t*)u->v.child; j < (long)u->n; ++j)
+                       u->v.child[j] = &a[tmp[j]];
 
        free(stack);
        *_n = n_a;
diff --git a/kson.h b/kson.h
index d43182f04c635f35f0c2edc7de6ef33411155455..5555bfe86565f204c6acdbbc37226e11003ba2c2 100644 (file)
--- a/kson.h
+++ b/kson.h
@@ -18,8 +18,7 @@ typedef struct kson_node_s {
        uint64_t type:3, n:61;
        char *key;
        union {
-               size_t *tmp; // a temporary pointer used by the parser; don't use this!!!
-               const struct kson_node_s **child;
+               struct kson_node_s **child;
                char *str;
        } v;
 } kson_node_t;