Better support for appending strings in JV.
This commit is contained in:
1 parent
df195b3187
commit
84ea129f26
3 files changed
+48
-17
No files matched your search
@@ -340,6 +340,7 @@ typedef struct {
|
|||||||
// high 31 bits are length, low bit is a flag
|
// high 31 bits are length, low bit is a flag
|
||||||
// indicating whether hash has been computed.
|
// indicating whether hash has been computed.
|
||||||
uint32_t length_hashed;
|
uint32_t length_hashed;
|
||||||
|
uint32_t alloc_length;
|
||||||
char data[];
|
char data[];
|
||||||
} jvp_string;
|
} jvp_string;
|
||||||
|
|
||||||
@@ -350,19 +351,19 @@ static jvp_string* jvp_string_ptr(jv_complex* a) {
|
|||||||
static jvp_string* jvp_string_alloc(uint32_t size) {
|
static jvp_string* jvp_string_alloc(uint32_t size) {
|
||||||
jvp_string* s = malloc(sizeof(jvp_string) + size + 1);
|
jvp_string* s = malloc(sizeof(jvp_string) + size + 1);
|
||||||
s->refcnt.count = 1;
|
s->refcnt.count = 1;
|
||||||
s->length_hashed = size << 1;
|
s->alloc_length = size;
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
static jv_complex jvp_string_new(const char* data, uint32_t length) {
|
static jv_complex jvp_string_new(const char* data, uint32_t length) {
|
||||||
jvp_string* s = jvp_string_alloc(length);
|
jvp_string* s = jvp_string_alloc(length);
|
||||||
|
s->length_hashed = length << 1;
|
||||||
memcpy(s->data, data, length);
|
memcpy(s->data, data, length);
|
||||||
s->data[length] = 0;
|
s->data[length] = 0;
|
||||||
jv_complex r = {&s->refcnt, {0,0}};
|
jv_complex r = {&s->refcnt, {0,0}};
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void jvp_string_free(jv_complex* s) {
|
static void jvp_string_free(jv_complex* s) {
|
||||||
if (jvp_refcnt_dec(s)) {
|
if (jvp_refcnt_dec(s)) {
|
||||||
jvp_string* str = jvp_string_ptr(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;
|
return s->length_hashed >> 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static jv_complex jvp_string_concat(jvp_string* a, jvp_string* b) {
|
static uint32_t jvp_string_remaining_space(jvp_string* s) {
|
||||||
uint32_t la = jvp_string_length(a), lb = jvp_string_length(b);
|
uint32_t r = s->alloc_length - jvp_string_length(s);
|
||||||
jvp_string* s = jvp_string_alloc(la + lb);
|
assert(r >= 0);
|
||||||
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}};
|
|
||||||
return r;
|
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 const uint32_t HASH_SEED = 0x432A9843;
|
||||||
|
|
||||||
static uint32_t rotl32 (uint32_t x, int8_t r){
|
static uint32_t rotl32 (uint32_t x, int8_t r){
|
||||||
@@ -507,15 +529,22 @@ const char* jv_string_value(jv j) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jv jv_string_concat(jv a, jv b) {
|
jv jv_string_concat(jv a, jv b) {
|
||||||
jv j;
|
jvp_string* sb = jvp_string_ptr(&b.val.complex);
|
||||||
j.kind = JV_KIND_STRING;
|
jvp_string_append(&a.val.complex, sb->data, jvp_string_length(sb));
|
||||||
j.val.complex = jvp_string_concat(jvp_string_ptr(&a.val.complex),
|
|
||||||
jvp_string_ptr(&b.val.complex));
|
|
||||||
jv_free(a);
|
|
||||||
jv_free(b);
|
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, ...) {
|
jv jv_string_fmt(const char* fmt, ...) {
|
||||||
int size = 1024;
|
int size = 1024;
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|||||||
@@ -80,6 +80,8 @@ uint32_t jv_string_hash(jv);
|
|||||||
const char* jv_string_value(jv);
|
const char* jv_string_value(jv);
|
||||||
jv jv_string_concat(jv, jv);
|
jv jv_string_concat(jv, jv);
|
||||||
jv jv_string_fmt(const char*, ...);
|
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();
|
||||||
jv jv_object_get(jv object, jv key);
|
jv jv_object_get(jv object, jv key);
|
||||||
|
|||||||
+2
-2
@@ -159,9 +159,9 @@ null
|
|||||||
"asdfasdf"
|
"asdfasdf"
|
||||||
{"a":1, "b":2, "c":3}
|
{"a":1, "b":2, "c":3}
|
||||||
|
|
||||||
"asdf" + "jkl;" + .
|
"asdf" + "jkl;" + . + . + .
|
||||||
"some string"
|
"some string"
|
||||||
"asdfjkl;some string"
|
"asdfjkl;some stringsome stringsome string"
|
||||||
|
|
||||||
"\u0000\u0020\u0000" + .
|
"\u0000\u0020\u0000" + .
|
||||||
"\u0000\u0020\u0000"
|
"\u0000\u0020\u0000"
|
||||||
|
|||||||
Reference in new issue
Block a user