summaryrefslogtreecommitdiff
path: root/ext/json
diff options
context:
space:
mode:
Diffstat (limited to 'ext/json')
-rw-r--r--ext/json/ext/fbuffer/fbuffer.h156
-rw-r--r--ext/json/ext/generator/generator.c187
-rw-r--r--ext/json/ext/generator/generator.h41
-rw-r--r--ext/json/ext/parser/parser.c191
-rw-r--r--ext/json/ext/parser/parser.h7
-rw-r--r--ext/json/ext/parser/parser.rl27
6 files changed, 333 insertions, 276 deletions
diff --git a/ext/json/ext/fbuffer/fbuffer.h b/ext/json/ext/fbuffer/fbuffer.h
new file mode 100644
index 0000000..7d1dad7
--- /dev/null
+++ b/ext/json/ext/fbuffer/fbuffer.h
@@ -0,0 +1,156 @@
+
+#ifndef _FBUFFER_H_
+#define _FBUFFER_H_
+
+#include <assert.h>
+#include "ruby.h"
+
+#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)
+#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
+
+typedef struct FBufferStruct {
+ unsigned long initial_length;
+ char *ptr;
+ unsigned long len;
+ unsigned long capa;
+} FBuffer;
+
+#define FBUFFER_INITIAL_LENGTH_DEFAULT 1024
+
+#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 FBuffer *fbuffer_alloc(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);
+
+static FBuffer *fbuffer_alloc(unsigned long initial_length)
+{
+ FBuffer *fb;
+ if (initial_length <= 0) initial_length = FBUFFER_INITIAL_LENGTH_DEFAULT;
+ 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;
+
+ assert(len > 0);
+ if (len > 0) {
+ result = fbuffer_alloc(len);
+ fbuffer_append(result, FBUFFER_PAIR(fb));
+ }
+ return result;
+}
+
+static VALUE fbuffer_to_s(FBuffer *fb)
+{
+ VALUE result = rb_str_new(FBUFFER_PAIR(fb));
+ fbuffer_free(fb);
+ FORCE_UTF8(result);
+ return result;
+}
+#endif
diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c
index 8d8e71e..fa99ffd 100644
--- a/ext/json/ext/generator/generator.c
+++ b/ext/json/ext/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/ext/generator/generator.h b/ext/json/ext/generator/generator.h
index 1fdd351..3220178 100644
--- a/ext/json/ext/generator/generator.h
+++ b/ext/json/ext/generator/generator.h
@@ -13,13 +13,6 @@
#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)
-#endif
-
#define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
#ifndef RHASH_SIZE
@@ -43,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
@@ -104,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 */
@@ -126,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) \
diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c
index 18ee58e..ae8f3ab 100644
--- a/ext/json/ext/parser/parser.c
+++ b/ext/json/ext/parser/parser.c
@@ -1,5 +1,6 @@
#line 1 "parser.rl"
+#include "../fbuffer/fbuffer.h"
#include "parser.h"
/* unicode */
@@ -83,11 +84,11 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_match_string, i_aset, i_leftshift;
-#line 109 "parser.rl"
+#line 110 "parser.rl"
-#line 91 "parser.c"
+#line 92 "parser.c"
static const int JSON_object_start = 1;
static const int JSON_object_first_final = 27;
static const int JSON_object_error = 0;
@@ -95,7 +96,7 @@ static const int JSON_object_error = 0;
static const int JSON_object_en_main = 1;
-#line 150 "parser.rl"
+#line 151 "parser.rl"
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -111,14 +112,14 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
-#line 115 "parser.c"
+#line 116 "parser.c"
{
cs = JSON_object_start;
}
-#line 165 "parser.rl"
+#line 166 "parser.rl"
-#line 122 "parser.c"
+#line 123 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -146,7 +147,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 132 "parser.rl"
+#line 133 "parser.rl"
{
char *np;
json->parsing_name = 1;
@@ -159,7 +160,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 163 "parser.c"
+#line 164 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -226,7 +227,7 @@ case 8:
goto st8;
goto st0;
tr11:
-#line 117 "parser.rl"
+#line 118 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v);
@@ -246,7 +247,7 @@ st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 250 "parser.c"
+#line 251 "parser.c"
switch( (*p) ) {
case 13: goto st9;
case 32: goto st9;
@@ -335,14 +336,14 @@ case 18:
goto st9;
goto st18;
tr4:
-#line 140 "parser.rl"
+#line 141 "parser.rl"
{ p--; {p++; cs = 27; goto _out;} }
goto st27;
st27:
if ( ++p == pe )
goto _test_eof27;
case 27:
-#line 346 "parser.c"
+#line 347 "parser.c"
goto st0;
st19:
if ( ++p == pe )
@@ -440,7 +441,7 @@ case 26:
_out: {}
}
-#line 166 "parser.rl"
+#line 167 "parser.rl"
if (cs >= JSON_object_first_final) {
if (json->create_additions) {
@@ -460,7 +461,7 @@ case 26:
-#line 464 "parser.c"
+#line 465 "parser.c"
static const int JSON_value_start = 1;
static const int JSON_value_first_final = 21;
static const int JSON_value_error = 0;
@@ -468,7 +469,7 @@ static const int JSON_value_error = 0;
static const int JSON_value_en_main = 1;
-#line 265 "parser.rl"
+#line 266 "parser.rl"
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -476,14 +477,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 480 "parser.c"
+#line 481 "parser.c"
{
cs = JSON_value_start;
}
-#line 272 "parser.rl"
+#line 273 "parser.rl"
-#line 487 "parser.c"
+#line 488 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -508,14 +509,14 @@ st0:
cs = 0;
goto _out;
tr0:
-#line 213 "parser.rl"
+#line 214 "parser.rl"
{
char *np = JSON_parse_string(json, p, pe, result);
if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
}
goto st21;
tr2:
-#line 218 "parser.rl"
+#line 219 "parser.rl"
{
char *np;
if(pe > p + 9 - json->quirks_mode && !strncmp(MinusInfinity, p, 9)) {
@@ -535,7 +536,7 @@ tr2:
}
goto st21;
tr5:
-#line 236 "parser.rl"
+#line 237 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -545,7 +546,7 @@ tr5:
}
goto st21;
tr9:
-#line 244 "parser.rl"
+#line 245 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -555,7 +556,7 @@ tr9:
}
goto st21;
tr16:
-#line 206 "parser.rl"
+#line 207 "parser.rl"
{
if (json->allow_nan) {
*result = CInfinity;
@@ -565,7 +566,7 @@ tr16:
}
goto st21;
tr18:
-#line 199 "parser.rl"
+#line 200 "parser.rl"
{
if (json->allow_nan) {
*result = CNaN;
@@ -575,19 +576,19 @@ tr18:
}
goto st21;
tr22:
-#line 193 "parser.rl"
+#line 194 "parser.rl"
{
*result = Qfalse;
}
goto st21;
tr25:
-#line 190 "parser.rl"
+#line 191 "parser.rl"
{
*result = Qnil;
}
goto st21;
tr28:
-#line 196 "parser.rl"
+#line 197 "parser.rl"
{
*result = Qtrue;
}
@@ -596,9 +597,9 @@ st21:
if ( ++p == pe )
goto _test_eof21;
case 21:
-#line 252 "parser.rl"
+#line 253 "parser.rl"
{ p--; {p++; cs = 21; goto _out;} }
-#line 602 "parser.c"
+#line 603 "parser.c"
goto st0;
st2:
if ( ++p == pe )
@@ -759,7 +760,7 @@ case 20:
_out: {}
}
-#line 273 "parser.rl"
+#line 274 "parser.rl"
if (cs >= JSON_value_first_final) {
return p;
@@ -769,7 +770,7 @@ case 20:
}
-#line 773 "parser.c"
+#line 774 "parser.c"
static const int JSON_integer_start = 1;
static const int JSON_integer_first_final = 3;
static const int JSON_integer_error = 0;
@@ -777,7 +778,7 @@ static const int JSON_integer_error = 0;
static const int JSON_integer_en_main = 1;
-#line 289 "parser.rl"
+#line 290 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -785,15 +786,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL;
-#line 789 "parser.c"
+#line 790 "parser.c"
{
cs = JSON_integer_start;
}
-#line 296 "parser.rl"
+#line 297 "parser.rl"
json->memo = p;
-#line 797 "parser.c"
+#line 798 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -827,14 +828,14 @@ case 3:
goto st0;
goto tr4;
tr4:
-#line 286 "parser.rl"
+#line 287 "parser.rl"
{ p--; {p++; cs = 4; goto _out;} }
goto st4;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
-#line 838 "parser.c"
+#line 839 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -853,11 +854,14 @@ case 5:
_out: {}
}
-#line 298 "parser.rl"
+#line 299 "parser.rl"
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
- *result = rb_Integer(rb_str_new(json->memo, len));
+ fbuffer_clear(json->fbuffer);
+ fbuffer_append(json->fbuffer, json->memo, len);
+ fbuffer_append_char(json->fbuffer, '\0');
+ *result = rb_cstr2inum(FBUFFER_PTR(json->fbuffer), 10);
return p + 1;
} else {
return NULL;
@@ -865,7 +869,7 @@ case 5:
}
-#line 869 "parser.c"
+#line 873 "parser.c"
static const int JSON_float_start = 1;
static const int JSON_float_first_final = 8;
static const int JSON_float_error = 0;
@@ -873,7 +877,7 @@ static const int JSON_float_error = 0;
static const int JSON_float_en_main = 1;
-#line 320 "parser.rl"
+#line 324 "parser.rl"
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -881,15 +885,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 885 "parser.c"
+#line 889 "parser.c"
{
cs = JSON_float_start;
}
-#line 327 "parser.rl"
+#line 331 "parser.rl"
json->memo = p;
-#line 893 "parser.c"
+#line 897 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -947,14 +951,14 @@ case 8:
goto st0;
goto tr9;
tr9:
-#line 314 "parser.rl"
+#line 318 "parser.rl"
{ p--; {p++; cs = 9; goto _out;} }
goto st9;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 958 "parser.c"
+#line 962 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -1015,11 +1019,14 @@ case 7:
_out: {}
}
-#line 329 "parser.rl"
+#line 333 "parser.rl"
if (cs >= JSON_float_first_final) {
long len = p - json->memo;
- *result = rb_Float(rb_str_new(json->memo, len));
+ fbuffer_clear(json->fbuffer);
+ fbuffer_append(json->fbuffer, json->memo, len);
+ fbuffer_append_char(json->fbuffer, '\0');
+ *result = rb_float_new(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1));
return p + 1;
} else {
return NULL;
@@ -1028,7 +1035,7 @@ case 7:
-#line 1032 "parser.c"
+#line 1039 "parser.c"
static const int JSON_array_start = 1;
static const int JSON_array_first_final = 17;
static const int JSON_array_error = 0;
@@ -1036,7 +1043,7 @@ static const int JSON_array_error = 0;
static const int JSON_array_en_main = 1;
-#line 369 "parser.rl"
+#line 376 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -1050,14 +1057,14 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul
*result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
-#line 1054 "parser.c"
+#line 1061 "parser.c"
{
cs = JSON_array_start;
}
-#line 382 "parser.rl"
+#line 389 "parser.rl"
-#line 1061 "parser.c"
+#line 1068 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1096,7 +1103,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 346 "parser.rl"
+#line 353 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v);
@@ -1116,7 +1123,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 1120 "parser.c"
+#line 1127 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -1216,14 +1223,14 @@ case 12:
goto st3;
goto st12;
tr4:
-#line 361 "parser.rl"
+#line 368 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 1227 "parser.c"
+#line 1234 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@@ -1279,7 +1286,7 @@ case 16:
_out: {}
}
-#line 383 "parser.rl"
+#line 390 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
@@ -1360,7 +1367,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
}
-#line 1364 "parser.c"
+#line 1371 "parser.c"
static const int JSON_string_start = 1;
static const int JSON_string_first_final = 8;
static const int JSON_string_error = 0;
@@ -1368,7 +1375,7 @@ static const int JSON_string_error = 0;
static const int JSON_string_en_main = 1;
-#line 482 "parser.rl"
+#line 489 "parser.rl"
static int
@@ -1390,15 +1397,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = rb_str_buf_new(0);
-#line 1394 "parser.c"
+#line 1401 "parser.c"
{
cs = JSON_string_start;
}
-#line 503 "parser.rl"
+#line 510 "parser.rl"
json->memo = p;
-#line 1402 "parser.c"
+#line 1409 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1423,7 +1430,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 468 "parser.rl"
+#line 475 "parser.rl"
{
*result = json_string_unescape(*result, json->memo + 1, p);
if (NIL_P(*result)) {
@@ -1434,14 +1441,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
-#line 479 "parser.rl"
+#line 486 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1445 "parser.c"
+#line 1452 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1517,7 +1524,7 @@ case 7:
_out: {}
}
-#line 505 "parser.rl"
+#line 512 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
@@ -1714,6 +1721,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
json->object_class = Qnil;
json->array_class = Qnil;
}
+ source = rb_convert_type(source, T_STRING, "String", "to_str");
if (!json->quirks_mode) {
source = convert_encoding(StringValue(source));
}
@@ -1725,7 +1733,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
-#line 1729 "parser.c"
+#line 1737 "parser.c"
static const int JSON_start = 1;
static const int JSON_first_final = 10;
static const int JSON_error = 0;
@@ -1733,7 +1741,7 @@ static const int JSON_error = 0;
static const int JSON_en_main = 1;
-#line 736 "parser.rl"
+#line 744 "parser.rl"
static VALUE cParser_parse_strict(VALUE self)
@@ -1744,16 +1752,16 @@ static VALUE cParser_parse_strict(VALUE self)
GET_PARSER;
-#line 1748 "parser.c"
+#line 1756 "parser.c"
{
cs = JSON_start;
}
-#line 746 "parser.rl"
+#line 754 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1757 "parser.c"
+#line 1765 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1809,7 +1817,7 @@ case 5:
goto st1;
goto st5;
tr3:
-#line 725 "parser.rl"
+#line 733 "parser.rl"
{
char *np;
json->current_nesting = 1;
@@ -1818,7 +1826,7 @@ tr3:
}
goto st10;
tr4:
-#line 718 "parser.rl"
+#line 726 "parser.rl"
{
char *np;
json->current_nesting = 1;
@@ -1830,7 +1838,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1834 "parser.c"
+#line 1842 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -1887,7 +1895,7 @@ case 9:
_out: {}
}
-#line 749 "parser.rl"
+#line 757 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
@@ -1899,7 +1907,7 @@ case 9:
-#line 1903 "parser.c"
+#line 1911 "parser.c"
static const int JSON_quirks_mode_start = 1;
static const int JSON_quirks_mode_first_final = 10;
static const int JSON_quirks_mode_error = 0;
@@ -1907,7 +1915,7 @@ static const int JSON_quirks_mode_error = 0;
static const int JSON_quirks_mode_en_main = 1;
-#line 774 "parser.rl"
+#line 782 "parser.rl"
static VALUE cParser_parse_quirks_mode(VALUE self)
@@ -1918,16 +1926,16 @@ static VALUE cParser_parse_quirks_mode(VALUE self)
GET_PARSER;
-#line 1922 "parser.c"
+#line 1930 "parser.c"
{
cs = JSON_quirks_mode_start;
}
-#line 784 "parser.rl"
+#line 792 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1931 "parser.c"
+#line 1939 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1961,7 +1969,7 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 766 "parser.rl"
+#line 774 "parser.rl"
{
char *np = JSON_parse_value(json, p, pe, &result);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1971,7 +1979,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1975 "parser.c"
+#line 1983 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -2060,7 +2068,7 @@ case 9:
_out: {}
}
-#line 787 "parser.rl"
+#line 795 "parser.rl"
if (cs >= JSON_quirks_mode_first_final && p == pe) {
return result;
@@ -2092,13 +2100,13 @@ static JSON_Parser *JSON_allocate()
{
JSON_Parser *json = ALLOC(JSON_Parser);
MEMZERO(json, JSON_Parser, 1);
- json->dwrapped_parser = Qnil;
- json->Vsource = Qnil;
- json->create_id = Qnil;
- json->object_class = Qnil;
- json->array_class = Qnil;
- json->match_string = Qnil;
-
+ json->dwrapped_parser = Qfalse;
+ json->Vsource = Qfalse;
+ json->create_id = Qfalse;
+ json->object_class = Qfalse;
+ json->array_class = Qfalse;
+ json->match_string = Qfalse;
+ json->fbuffer = fbuffer_alloc(0);
return json;
}
@@ -2113,6 +2121,7 @@ static void JSON_mark(JSON_Parser *json)
static void JSON_free(JSON_Parser *json)
{
+ fbuffer_free(json->fbuffer);
ruby_xfree(json);
}
diff --git a/ext/json/ext/parser/parser.h b/ext/json/ext/parser/parser.h
index 051f79c..e01e020 100644
--- a/ext/json/ext/parser/parser.h
+++ b/ext/json/ext/parser/parser.h
@@ -7,12 +7,6 @@
#include "re.h"
#endif
-#ifdef HAVE_RUBY_ENCODING_H
-#include "ruby/encoding.h"
-#define FORCE_UTF8(obj) ((obj) = rb_enc_associate(rb_str_dup(obj), rb_utf8_encoding()))
-#else
-#define FORCE_UTF8(obj)
-#endif
#ifdef HAVE_RUBY_ST_H
#include "ruby/st.h"
#else
@@ -50,6 +44,7 @@ typedef struct JSON_ParserStruct {
VALUE array_class;
int create_additions;
VALUE match_string;
+ FBuffer *fbuffer;
} JSON_Parser;
#define GET_PARSER \
diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl
index 2a2cc89..9e7b7da 100644
--- a/ext/json/ext/parser/parser.rl
+++ b/ext/json/ext/parser/parser.rl
@@ -1,3 +1,4 @@
+#include "../fbuffer/fbuffer.h"
#include "parser.h"
/* unicode */
@@ -298,7 +299,10 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
- *result = rb_Integer(rb_str_new(json->memo, len));
+ fbuffer_clear(json->fbuffer);
+ fbuffer_append(json->fbuffer, json->memo, len);
+ fbuffer_append_char(json->fbuffer, '\0');
+ *result = rb_cstr2inum(FBUFFER_PTR(json->fbuffer), 10);
return p + 1;
} else {
return NULL;
@@ -329,7 +333,10 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
if (cs >= JSON_float_first_final) {
long len = p - json->memo;
- *result = rb_Float(rb_str_new(json->memo, len));
+ fbuffer_clear(json->fbuffer);
+ fbuffer_append(json->fbuffer, json->memo, len);
+ fbuffer_append_char(json->fbuffer, '\0');
+ *result = rb_float_new(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1));
return p + 1;
} else {
return NULL;
@@ -698,6 +705,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
json->object_class = Qnil;
json->array_class = Qnil;
}
+ source = rb_convert_type(source, T_STRING, "String", "to_str");
if (!json->quirks_mode) {
source = convert_encoding(StringValue(source));
}
@@ -815,13 +823,13 @@ static JSON_Parser *JSON_allocate()
{
JSON_Parser *json = ALLOC(JSON_Parser);
MEMZERO(json, JSON_Parser, 1);
- json->dwrapped_parser = Qnil;
- json->Vsource = Qnil;
- json->create_id = Qnil;
- json->object_class = Qnil;
- json->array_class = Qnil;
- json->match_string = Qnil;
-
+ json->dwrapped_parser = Qfalse;
+ json->Vsource = Qfalse;
+ json->create_id = Qfalse;
+ json->object_class = Qfalse;
+ json->array_class = Qfalse;
+ json->match_string = Qfalse;
+ json->fbuffer = fbuffer_alloc(0);
return json;
}
@@ -836,6 +844,7 @@ static void JSON_mark(JSON_Parser *json)
static void JSON_free(JSON_Parser *json)
{
+ fbuffer_free(json->fbuffer);
ruby_xfree(json);
}