From 05cbdc7db125240e18b909096a8b5d7b8a8cad06 Mon Sep 17 00:00:00 2001 From: Diego Plentz Date: Tue, 14 Feb 2012 23:24:38 -0200 Subject: making the travis badge visible :) --- README.rdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 039d759..eda896f 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,4 +1,4 @@ -= JSON implementation for Ruby http://travis-ci.org/flori/json.png?branch=master += JSON implementation for Ruby {}[http://travis-ci.org/flori/json] == Description -- cgit v1.2.1 From 8fd84e66cafa983b4922208950e4572b4734aa91 Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Tue, 20 Mar 2012 13:44:59 +0100 Subject: Use C implementation and better regexp for pure --- ext/json/ext/generator/generator.c | 35 +++++++++++++++++++++++++++-------- ext/json/ext/generator/generator.h | 1 + lib/json/pure/generator.rb | 8 ++++++-- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index 21fef2b..a765486 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -852,6 +852,31 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj) return fbuffer_to_s(buffer); } +static int isArrayOrObject(VALUE string) +{ + char c, *q, *p = RSTRING_PTR(string), *pend = p + RSTRING_LEN(string); + + while (p < pend) { + if (isspace(*p)) { + p++; + continue; + } + if (*p == '[') c = ']'; + else if (*p == '{') c = '}'; + else return 0; + q = pend - 1; + while (q > p) { + if (isspace(*q)) { + q--; + continue; + } + if (*q == c) return 1; + } + return 0; + } + return 0; +} + /* * call-seq: generate(obj) * @@ -862,15 +887,9 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj) static VALUE cState_generate(VALUE self, VALUE obj) { VALUE result = cState_partial_generate(self, obj); - VALUE re, args[2]; GET_STATE(self); - if (!state->quirks_mode) { - args[0] = rb_str_new2("\\A\\s*(?:\\[.*\\]|\\{.*\\})\\s*\\Z"); - args[1] = CRegexp_MULTILINE; - re = rb_class_new_instance(2, args, rb_cRegexp); - if (NIL_P(rb_funcall(re, i_match, 1, result))) { - rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed"); - } + if (!state->quirks_mode && !isArrayOrObject(result)) { + rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed"); } return result; } diff --git a/ext/json/ext/generator/generator.h b/ext/json/ext/generator/generator.h index 3220178..901b62c 100644 --- a/ext/json/ext/generator/generator.h +++ b/ext/json/ext/generator/generator.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "ruby.h" diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb index 9141ae5..3d5f09f 100644 --- a/lib/json/pure/generator.rb +++ b/lib/json/pure/generator.rb @@ -255,8 +255,12 @@ module JSON # GeneratorError exception. def generate(obj) result = obj.to_json(self) - if !@quirks_mode && result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m - raise GeneratorError, "only generation of JSON objects or arrays allowed" + unless @quirks_mode + unless result =~ /\A\s*\[/ && result =~ /\]\s*\Z/ || + result =~ /\A\s*\{/ && result =~ /\}\s*\Z/ + then + raise GeneratorError, "only generation of JSON objects or arrays allowed" + end end result end -- cgit v1.2.1 From 59f51d2d1b0b8d390fc4b14dbbd60e189b9280d3 Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Wed, 21 Mar 2012 09:10:35 +0100 Subject: Fix the fix --- ext/json/ext/generator/generator.c | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index a765486..cfd00b5 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -854,27 +854,12 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj) static int isArrayOrObject(VALUE string) { - char c, *q, *p = RSTRING_PTR(string), *pend = p + RSTRING_LEN(string); - - while (p < pend) { - if (isspace(*p)) { - p++; - continue; - } - if (*p == '[') c = ']'; - else if (*p == '{') c = '}'; - else return 0; - q = pend - 1; - while (q > p) { - if (isspace(*q)) { - q--; - continue; - } - if (*q == c) return 1; - } - return 0; - } - return 0; + long string_len = RSTRING_LEN(string); + char c, *p = RSTRING_PTR(string), *q = p + string_len - 1; + if (string_len < 2) return 0; + for (; p < q && isspace(*p); p++); + for (; q > p && isspace(*q); q--); + return *p == '[' && *q == ']' || *p == '{' && *q == '}'; } /* -- cgit v1.2.1