From: Heng Li Date: Sun, 30 Nov 2014 01:00:28 +0000 (-0500) Subject: simplified the struct a little X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=bae79deade3f63dbab161cabfc12167254021020;p=klib.git simplified the struct a little --- diff --git a/kson.c b/kson.c index 33d1e6b..bdfbf64 100644 --- a/kson.c +++ b/kson.c @@ -56,9 +56,9 @@ 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.child = (kson_ptr_t*)malloc(u->n * sizeof(kson_ptr_t)); + u->v.tmp = (size_t*)malloc(u->n * sizeof(size_t)); for (i = start + 1; i < top; ++i) - u->v.child[i - start - 1].i = stack[i]; + u->v.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 == ':') { @@ -98,7 +98,7 @@ kson_node_t *kson_parse_core(const char *json, long *_n, int *error, long *parse for (i = 0; i < n_a; ++i) { u = &a[i]; for (j = 0; j < (long)u->n; ++j) - u->v.child[j].p = &a[u->v.child[j].i]; + u->v.child[j] = &a[u->v.tmp[j]]; } free(stack); @@ -169,7 +169,7 @@ void kson_format_recur(const kson_node_t *p, int depth) putchar(','); putchar('\n'); for (i = 0; i <= depth; ++i) fputs(" ", stdout); } - kson_format_recur(p->v.child[i].p, depth + 1); + kson_format_recur(p->v.child[i], depth + 1); } putchar('\n'); for (i = 0; i < depth; ++i) fputs(" ", stdout); } @@ -219,7 +219,7 @@ int main(int argc, char *argv[]) kson = kson_parse(json, &error); free(json); if (kson) { - kson_format(kson); + kson_format(kson->nodes); if (argc > 2) { // path finding const kson_node_t *p = kson->nodes; @@ -243,7 +243,7 @@ int main(int argc, char *argv[]) const kson_node_t *p = kson_query(kson->nodes, 2, "b", 1); if (p) printf("*** %s\n", p->v.str); else printf("!!! not found\n"); - kson_format(kson); + kson_format(kson->nodes); } else { printf("Error code: %d\n", error); } diff --git a/kson.h b/kson.h index d8b7e38..d43182f 100644 --- a/kson.h +++ b/kson.h @@ -14,18 +14,12 @@ #define KSON_ERR_EXTRA_RIGHT 2 #define KSON_ERR_NO_KEY 3 -struct kson_node_s; - -typedef union { - long i; // this is a temporary variable; don't use it! - struct kson_node_s *p; -} kson_ptr_t; - typedef struct kson_node_s { uint64_t type:3, n:61; char *key; union { - kson_ptr_t *child; + size_t *tmp; // a temporary pointer used by the parser; don't use this!!! + const struct kson_node_s **child; char *str; } v; } kson_node_t; @@ -66,7 +60,7 @@ static inline const kson_node_t *kson_by_key(const kson_node_t *p, const char *k { long i; for (i = 0; i < (long)p->n; ++i) { - const kson_node_t *q = p->v.child[i].p; + const kson_node_t *q = p->v.child[i]; if (q->key && strcmp(q->key, key) == 0) return q; } @@ -75,7 +69,7 @@ static inline const kson_node_t *kson_by_key(const kson_node_t *p, const char *k static inline const kson_node_t *kson_by_index(const kson_node_t *p, long i) { - return 0 <= i && i < (long)p->n? p->v.child[i].p : 0; + return 0 <= i && i < (long)p->n? p->v.child[i] : 0; } #endif