summaryrefslogtreecommitdiff
path: root/ext/json/ext/generator/generator.c
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2012-03-26 16:17:54 +0200
committerFlorian Frank <flori@ping.de>2012-03-26 16:17:54 +0200
commit519437ab428d67f4b4105e2a94f2f2c1601fdcf5 (patch)
tree26b76c61532401db2760f4aec7624acac5c1478f /ext/json/ext/generator/generator.c
parent334a4dd846ce8c93a5aea83cfe0b7ef42168de36 (diff)
parent34e97741d8ec6827d11e783842928e55dfce2abf (diff)
downloadjson-519437ab428d67f4b4105e2a94f2f2c1601fdcf5.tar.gz
Merge branch 'master' of gate.dyna.ping.de:/git/json
Diffstat (limited to 'ext/json/ext/generator/generator.c')
-rw-r--r--ext/json/ext/generator/generator.c20
1 files changed, 12 insertions, 8 deletions
diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c
index 21fef2b..cfd00b5 100644
--- a/ext/json/ext/generator/generator.c
+++ b/ext/json/ext/generator/generator.c
@@ -852,6 +852,16 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj)
return fbuffer_to_s(buffer);
}
+static int isArrayOrObject(VALUE string)
+{
+ 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 == '}';
+}
+
/*
* call-seq: generate(obj)
*
@@ -862,15 +872,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;
}