jq: parser.tab.c lexer.yy.c main.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c
$(CC) -DJQ_DEBUG=0 -o $@ $^
-jv_test: jv_test.c jv.c jv_print.c jv_dtoa.c
+jv_test: jv_test.c jv.c jv_print.c jv_dtoa.c jv_unicode.c
$(CC) -DNO_JANSSON -o $@ $^
jv_parse: jv_parse.c jv.c jv_print.c jv_dtoa.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <stdarg.h>
#include "jv.h"
return jv_invalid_with_msg(jv_null());
}
-jv jv_invalid_get_message(jv inv) {
- jv x = ((jvp_invalid*)inv.val.complex.ptr)->errmsg;
+jv jv_invalid_get_msg(jv inv) {
+ jv x = jv_copy(((jvp_invalid*)inv.val.complex.ptr)->errmsg);
jv_free(inv);
return x;
}
return jvp_string_ptr(&j.val.complex)->data;
}
+jv jv_string_fmt(const char* fmt, ...) {
+ int size = 1024;
+ while (1) {
+ char* buf = malloc(size);
+ va_list args;
+ va_start(args, fmt);
+ int n = vsnprintf(buf, size, fmt, args);
+ va_end(args);
+ if (n < size) {
+ jv ret = jv_string_sized(buf, n);
+ free(buf);
+ return ret;
+ } else {
+ free(buf);
+ size = n * 2;
+ }
+ }
+}
+
/*
* Objects (internal helpers)
*/
int jv_string_length(jv);
uint32_t jv_string_hash(jv);
const char* jv_string_value(jv);
+jv jv_string_fmt(const char*, ...);
jv jv_object();
jv jv_object_get(jv object, jv key);
jv_free(a1);
jv_free(a2);
jv_free(b);
+
+ assert(jv_equal(jv_string("hello42!"), jv_string_fmt("hello%d%s", 42, "!")));
+ char big[20000];
+ for (int i=0; i<sizeof(big); i++) big[i] = 'a';
+ big[sizeof(big)-1] = 0;
+ jv str = jv_string_fmt("%s", big);
+ assert(jv_string_length(jv_copy(str)) == sizeof(big) - 1);
+ assert(!strcmp(big, jv_string_value(str)));
+ jv_free(str);
}
/// Objects