summaryrefslogtreecommitdiff
path: root/ext/json/generator
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-11 13:36:06 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-11 13:36:06 +0000
commit4d8d3184d18b1a9d2eadb86a6688dc62eca7eabc (patch)
tree03e3a00e4e484ffdb46d5d774ab887dc695b8ed0 /ext/json/generator
parent9d9ab5a384de3877f29cef8c0293c1caa432692c (diff)
* ext/json: Merge 164a75c8bd2007d32c4d7665d53140d8fc126dcd.
[ruby-core:41917] [Bug #5846] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34971 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/json/generator')
-rw-r--r--ext/json/generator/extconf.rb7
-rw-r--r--ext/json/generator/generator.c187
-rw-r--r--ext/json/generator/generator.h47
3 files changed, 60 insertions, 181 deletions
diff --git a/ext/json/generator/extconf.rb b/ext/json/generator/extconf.rb
index b94f71e8b3..75c76ce15b 100644
--- a/ext/json/generator/extconf.rb
+++ b/ext/json/generator/extconf.rb
@@ -1,10 +1,3 @@
require 'mkmf'
-require 'rbconfig'
-if RUBY_VERSION < "1.9"
- have_header("re.h")
-else
- have_header("ruby/re.h")
- have_header("ruby/encoding.h")
-end
create_makefile 'json/ext/generator'
diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c
index 781e9e6168..21fef2b657 100644
--- a/ext/json/generator/generator.c
+++ b/ext/json/generator/generator.c
@@ -1,3 +1,4 @@
+#include "../fbuffer/fbuffer.h"
#include "generator.h"
#ifdef HAVE_RUBY_ENCODING_H
@@ -14,7 +15,8 @@ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
i_quirks_mode, i_pack, i_unpack, i_create_id, i_extend, i_key_p,
- i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth, i_dup;
+ i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth,
+ i_buffer_initial_length, i_dup;
/*
* Copyright 2001-2004 Unicode, Inc.
@@ -292,123 +294,6 @@ static char *fstrndup(const char *ptr, unsigned long len) {
return result;
}
-/* fbuffer implementation */
-
-static FBuffer *fbuffer_alloc()
-{
- FBuffer *fb = ALLOC(FBuffer);
- memset((void *) fb, 0, sizeof(FBuffer));
- fb->initial_length = FBUFFER_INITIAL_LENGTH;
- return fb;
-}
-
-static FBuffer *fbuffer_alloc_with_length(unsigned long initial_length)
-{
- FBuffer *fb;
- assert(initial_length > 0);
- fb = ALLOC(FBuffer);
- memset((void *) fb, 0, sizeof(FBuffer));
- fb->initial_length = initial_length;
- return fb;
-}
-
-static void fbuffer_free(FBuffer *fb)
-{
- if (fb->ptr) ruby_xfree(fb->ptr);
- ruby_xfree(fb);
-}
-
-static void fbuffer_clear(FBuffer *fb)
-{
- fb->len = 0;
-}
-
-static void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
-{
- unsigned long required;
-
- if (!fb->ptr) {
- fb->ptr = ALLOC_N(char, fb->initial_length);
- fb->capa = fb->initial_length;
- }
-
- for (required = fb->capa; requested > required - fb->len; required <<= 1);
-
- if (required > fb->capa) {
- REALLOC_N(fb->ptr, char, required);
- fb->capa = required;
- }
-}
-
-static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
-{
- if (len > 0) {
- fbuffer_inc_capa(fb, len);
- MEMCPY(fb->ptr + fb->len, newstr, char, len);
- fb->len += len;
- }
-}
-
-static void fbuffer_append_str(FBuffer *fb, VALUE str)
-{
- const char *newstr = StringValuePtr(str);
- unsigned long len = RSTRING_LEN(str);
-
- RB_GC_GUARD(str);
-
- fbuffer_append(fb, newstr, len);
-}
-
-static void fbuffer_append_char(FBuffer *fb, char newchr)
-{
- fbuffer_inc_capa(fb, 1);
- *(fb->ptr + fb->len) = newchr;
- fb->len++;
-}
-
-static void freverse(char *start, char *end)
-{
- char c;
-
- while (end > start) {
- c = *end, *end-- = *start, *start++ = c;
- }
-}
-
-static long fltoa(long number, char *buf)
-{
- static char digits[] = "0123456789";
- long sign = number;
- char* tmp = buf;
-
- if (sign < 0) number = -number;
- do *tmp++ = digits[number % 10]; while (number /= 10);
- if (sign < 0) *tmp++ = '-';
- freverse(buf, tmp - 1);
- return tmp - buf;
-}
-
-static void fbuffer_append_long(FBuffer *fb, long number)
-{
- char buf[20];
- unsigned long len = fltoa(number, buf);
- fbuffer_append(fb, buf, len);
-}
-
-static FBuffer *fbuffer_dup(FBuffer *fb)
-{
- unsigned long len = fb->len;
- FBuffer *result;
-
- if (len > 0) {
- result = fbuffer_alloc_with_length(len);
- fbuffer_append(result, FBUFFER_PAIR(fb));
- } else {
- result = fbuffer_alloc();
- }
- return result;
-}
-
/*
* Document-module: JSON::Ext::Generator
*
@@ -694,6 +579,16 @@ static VALUE cState_configure(VALUE self, VALUE opts)
state->depth = 0;
}
}
+ tmp = ID2SYM(i_buffer_initial_length);
+ if (option_given_p(opts, tmp)) {
+ VALUE buffer_initial_length = rb_hash_aref(opts, tmp);
+ if (RTEST(buffer_initial_length)) {
+ long initial_length;
+ Check_Type(buffer_initial_length, T_FIXNUM);
+ initial_length = FIX2LONG(buffer_initial_length);
+ if (initial_length > 0) state->buffer_initial_length = initial_length;
+ }
+ }
tmp = rb_hash_aref(opts, ID2SYM(i_allow_nan));
state->allow_nan = RTEST(tmp);
tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
@@ -723,6 +618,7 @@ static VALUE cState_to_h(VALUE self)
rb_hash_aset(result, ID2SYM(i_quirks_mode), state->quirks_mode ? Qtrue : Qfalse);
rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
+ rb_hash_aset(result, ID2SYM(i_buffer_initial_length), LONG2FIX(state->buffer_initial_length));
return result;
}
@@ -920,19 +816,20 @@ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *s
static FBuffer *cState_prepare_buffer(VALUE self)
{
- FBuffer *buffer = fbuffer_alloc();
+ FBuffer *buffer;
GET_STATE(self);
+ buffer = fbuffer_alloc(state->buffer_initial_length);
if (state->object_delim) {
fbuffer_clear(state->object_delim);
} else {
- state->object_delim = fbuffer_alloc_with_length(16);
+ state->object_delim = fbuffer_alloc(16);
}
fbuffer_append_char(state->object_delim, ',');
if (state->object_delim2) {
fbuffer_clear(state->object_delim2);
} else {
- state->object_delim2 = fbuffer_alloc_with_length(16);
+ state->object_delim2 = fbuffer_alloc(16);
}
fbuffer_append_char(state->object_delim2, ':');
if (state->space) fbuffer_append(state->object_delim2, state->space, state->space_len);
@@ -940,21 +837,13 @@ static FBuffer *cState_prepare_buffer(VALUE self)
if (state->array_delim) {
fbuffer_clear(state->array_delim);
} else {
- state->array_delim = fbuffer_alloc_with_length(16);
+ state->array_delim = fbuffer_alloc(16);
}
fbuffer_append_char(state->array_delim, ',');
if (state->array_nl) fbuffer_append(state->array_delim, state->array_nl, state->array_nl_len);
return buffer;
}
-static VALUE fbuffer_to_s(FBuffer *fb)
-{
- VALUE result = rb_str_new(FBUFFER_PAIR(fb));
- fbuffer_free(fb);
- FORCE_UTF8(result);
- return result;
-}
-
static VALUE cState_partial_generate(VALUE self, VALUE obj)
{
FBuffer *buffer = cState_prepare_buffer(self);
@@ -1003,12 +892,15 @@ static VALUE cState_generate(VALUE self, VALUE obj)
* encountered. This options defaults to false.
* * *quirks_mode*: Enables quirks_mode for parser, that is for example
* generating single JSON values instead of documents is possible.
+ * * *buffer_initial_length*: sets the initial length of the generator's
+ * internal buffer.
*/
static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE opts;
GET_STATE(self);
state->max_nesting = 19;
+ state->buffer_initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
rb_scan_args(argc, argv, "01", &opts);
if (!NIL_P(opts)) cState_configure(self, opts);
return self;
@@ -1349,7 +1241,37 @@ static VALUE cState_depth_set(VALUE self, VALUE depth)
{
GET_STATE(self);
Check_Type(depth, T_FIXNUM);
- return state->depth = FIX2LONG(depth);
+ state->depth = FIX2LONG(depth);
+ return Qnil;
+}
+
+/*
+ * call-seq: buffer_initial_length
+ *
+ * This integer returns the current inital length of the buffer.
+ */
+static VALUE cState_buffer_initial_length(VALUE self)
+{
+ GET_STATE(self);
+ return LONG2FIX(state->buffer_initial_length);
+}
+
+/*
+ * call-seq: buffer_initial_length=(length)
+ *
+ * This sets the initial length of the buffer to +length+, if +length+ > 0,
+ * otherwise its value isn't changed.
+ */
+static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_length)
+{
+ long initial_length;
+ GET_STATE(self);
+ Check_Type(buffer_initial_length, T_FIXNUM);
+ initial_length = FIX2LONG(buffer_initial_length);
+ if (initial_length > 0) {
+ state->buffer_initial_length = initial_length;
+ }
+ return Qnil;
}
/*
@@ -1391,6 +1313,8 @@ void Init_generator()
rb_define_method(cState, "quirks_mode=", cState_quirks_mode_set, 1);
rb_define_method(cState, "depth", cState_depth, 0);
rb_define_method(cState, "depth=", cState_depth_set, 1);
+ rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
+ rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
rb_define_method(cState, "configure", cState_configure, 1);
rb_define_alias(cState, "merge", "configure");
rb_define_method(cState, "to_h", cState_to_h, 0);
@@ -1438,6 +1362,7 @@ void Init_generator()
i_ascii_only = rb_intern("ascii_only");
i_quirks_mode = rb_intern("quirks_mode");
i_depth = rb_intern("depth");
+ i_buffer_initial_length = rb_intern("buffer_initial_length");
i_pack = rb_intern("pack");
i_unpack = rb_intern("unpack");
i_create_id = rb_intern("create_id");
diff --git a/ext/json/generator/generator.h b/ext/json/generator/generator.h
index f882ea004b..3220178a8f 100644
--- a/ext/json/generator/generator.h
+++ b/ext/json/generator/generator.h
@@ -7,19 +7,10 @@
#include "ruby.h"
-#if HAVE_RUBY_RE_H
+#ifdef HAVE_RUBY_RE_H
#include "ruby/re.h"
-#endif
-
-#if HAVE_RE_H
-#include "re.h"
-#endif
-
-#ifdef HAVE_RUBY_ENCODING_H
-#include "ruby/encoding.h"
-#define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
#else
-#define FORCE_UTF8(obj)
+#include "re.h"
#endif
#define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
@@ -45,38 +36,6 @@
#define RSTRING_LEN(string) RSTRING(string)->len
#endif
-/* We don't need to guard objects for rbx, so let's do nothing at all. */
-#ifndef RB_GC_GUARD
-#define RB_GC_GUARD(object)
-#endif
-
-/* fbuffer implementation */
-
-typedef struct FBufferStruct {
- unsigned long initial_length;
- char *ptr;
- unsigned long len;
- unsigned long capa;
-} FBuffer;
-
-#define FBUFFER_INITIAL_LENGTH 4096
-
-#define FBUFFER_PTR(fb) (fb->ptr)
-#define FBUFFER_LEN(fb) (fb->len)
-#define FBUFFER_CAPA(fb) (fb->capa)
-#define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
-
-static char *fstrndup(const char *ptr, unsigned long len);
-static FBuffer *fbuffer_alloc();
-static FBuffer *fbuffer_alloc_with_length(unsigned long initial_length);
-static void fbuffer_free(FBuffer *fb);
-static void fbuffer_clear(FBuffer *fb);
-static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
-static void fbuffer_append_long(FBuffer *fb, long number);
-static void fbuffer_append_char(FBuffer *fb, char newchr);
-static FBuffer *fbuffer_dup(FBuffer *fb);
-static VALUE fbuffer_to_s(FBuffer *fb);
-
/* unicode defintions */
#define UNI_STRICT_CONVERSION 1
@@ -106,6 +65,7 @@ static void unicode_escape(char *buf, UTF16 character);
static void unicode_escape_to_buffer(FBuffer *buffer, char buf[6], UTF16 character);
static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string);
static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string);
+static char *fstrndup(const char *ptr, unsigned long len);
/* ruby api and some helpers */
@@ -128,6 +88,7 @@ typedef struct JSON_Generator_StateStruct {
char ascii_only;
char quirks_mode;
long depth;
+ long buffer_initial_length;
} JSON_Generator_State;
#define GET_STATE(self) \