summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2009-11-20 04:42:33 +0100
committerFlorian Frank <flori@ping.de>2009-11-20 04:42:33 +0100
commit7fbf4caae104b6010c849f8e1edcd060e95485f4 (patch)
treec23785b2677309eecbd5b71bedd0b4c93b42b8e3
parentd1047b1f9cc022ee575ee7434a02b45aa238b0b9 (diff)
downloadjson-7fbf4caae104b6010c849f8e1edcd060e95485f4.tar.gz
some more optimisation
-rw-r--r--ext/json/ext/generator.c32
-rw-r--r--ext/json/ext/generator.h1
2 files changed, 23 insertions, 10 deletions
diff --git a/ext/json/ext/generator.c b/ext/json/ext/generator.c
index 1db22a7..e07a7af 100644
--- a/ext/json/ext/generator.c
+++ b/ext/json/ext/generator.c
@@ -282,6 +282,13 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
fbuffer_append(buffer, ptr + start, end - start);
}
+static char *fstrndup(const char *ptr, int len) {
+ if (len <= 0) return NULL;
+ char *result = ALLOC_N(char, len);
+ memccpy(result, ptr, 0, len);
+ return result;
+}
+
/* fbuffer implementation */
static FBuffer *fbuffer_alloc()
@@ -592,32 +599,37 @@ static VALUE cState_configure(VALUE self, VALUE opts)
tmp = rb_hash_aref(opts, ID2SYM(i_indent));
if (RTEST(tmp)) {
Check_Type(tmp, T_STRING);
- state->indent = strdup(RSTRING_PTR(tmp));
- state->indent_len = strlen(state->indent);
+ int len = RSTRING_LEN(tmp);
+ state->indent = fstrndup(RSTRING_PTR(tmp), len);
+ state->indent_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_space));
if (RTEST(tmp)) {
Check_Type(tmp, T_STRING);
- state->space = strdup(RSTRING_PTR(tmp));
- state->space_len = strlen(state->space);
+ int len = RSTRING_LEN(tmp);
+ state->space = fstrndup(RSTRING_PTR(tmp), len);
+ state->space_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
if (RTEST(tmp)) {
Check_Type(tmp, T_STRING);
- state->space_before = strdup(RSTRING_PTR(tmp));
- state->space_before_len = strlen(state->space_before);
+ int len = RSTRING_LEN(tmp);
+ state->space_before = fstrndup(RSTRING_PTR(tmp), len);
+ state->space_before_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
if (RTEST(tmp)) {
Check_Type(tmp, T_STRING);
- state->array_nl = strdup(RSTRING_PTR(tmp));
- state->array_nl_len = strlen(state->array_nl);
+ int len = RSTRING_LEN(tmp);
+ state->array_nl = fstrndup(RSTRING_PTR(tmp), len);
+ state->array_nl_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
if (RTEST(tmp)) {
Check_Type(tmp, T_STRING);
- state->object_nl = strdup(RSTRING_PTR(tmp));
- state->object_nl_len = strlen(state->object_nl);
+ int len = RSTRING_LEN(tmp);
+ state->object_nl = fstrndup(RSTRING_PTR(tmp), len);
+ state->object_nl_len = len;
}
tmp = ID2SYM(i_max_nesting);
state->max_nesting = 19;
diff --git a/ext/json/ext/generator.h b/ext/json/ext/generator.h
index 5b5a28c..c466402 100644
--- a/ext/json/ext/generator.h
+++ b/ext/json/ext/generator.h
@@ -73,6 +73,7 @@ typedef struct FBufferStruct {
#define FBUFFER_CAPA(fb) (fb->capa)
#define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
+static char *fstrndup(const char *ptr, int len);
static FBuffer *fbuffer_alloc();
static FBuffer *fbuffer_alloc_with_length(unsigned initial_length);
static void fbuffer_free(FBuffer *fb);