summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2016-04-24 01:47:49 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2016-04-24 02:34:35 +0900
commitb94f1aa5d1708168ed2acdea0ee01269290666c5 (patch)
treeb6e487c9f0f54c0b732004335fee8be0573c7236
parentd69a661d217a4174538c11f7616472649429b92a (diff)
downloadjson-b94f1aa5d1708168ed2acdea0ee01269290666c5.tar.gz
Use rb_encoding
Use rb_encoding functions to convert encodings.
-rw-r--r--ext/json/ext/parser/parser.c162
-rw-r--r--ext/json/ext/parser/parser.rl16
2 files changed, 75 insertions, 103 deletions
diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c
index e933e4f..4bbd30c 100644
--- a/ext/json/ext/parser/parser.c
+++ b/ext/json/ext/parser/parser.c
@@ -67,14 +67,6 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
return len;
}
-#ifdef HAVE_RUBY_ENCODING_H
-static VALUE CEncoding_UTF_8;
-
-static ID i_encode;
-#else
-static ID i_iconv;
-#endif
-
static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
static VALUE CNaN, CInfinity, CMinusInfinity;
@@ -84,11 +76,11 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_match_string, i_aset, i_aref, i_leftshift;
-#line 110 "parser.rl"
+#line 102 "parser.rl"
-#line 92 "parser.c"
+#line 84 "parser.c"
enum {JSON_object_start = 1};
enum {JSON_object_first_final = 27};
enum {JSON_object_error = 0};
@@ -96,7 +88,7 @@ enum {JSON_object_error = 0};
enum {JSON_object_en_main = 1};
-#line 151 "parser.rl"
+#line 143 "parser.rl"
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -112,14 +104,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 116 "parser.c"
+#line 108 "parser.c"
{
cs = JSON_object_start;
}
-#line 166 "parser.rl"
+#line 158 "parser.rl"
-#line 123 "parser.c"
+#line 115 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -147,7 +139,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 133 "parser.rl"
+#line 125 "parser.rl"
{
char *np;
json->parsing_name = 1;
@@ -160,7 +152,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 164 "parser.c"
+#line 156 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -227,7 +219,7 @@ case 8:
goto st8;
goto st0;
tr11:
-#line 118 "parser.rl"
+#line 110 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v);
@@ -247,7 +239,7 @@ st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 251 "parser.c"
+#line 243 "parser.c"
switch( (*p) ) {
case 13: goto st9;
case 32: goto st9;
@@ -336,14 +328,14 @@ case 18:
goto st9;
goto st18;
tr4:
-#line 141 "parser.rl"
+#line 133 "parser.rl"
{ p--; {p++; cs = 27; goto _out;} }
goto st27;
st27:
if ( ++p == pe )
goto _test_eof27;
case 27:
-#line 347 "parser.c"
+#line 339 "parser.c"
goto st0;
st19:
if ( ++p == pe )
@@ -441,7 +433,7 @@ case 26:
_out: {}
}
-#line 167 "parser.rl"
+#line 159 "parser.rl"
if (cs >= JSON_object_first_final) {
if (json->create_additions) {
@@ -466,7 +458,7 @@ case 26:
-#line 470 "parser.c"
+#line 462 "parser.c"
enum {JSON_value_start = 1};
enum {JSON_value_first_final = 29};
enum {JSON_value_error = 0};
@@ -474,7 +466,7 @@ enum {JSON_value_error = 0};
enum {JSON_value_en_main = 1};
-#line 271 "parser.rl"
+#line 263 "parser.rl"
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -482,14 +474,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 486 "parser.c"
+#line 478 "parser.c"
{
cs = JSON_value_start;
}
-#line 278 "parser.rl"
+#line 270 "parser.rl"
-#line 493 "parser.c"
+#line 485 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -523,14 +515,14 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 219 "parser.rl"
+#line 211 "parser.rl"
{
char *np = JSON_parse_string(json, p, pe, result);
if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;}
}
goto st29;
tr3:
-#line 224 "parser.rl"
+#line 216 "parser.rl"
{
char *np;
if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) {
@@ -550,7 +542,7 @@ tr3:
}
goto st29;
tr7:
-#line 242 "parser.rl"
+#line 234 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -560,7 +552,7 @@ tr7:
}
goto st29;
tr11:
-#line 250 "parser.rl"
+#line 242 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -570,7 +562,7 @@ tr11:
}
goto st29;
tr25:
-#line 212 "parser.rl"
+#line 204 "parser.rl"
{
if (json->allow_nan) {
*result = CInfinity;
@@ -580,7 +572,7 @@ tr25:
}
goto st29;
tr27:
-#line 205 "parser.rl"
+#line 197 "parser.rl"
{
if (json->allow_nan) {
*result = CNaN;
@@ -590,19 +582,19 @@ tr27:
}
goto st29;
tr31:
-#line 199 "parser.rl"
+#line 191 "parser.rl"
{
*result = Qfalse;
}
goto st29;
tr34:
-#line 196 "parser.rl"
+#line 188 "parser.rl"
{
*result = Qnil;
}
goto st29;
tr37:
-#line 202 "parser.rl"
+#line 194 "parser.rl"
{
*result = Qtrue;
}
@@ -611,9 +603,9 @@ st29:
if ( ++p == pe )
goto _test_eof29;
case 29:
-#line 258 "parser.rl"
+#line 250 "parser.rl"
{ p--; {p++; cs = 29; goto _out;} }
-#line 617 "parser.c"
+#line 609 "parser.c"
switch( (*p) ) {
case 13: goto st29;
case 32: goto st29;
@@ -854,7 +846,7 @@ case 28:
_out: {}
}
-#line 279 "parser.rl"
+#line 271 "parser.rl"
if (cs >= JSON_value_first_final) {
return p;
@@ -864,7 +856,7 @@ case 28:
}
-#line 868 "parser.c"
+#line 860 "parser.c"
enum {JSON_integer_start = 1};
enum {JSON_integer_first_final = 3};
enum {JSON_integer_error = 0};
@@ -872,7 +864,7 @@ enum {JSON_integer_error = 0};
enum {JSON_integer_en_main = 1};
-#line 295 "parser.rl"
+#line 287 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -880,15 +872,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL;
-#line 884 "parser.c"
+#line 876 "parser.c"
{
cs = JSON_integer_start;
}
-#line 302 "parser.rl"
+#line 294 "parser.rl"
json->memo = p;
-#line 892 "parser.c"
+#line 884 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -922,14 +914,14 @@ case 3:
goto st0;
goto tr4;
tr4:
-#line 292 "parser.rl"
+#line 284 "parser.rl"
{ p--; {p++; cs = 4; goto _out;} }
goto st4;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
-#line 933 "parser.c"
+#line 925 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -948,7 +940,7 @@ case 5:
_out: {}
}
-#line 304 "parser.rl"
+#line 296 "parser.rl"
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
@@ -963,7 +955,7 @@ case 5:
}
-#line 967 "parser.c"
+#line 959 "parser.c"
enum {JSON_float_start = 1};
enum {JSON_float_first_final = 8};
enum {JSON_float_error = 0};
@@ -971,7 +963,7 @@ enum {JSON_float_error = 0};
enum {JSON_float_en_main = 1};
-#line 329 "parser.rl"
+#line 321 "parser.rl"
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -979,15 +971,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 983 "parser.c"
+#line 975 "parser.c"
{
cs = JSON_float_start;
}
-#line 336 "parser.rl"
+#line 328 "parser.rl"
json->memo = p;
-#line 991 "parser.c"
+#line 983 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1045,14 +1037,14 @@ case 8:
goto st0;
goto tr9;
tr9:
-#line 323 "parser.rl"
+#line 315 "parser.rl"
{ p--; {p++; cs = 9; goto _out;} }
goto st9;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 1056 "parser.c"
+#line 1048 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -1113,7 +1105,7 @@ case 7:
_out: {}
}
-#line 338 "parser.rl"
+#line 330 "parser.rl"
if (cs >= JSON_float_first_final) {
long len = p - json->memo;
@@ -1129,7 +1121,7 @@ case 7:
-#line 1133 "parser.c"
+#line 1125 "parser.c"
enum {JSON_array_start = 1};
enum {JSON_array_first_final = 17};
enum {JSON_array_error = 0};
@@ -1137,7 +1129,7 @@ enum {JSON_array_error = 0};
enum {JSON_array_en_main = 1};
-#line 381 "parser.rl"
+#line 373 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -1151,14 +1143,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 1155 "parser.c"
+#line 1147 "parser.c"
{
cs = JSON_array_start;
}
-#line 394 "parser.rl"
+#line 386 "parser.rl"
-#line 1162 "parser.c"
+#line 1154 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1197,7 +1189,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 358 "parser.rl"
+#line 350 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v);
@@ -1217,7 +1209,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 1221 "parser.c"
+#line 1213 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -1317,14 +1309,14 @@ case 12:
goto st3;
goto st12;
tr4:
-#line 373 "parser.rl"
+#line 365 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 1328 "parser.c"
+#line 1320 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@@ -1380,7 +1372,7 @@ case 16:
_out: {}
}
-#line 395 "parser.rl"
+#line 387 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
@@ -1461,7 +1453,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
}
-#line 1465 "parser.c"
+#line 1457 "parser.c"
enum {JSON_string_start = 1};
enum {JSON_string_first_final = 8};
enum {JSON_string_error = 0};
@@ -1469,7 +1461,7 @@ enum {JSON_string_error = 0};
enum {JSON_string_en_main = 1};
-#line 494 "parser.rl"
+#line 486 "parser.rl"
static int
@@ -1491,15 +1483,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = rb_str_buf_new(0);
-#line 1495 "parser.c"
+#line 1487 "parser.c"
{
cs = JSON_string_start;
}
-#line 515 "parser.rl"
+#line 507 "parser.rl"
json->memo = p;
-#line 1503 "parser.c"
+#line 1495 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1524,7 +1516,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 480 "parser.rl"
+#line 472 "parser.rl"
{
*result = json_string_unescape(*result, json->memo + 1, p);
if (NIL_P(*result)) {
@@ -1535,14 +1527,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
-#line 491 "parser.rl"
+#line 483 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1546 "parser.c"
+#line 1538 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1618,7 +1610,7 @@ case 7:
_out: {}
}
-#line 517 "parser.rl"
+#line 509 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
@@ -1657,7 +1649,7 @@ static VALUE convert_encoding(VALUE source)
{
#ifdef HAVE_RUBY_ENCODING_H
{
- source = rb_funcall(source, i_encode, 1, CEncoding_UTF_8);
+ source = rb_str_conv_enc(source, NULL, rb_utf8_encoding());
}
#endif
return source;
@@ -1783,7 +1775,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
-#line 1787 "parser.c"
+#line 1779 "parser.c"
enum {JSON_start = 1};
enum {JSON_first_final = 10};
enum {JSON_error = 0};
@@ -1791,7 +1783,7 @@ enum {JSON_error = 0};
enum {JSON_en_main = 1};
-#line 695 "parser.rl"
+#line 687 "parser.rl"
/*
@@ -1808,16 +1800,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER;
-#line 1812 "parser.c"
+#line 1804 "parser.c"
{
cs = JSON_start;
}
-#line 711 "parser.rl"
+#line 703 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1821 "parser.c"
+#line 1813 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1851,7 +1843,7 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 687 "parser.rl"
+#line 679 "parser.rl"
{
char *np = JSON_parse_value(json, p, pe, &result);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1861,7 +1853,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1865 "parser.c"
+#line 1857 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -1950,7 +1942,7 @@ case 9:
_out: {}
}
-#line 714 "parser.rl"
+#line 706 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
@@ -2048,12 +2040,6 @@ void Init_parser(void)
i_aset = rb_intern("[]=");
i_aref = rb_intern("[]");
i_leftshift = rb_intern("<<");
-#ifdef HAVE_RUBY_ENCODING_H
- CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
- i_encode = rb_intern("encode");
-#else
- i_iconv = rb_intern("iconv");
-#endif
}
/*
diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl
index b6ab64a..f35336c 100644
--- a/ext/json/ext/parser/parser.rl
+++ b/ext/json/ext/parser/parser.rl
@@ -65,14 +65,6 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
return len;
}
-#ifdef HAVE_RUBY_ENCODING_H
-static VALUE CEncoding_UTF_8;
-
-static ID i_encode;
-#else
-static ID i_iconv;
-#endif
-
static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
static VALUE CNaN, CInfinity, CMinusInfinity;
@@ -552,7 +544,7 @@ static VALUE convert_encoding(VALUE source)
{
#ifdef HAVE_RUBY_ENCODING_H
{
- source = rb_funcall(source, i_encode, 1, CEncoding_UTF_8);
+ source = rb_str_conv_enc(source, NULL, rb_utf8_encoding());
}
#endif
return source;
@@ -808,12 +800,6 @@ void Init_parser(void)
i_aset = rb_intern("[]=");
i_aref = rb_intern("[]");
i_leftshift = rb_intern("<<");
-#ifdef HAVE_RUBY_ENCODING_H
- CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
- i_encode = rb_intern("encode");
-#else
- i_iconv = rb_intern("iconv");
-#endif
}
/*