jv_string_fmt (create printf-formatted JSON strings)
This commit is contained in:
1 parent
0159c0ecc5
commit
a3e509034b
4 files changed
+33
-3
No files matched your search
+1
-1
@@ -27,7 +27,7 @@ parsertest: parser.tab.c lexer.yy.c main.c opcode.c bytecode.c compile.c execute
|
|||||||
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
|
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 $@ $^
|
$(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 $@ $^
|
$(CC) -DNO_JANSSON -o $@ $^
|
||||||
|
|
||||||
jv_parse: jv_parse.c jv.c jv_print.c jv_dtoa.c
|
jv_parse: jv_parse.c jv.c jv_print.c jv_dtoa.c
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
#include "jv.h"
|
#include "jv.h"
|
||||||
|
|
||||||
@@ -81,8 +82,8 @@ jv jv_invalid() {
|
|||||||
return jv_invalid_with_msg(jv_null());
|
return jv_invalid_with_msg(jv_null());
|
||||||
}
|
}
|
||||||
|
|
||||||
jv jv_invalid_get_message(jv inv) {
|
jv jv_invalid_get_msg(jv inv) {
|
||||||
jv x = ((jvp_invalid*)inv.val.complex.ptr)->errmsg;
|
jv x = jv_copy(((jvp_invalid*)inv.val.complex.ptr)->errmsg);
|
||||||
jv_free(inv);
|
jv_free(inv);
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
@@ -472,6 +473,25 @@ const char* jv_string_value(jv j) {
|
|||||||
return jvp_string_ptr(&j.val.complex)->data;
|
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)
|
* Objects (internal helpers)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ jv jv_string_sized(const char*, int);
|
|||||||
int jv_string_length(jv);
|
int jv_string_length(jv);
|
||||||
uint32_t jv_string_hash(jv);
|
uint32_t jv_string_hash(jv);
|
||||||
const char* jv_string_value(jv);
|
const char* jv_string_value(jv);
|
||||||
|
jv jv_string_fmt(const char*, ...);
|
||||||
|
|
||||||
jv jv_object();
|
jv jv_object();
|
||||||
jv jv_object_get(jv object, jv key);
|
jv jv_object_get(jv object, jv key);
|
||||||
|
|||||||
@@ -108,6 +108,15 @@ int main(){
|
|||||||
jv_free(a1);
|
jv_free(a1);
|
||||||
jv_free(a2);
|
jv_free(a2);
|
||||||
jv_free(b);
|
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
|
/// Objects
|
||||||
|
|||||||
Reference in new issue
Block a user