summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Dolan <mu@netsoc.tcd.ie>2012-09-17 20:46:13 +0100
committerStephen Dolan <mu@netsoc.tcd.ie>2012-09-17 20:49:41 +0100
commitfe33150b7f2950b90d710937ecb72522ca202dca (patch)
treeb2301658b0c74588cc16df5540f50246ca1b3d91
parent3db27b01a1ee58180e1c97592ceac24d0e434f0a (diff)
parent326771f4b4ee1039f5ab8a1eaf0662107949b169 (diff)
Merge branch 'master' of github:stedolan/jq
Conflicts: c/builtin.c
-rw-r--r--c/builtin.c11
-rw-r--r--c/jv.c58
-rw-r--r--c/jv.h5
-rw-r--r--c/jv_print.c146
-rw-r--r--c/main.c5
-rw-r--r--c/testdata4
6 files changed, 163 insertions, 66 deletions
diff --git a/c/builtin.c b/c/builtin.c
index 9be18208..e8c51cd7 100644
--- a/c/builtin.c
+++ b/c/builtin.c
@@ -143,7 +143,15 @@ static void f_length(jv input[], jv output[]) {
}
}
-static struct cfunction function_list[] = {
+static void f_tostring(jv input[], jv output[]) {
+ if (jv_get_kind(input[0]) == JV_KIND_STRING) {
+ output[0] = input[0];
+ } else {
+ output[0] = jv_dump_string(input[0], 0);
+ }
+}
+
+struct cfunction function_list[] = {
{f_true, "true", CALL_BUILTIN_1_1},
{f_false, "false", CALL_BUILTIN_1_1},
{f_null, "null", CALL_BUILTIN_1_1},
@@ -152,6 +160,7 @@ static struct cfunction function_list[] = {
{f_multiply, "_multiply", CALL_BUILTIN_3_1},
{f_divide, "_divide", CALL_BUILTIN_3_1},
{f_tonumber, "tonumber", CALL_BUILTIN_1_1},
+ {f_tostring, "tostring", CALL_BUILTIN_1_1},
{f_equal, "_equal", CALL_BUILTIN_3_1},
{f_length, "length", CALL_BUILTIN_1_1},
};
diff --git a/c/jv.c b/c/jv.c
index 786e7b26..51070636 100644
--- a/c/jv.c
+++ b/c/jv.c
@@ -340,6 +340,7 @@ typedef struct {
// high 31 bits are length, low bit is a flag
// indicating whether hash has been computed.
uint32_t length_hashed;
+ uint32_t alloc_length;
char data[];
} jvp_string;
@@ -350,19 +351,19 @@ static jvp_string* jvp_string_ptr(jv_complex* a) {
static jvp_string* jvp_string_alloc(uint32_t size) {
jvp_string* s = malloc(sizeof(jvp_string) + size + 1);
s->refcnt.count = 1;
- s->length_hashed = size << 1;
+ s->alloc_length = size;
return s;
}
static jv_complex jvp_string_new(const char* data, uint32_t length) {
jvp_string* s = jvp_string_alloc(length);
+ s->length_hashed = length << 1;
memcpy(s->data, data, length);
s->data[length] = 0;
jv_complex r = {&s->refcnt, {0,0}};
return r;
}
-
static void jvp_string_free(jv_complex* s) {
if (jvp_refcnt_dec(s)) {
jvp_string* str = jvp_string_ptr(s);
@@ -385,16 +386,37 @@ static uint32_t jvp_string_length(jvp_string* s) {
return s->length_hashed >> 1;
}
-static jv_complex jvp_string_concat(jvp_string* a, jvp_string* b) {
- uint32_t la = jvp_string_length(a), lb = jvp_string_length(b);
- jvp_string* s = jvp_string_alloc(la + lb);
- memcpy(s->data, a->data, la);
- memcpy(s->data + la, b->data, lb);
- s->data[la + lb] = 0;
- jv_complex r = {&s->refcnt, {0,0}};
+static uint32_t jvp_string_remaining_space(jvp_string* s) {
+ uint32_t r = s->alloc_length - jvp_string_length(s);
+ assert(r >= 0);
return r;
}
+static void jvp_string_append(jv_complex* string, const char* data, uint32_t len) {
+ jvp_string* s = jvp_string_ptr(string);
+ uint32_t currlen = jvp_string_length(s);
+
+ if (jvp_refcnt_unshared(string) &&
+ jvp_string_remaining_space(s) >= len) {
+ // the next string fits at the end of a
+ memcpy(s->data + currlen, data, len);
+ s->data[currlen + len] = 0;
+ s->length_hashed = (currlen + len) << 1;
+ } else {
+ // allocate a bigger buffer and copy
+ uint32_t allocsz = (currlen + len) * 2;
+ if (allocsz < 32) allocsz = 32;
+ jvp_string* news = jvp_string_alloc(allocsz);
+ news->length_hashed = (currlen + len) << 1;
+ memcpy(news->data, s->data, currlen);
+ memcpy(news->data + currlen, data, len);
+ news->data[currlen + len] = 0;
+ jvp_string_free(string);
+ jv_complex r = {&news->refcnt, {0,0}};
+ *string = r;
+ }
+}
+
static const uint32_t HASH_SEED = 0x432A9843;
static uint32_t rotl32 (uint32_t x, int8_t r){
@@ -507,15 +529,21 @@ const char* jv_string_value(jv j) {
}
jv jv_string_concat(jv a, jv b) {
- jv j;
- j.kind = JV_KIND_STRING;
- j.val.complex = jvp_string_concat(jvp_string_ptr(&a.val.complex),
- jvp_string_ptr(&b.val.complex));
- jv_free(a);
+ jvp_string* sb = jvp_string_ptr(&b.val.complex);
+ jvp_string_append(&a.val.complex, sb->data, jvp_string_length(sb));
jv_free(b);
- return j;
+ return a;
}
+jv jv_string_append_buf(jv a, const char* buf, int len) {
+ jvp_string_append(&a.val.complex, buf, len);
+ return a;
+}
+
+jv jv_string_append_str(jv a, const char* str) {
+ return jv_string_append_buf(a, str, strlen(str));
+}
+
jv jv_string_fmt(const char* fmt, ...) {
int size = 1024;
while (1) {
diff --git a/c/jv.h b/c/jv.h
index ec23262c..e67d614a 100644
--- a/c/jv.h
+++ b/c/jv.h
@@ -80,6 +80,8 @@ uint32_t jv_string_hash(jv);
const char* jv_string_value(jv);
jv jv_string_concat(jv, jv);
jv jv_string_fmt(const char*, ...);
+jv jv_string_append_buf(jv a, const char* buf, int len);
+jv jv_string_append_str(jv a, const char* str);
jv jv_object();
jv jv_object_get(jv object, jv key);
@@ -103,7 +105,8 @@ jv jv_object_iter_value(jv, int);
int jv_get_refcnt(jv);
enum { JV_PRINT_PRETTY = 1, JV_PRINT_ASCII = 2 };
-void jv_dump(jv, int);
+void jv_dump(jv, int flags);
+jv jv_dump_string(jv, int flags);
jv jv_parse(const char* string);
jv jv_parse_sized(const char* string, int length);
diff --git a/c/jv_print.c b/c/jv_print.c
index f1ec92fb..64bf178a 100644
--- a/c/jv_print.c
+++ b/c/jv_print.c
@@ -1,47 +1,70 @@
#include "jv.h"
#include <stdio.h>
#include <float.h>
-#include <math.h>
+#include <string.h>
#include "jv_dtoa.h"
#include "jv_unicode.h"
-static void jv_dump_string(jv str, int ascii_only) {
+static void put_buf(const char* s, int len, FILE* fout, jv* strout) {
+ if (strout) {
+ *strout = jv_string_append_buf(*strout, s, len);
+ } else {
+ fwrite(s, 1, len, fout);
+ }
+}
+
+static void put_char(char c, FILE* fout, jv* strout) {
+ put_buf(&c, 1, fout, strout);
+}
+
+static void put_str(const char* s, FILE* fout, jv* strout) {
+ put_buf(s, strlen(s), fout, strout);
+}
+
+static void put_space(int n, FILE* fout, jv* strout) {
+ while (n--) {
+ put_char(' ', fout, strout);
+ }
+}
+
+static void jvp_dump_string(jv str, int ascii_only, FILE* F, jv* S) {
assert(jv_get_kind(str) == JV_KIND_STRING);
const char* i = jv_string_value(str);
const char* end = i + jv_string_length(jv_copy(str));
int c = 0;
+ char buf[32];
while ((i = jvp_utf8_next(i, end, &c))) {
assert(c != -1);
int unicode_escape = 0;
if (0x20 <= c && c <= 0x7E) {
// printable ASCII
if (c == '"' || c == '\\') {
- putchar('\\');
+ put_char('\\', F, S);
}
- putchar(c);
+ put_char(c, F, S);
} else if (c < 0x20 || c == 0x7F) {
// ASCII control character
switch (c) {
case '\b':
- putchar('\\');
- putchar('b');
+ put_char('\\', F, S);
+ put_char('b', F, S);
break;
case '\t':
- putchar('\\');
- putchar('t');
+ put_char('\\', F, S);
+ put_char('t', F, S);
break;
case '\r':
- putchar('\\');
- putchar('r');
+ put_char('\\', F, S);
+ put_char('r', F, S);
break;
case '\n':
- putchar('\\');
- putchar('n');
+ put_char('\\', F, S);
+ put_char('n', F, S);
break;
case '\f':
- putchar('\\');
- putchar('f');
+ put_char('\\', F, S);
+ put_char('f', F, S);
break;
default:
unicode_escape = 1;
@@ -52,13 +75,14 @@ static void jv_dump_string(jv str, int ascii_only) {
}
if (unicode_escape) {
if (c <= 0xffff) {
- printf("\\u%04x", c);
+ sprintf(buf, "\\u%04x", c);
} else {
c -= 0x10000;
- printf("\\u%04x\\u%04x",
- 0xD800 | ((c & 0xffc00) >> 10),
- 0xDC00 | (c & 0x003ff));
+ sprintf(buf, "\\u%04x\\u%04x",
+ 0xD800 | ((c & 0xffc00) >> 10),
+ 0xDC00 | (c & 0x003ff));
}
+ put_str(buf, F, S);
}
}
assert(c != -1);
@@ -66,78 +90,97 @@ static void jv_dump_string(jv str, int ascii_only) {
enum { INDENT = 2 };
-static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent) {
+static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FILE* F, jv* S) {
char buf[JVP_DTOA_FMT_MAX_LEN];
switch (jv_get_kind(x)) {
case JV_KIND_INVALID:
assert(0 && "Invalid value");
break;
case JV_KIND_NULL:
- printf("null");
+ put_str("null", F, S);
break;
case JV_KIND_FALSE:
- printf("false");
+ put_str("false", F, S);
break;
case JV_KIND_TRUE:
- printf("true");
+ put_str("true", F, S);
break;
case JV_KIND_NUMBER: {
double d = jv_number_value(x);
if (d != d) {
// JSON doesn't have NaN, so we'll render it as "null"
- printf("null");
+ put_str("null", F, S);
} else {
// Normalise infinities to something we can print in valid JSON
if (d > DBL_MAX) d = DBL_MAX;
if (d < -DBL_MAX) d = -DBL_MAX;
- printf("%s", jvp_dtoa_fmt(C, buf, d));
+ put_str(jvp_dtoa_fmt(C, buf, d), F, S);
}
break;
}
case JV_KIND_STRING:
- // FIXME: all sorts of broken
- putchar('"');
- jv_dump_string(x, 0);
- putchar('"');
+ put_char('"', F, S);
+ jvp_dump_string(x, 0, F, S);
+ put_char('"', F, S);
break;
case JV_KIND_ARRAY: {
if (jv_array_length(jv_copy(x)) == 0) {
- printf("[]");
+ put_str("[]", F, S);
break;
}
- printf("[");
- if (flags & JV_PRINT_PRETTY) printf("\n%*s", indent+INDENT, "");
+ put_str("[", F, S);
+ if (flags & JV_PRINT_PRETTY) {
+ put_char('\n', F, S);
+ put_space(indent+INDENT, F, S);
+ }
for (int i=0; i<jv_array_length(jv_copy(x)); i++) {
if (i!=0) {
- if (flags & JV_PRINT_PRETTY) printf(",\n%*s", indent+INDENT, "");
- else printf(", ");
+ if (flags & JV_PRINT_PRETTY) {
+ put_str(",\n", F, S);
+ put_space(indent+INDENT, F, S);
+ } else {
+ put_str(", ", F, S);
+ }
}
- jv_dump_term(C, jv_array_get(jv_copy(x), i), flags, indent + INDENT);
+ jv_dump_term(C, jv_array_get(jv_copy(x), i), flags, indent + INDENT, F, S);
+ }
+ if (flags & JV_PRINT_PRETTY) {
+ put_char('\n', F, S);
+ put_space(indent, F, S);
}
- if (flags & JV_PRINT_PRETTY) printf("\n%*s", indent, "");
- printf("]");
+ put_char(']', F, S);
break;
}
case JV_KIND_OBJECT: {
if (jv_object_length(jv_copy(x)) == 0) {
- printf("{}");
+ put_str("{}", F, S);
break;
}
- printf("{");
- if (flags & JV_PRINT_PRETTY) printf("\n%*s", indent+INDENT, "");
+ put_char('{', F, S);
+ if (flags & JV_PRINT_PRETTY) {
+ put_char('\n', F, S);
+ put_space(indent+INDENT, F, S);
+ }
int first = 1;
jv_object_foreach(i, x) {
if (!first) {
- if (flags & JV_PRINT_PRETTY) printf(",\n%*s", indent+INDENT, "");
- else printf(", ");
+ if (flags & JV_PRINT_PRETTY){
+ put_str(",\n", F, S);
+ put_space(indent+INDENT, F, S);
+ } else {
+ put_str(", ", F, S);
+ }
}
first = 0;
- jv_dump_term(C, jv_object_iter_key(x, i), flags, indent + INDENT);
- printf(": ");
- jv_dump_term(C, jv_object_iter_value(x, i), flags, indent + INDENT);
+ jv_dump_term(C, jv_object_iter_key(x, i), flags, indent + INDENT, F, S);
+ put_str(": ", F, S);
+ jv_dump_term(C, jv_object_iter_value(x, i), flags, indent + INDENT, F, S);
}
- if (flags & JV_PRINT_PRETTY) printf("\n%*s", indent, "");
- printf("}");
+ if (flags & JV_PRINT_PRETTY) {
+ put_char('\n', F, S);
+ put_space(indent, F, S);
+ }
+ put_char('}', F, S);
}
}
jv_free(x);
@@ -146,6 +189,15 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent) {
void jv_dump(jv x, int flags) {
struct dtoa_context C;
jvp_dtoa_context_init(&C);
- jv_dump_term(&C, x, flags, 0);
+ jv_dump_term(&C, x, flags, 0, stdout, 0);
+ jvp_dtoa_context_free(&C);
+}
+
+jv jv_dump_string(jv x, int flags) {
+ struct dtoa_context C;
+ jvp_dtoa_context_init(&C);
+ jv s = jv_string("");
+ jv_dump_term(&C, x, flags, 0, 0, &s);
jvp_dtoa_context_free(&C);
+ return s;
}
diff --git a/c/main.c b/c/main.c
index b65d820f..c9188b3e 100644
--- a/c/main.c
+++ b/c/main.c
@@ -77,6 +77,11 @@ void run_tests() {
printf("\n");
pass = 0;
}
+ jv as_string = jv_dump_string(jv_copy(expected), rand());
+ jv reparsed = jv_parse_sized(jv_string_value(as_string), jv_string_length(jv_copy(as_string)));
+ assert(jv_equal(jv_copy(expected), jv_copy(reparsed)));
+ jv_free(as_string);
+ jv_free(reparsed);
jv_free(expected);
jv_free(actual);
}
diff --git a/c/testdata b/c/testdata
index f2eb32a2..8e66a02a 100644
--- a/c/testdata
+++ b/c/testdata
@@ -159,9 +159,9 @@ null
"asdfasdf"
{"a":1, "b":2, "c":3}
-"asdf" + "jkl;" + .
+"asdf" + "jkl;" + . + . + .
"some string"
-"asdfjkl;some string"
+"asdfjkl;some stringsome stringsome string"
"\u0000\u0020\u0000" + .
"\u0000\u0020\u0000"