[ruby/json] Get rid of some more outdated compatibility code

All these macros are available on Ruby 2.3+

https://github.com/ruby/json/commit/227885f460
This commit is contained in:
Jean Boussier 2024-10-17 14:55:18 +02:00 committed by git
parent 6105bae331
commit df48f597cf
4 changed files with 112 additions and 130 deletions

View File

@ -1,43 +1,8 @@
#ifndef _FBUFFER_H_ #ifndef _FBUFFER_H_
#define _FBUFFER_H_ #define _FBUFFER_H_
#include "ruby.h" #include "ruby.h"
#ifndef RHASH_SIZE
#define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries)
#endif
#ifndef RFLOAT_VALUE
#define RFLOAT_VALUE(val) (RFLOAT(val)->value)
#endif
#ifndef RARRAY_LEN
#define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
#endif
#ifndef RSTRING_PTR
#define RSTRING_PTR(string) RSTRING(string)->ptr
#endif
#ifndef RSTRING_LEN
#define RSTRING_LEN(string) RSTRING(string)->len
#endif
#ifdef PRIsVALUE
# define RB_OBJ_CLASSNAME(obj) rb_obj_class(obj)
# define RB_OBJ_STRING(obj) (obj)
#else
# define PRIsVALUE "s"
# define RB_OBJ_CLASSNAME(obj) rb_obj_classname(obj)
# define RB_OBJ_STRING(obj) StringValueCStr(obj)
#endif
#include "ruby/encoding.h" #include "ruby/encoding.h"
#define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
/* 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 { typedef struct FBufferStruct {
unsigned long initial_length; unsigned long initial_length;
@ -173,9 +138,8 @@ static void fbuffer_append_long(FBuffer *fb, long number)
static VALUE fbuffer_to_s(FBuffer *fb) static VALUE fbuffer_to_s(FBuffer *fb)
{ {
VALUE result = rb_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb)); VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
fbuffer_free(fb); fbuffer_free(fb);
FORCE_UTF8(result);
return result; return result;
} }
#endif #endif

View File

@ -620,9 +620,9 @@ static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
VALUE tmp = rb_funcall(obj, i_to_s, 0); VALUE tmp = rb_funcall(obj, i_to_s, 0);
if (!allow_nan) { if (!allow_nan) {
if (isinf(value)) { if (isinf(value)) {
rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", RB_OBJ_STRING(tmp)); rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", tmp);
} else if (isnan(value)) { } else if (isnan(value)) {
rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", RB_OBJ_STRING(tmp)); rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", tmp);
} }
} }
fbuffer_append_str(buffer, tmp); fbuffer_append_str(buffer, tmp);
@ -670,7 +670,7 @@ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *s
default: default:
general: general:
if (state->strict) { if (state->strict) {
rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", RB_OBJ_STRING(CLASS_OF(obj))); rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", CLASS_OF(obj));
} else if (rb_respond_to(obj, i_to_json)) { } else if (rb_respond_to(obj, i_to_json)) {
tmp = rb_funcall(obj, i_to_json, 1, Vstate); tmp = rb_funcall(obj, i_to_json, 1, Vstate);
Check_Type(tmp, T_STRING); Check_Type(tmp, T_STRING);

View File

@ -78,12 +78,16 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_deep_const_get, i_match, i_match_string, i_aset, i_aref, i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
i_leftshift, i_new, i_try_convert, i_freeze, i_uminus; i_leftshift, i_new, i_try_convert, i_freeze, i_uminus;
static int binary_encindex;
#line 105 "parser.rl" static int utf8_encindex;
#line 87 "parser.c" #line 109 "parser.rl"
#line 91 "parser.c"
enum {JSON_object_start = 1}; enum {JSON_object_start = 1};
enum {JSON_object_first_final = 27}; enum {JSON_object_first_final = 27};
enum {JSON_object_error = 0}; enum {JSON_object_error = 0};
@ -91,7 +95,7 @@ enum {JSON_object_error = 0};
enum {JSON_object_en_main = 1}; enum {JSON_object_en_main = 1};
#line 147 "parser.rl" #line 151 "parser.rl"
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting) static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@ -107,14 +111,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); *result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
#line 111 "parser.c" #line 115 "parser.c"
{ {
cs = JSON_object_start; cs = JSON_object_start;
} }
#line 162 "parser.rl" #line 166 "parser.rl"
#line 118 "parser.c" #line 122 "parser.c"
{ {
if ( p == pe ) if ( p == pe )
goto _test_eof; goto _test_eof;
@ -142,7 +146,7 @@ case 2:
goto st2; goto st2;
goto st0; goto st0;
tr2: tr2:
#line 129 "parser.rl" #line 133 "parser.rl"
{ {
char *np; char *np;
json->parsing_name = 1; json->parsing_name = 1;
@ -155,7 +159,7 @@ st3:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof3; goto _test_eof3;
case 3: case 3:
#line 159 "parser.c" #line 163 "parser.c"
switch( (*p) ) { switch( (*p) ) {
case 13: goto st3; case 13: goto st3;
case 32: goto st3; case 32: goto st3;
@ -222,7 +226,7 @@ case 8:
goto st8; goto st8;
goto st0; goto st0;
tr11: tr11:
#line 113 "parser.rl" #line 117 "parser.rl"
{ {
VALUE v = Qnil; VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v, current_nesting); char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
@ -243,7 +247,7 @@ st9:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof9; goto _test_eof9;
case 9: case 9:
#line 247 "parser.c" #line 251 "parser.c"
switch( (*p) ) { switch( (*p) ) {
case 13: goto st9; case 13: goto st9;
case 32: goto st9; case 32: goto st9;
@ -332,14 +336,14 @@ case 18:
goto st9; goto st9;
goto st18; goto st18;
tr4: tr4:
#line 137 "parser.rl" #line 141 "parser.rl"
{ p--; {p++; cs = 27; goto _out;} } { p--; {p++; cs = 27; goto _out;} }
goto st27; goto st27;
st27: st27:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof27; goto _test_eof27;
case 27: case 27:
#line 343 "parser.c" #line 347 "parser.c"
goto st0; goto st0;
st19: st19:
if ( ++p == pe ) if ( ++p == pe )
@ -437,7 +441,7 @@ case 26:
_out: {} _out: {}
} }
#line 163 "parser.rl" #line 167 "parser.rl"
if (cs >= JSON_object_first_final) { if (cs >= JSON_object_first_final) {
if (json->create_additions) { if (json->create_additions) {
@ -462,7 +466,7 @@ case 26:
#line 466 "parser.c" #line 470 "parser.c"
enum {JSON_value_start = 1}; enum {JSON_value_start = 1};
enum {JSON_value_first_final = 29}; enum {JSON_value_first_final = 29};
enum {JSON_value_error = 0}; enum {JSON_value_error = 0};
@ -470,7 +474,7 @@ enum {JSON_value_error = 0};
enum {JSON_value_en_main = 1}; enum {JSON_value_en_main = 1};
#line 263 "parser.rl" #line 267 "parser.rl"
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting) static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@ -478,14 +482,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL; int cs = EVIL;
#line 482 "parser.c" #line 486 "parser.c"
{ {
cs = JSON_value_start; cs = JSON_value_start;
} }
#line 270 "parser.rl" #line 274 "parser.rl"
#line 489 "parser.c" #line 493 "parser.c"
{ {
if ( p == pe ) if ( p == pe )
goto _test_eof; goto _test_eof;
@ -519,14 +523,14 @@ st0:
cs = 0; cs = 0;
goto _out; goto _out;
tr2: tr2:
#line 215 "parser.rl" #line 219 "parser.rl"
{ {
char *np = JSON_parse_string(json, p, pe, result); char *np = JSON_parse_string(json, p, pe, result);
if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;} if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;}
} }
goto st29; goto st29;
tr3: tr3:
#line 220 "parser.rl" #line 224 "parser.rl"
{ {
char *np; char *np;
if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) { if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) {
@ -546,7 +550,7 @@ tr3:
} }
goto st29; goto st29;
tr7: tr7:
#line 238 "parser.rl" #line 242 "parser.rl"
{ {
char *np; char *np;
np = JSON_parse_array(json, p, pe, result, current_nesting + 1); np = JSON_parse_array(json, p, pe, result, current_nesting + 1);
@ -554,7 +558,7 @@ tr7:
} }
goto st29; goto st29;
tr11: tr11:
#line 244 "parser.rl" #line 248 "parser.rl"
{ {
char *np; char *np;
np = JSON_parse_object(json, p, pe, result, current_nesting + 1); np = JSON_parse_object(json, p, pe, result, current_nesting + 1);
@ -562,7 +566,7 @@ tr11:
} }
goto st29; goto st29;
tr25: tr25:
#line 208 "parser.rl" #line 212 "parser.rl"
{ {
if (json->allow_nan) { if (json->allow_nan) {
*result = CInfinity; *result = CInfinity;
@ -572,7 +576,7 @@ tr25:
} }
goto st29; goto st29;
tr27: tr27:
#line 201 "parser.rl" #line 205 "parser.rl"
{ {
if (json->allow_nan) { if (json->allow_nan) {
*result = CNaN; *result = CNaN;
@ -582,19 +586,19 @@ tr27:
} }
goto st29; goto st29;
tr31: tr31:
#line 195 "parser.rl" #line 199 "parser.rl"
{ {
*result = Qfalse; *result = Qfalse;
} }
goto st29; goto st29;
tr34: tr34:
#line 192 "parser.rl" #line 196 "parser.rl"
{ {
*result = Qnil; *result = Qnil;
} }
goto st29; goto st29;
tr37: tr37:
#line 198 "parser.rl" #line 202 "parser.rl"
{ {
*result = Qtrue; *result = Qtrue;
} }
@ -603,9 +607,9 @@ st29:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof29; goto _test_eof29;
case 29: case 29:
#line 250 "parser.rl" #line 254 "parser.rl"
{ p--; {p++; cs = 29; goto _out;} } { p--; {p++; cs = 29; goto _out;} }
#line 609 "parser.c" #line 613 "parser.c"
switch( (*p) ) { switch( (*p) ) {
case 13: goto st29; case 13: goto st29;
case 32: goto st29; case 32: goto st29;
@ -846,7 +850,7 @@ case 28:
_out: {} _out: {}
} }
#line 271 "parser.rl" #line 275 "parser.rl"
if (json->freeze) { if (json->freeze) {
OBJ_FREEZE(*result); OBJ_FREEZE(*result);
@ -860,7 +864,7 @@ case 28:
} }
#line 864 "parser.c" #line 868 "parser.c"
enum {JSON_integer_start = 1}; enum {JSON_integer_start = 1};
enum {JSON_integer_first_final = 3}; enum {JSON_integer_first_final = 3};
enum {JSON_integer_error = 0}; enum {JSON_integer_error = 0};
@ -868,7 +872,7 @@ enum {JSON_integer_error = 0};
enum {JSON_integer_en_main = 1}; enum {JSON_integer_en_main = 1};
#line 291 "parser.rl" #line 295 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result) static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@ -876,15 +880,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL; int cs = EVIL;
#line 880 "parser.c" #line 884 "parser.c"
{ {
cs = JSON_integer_start; cs = JSON_integer_start;
} }
#line 298 "parser.rl" #line 302 "parser.rl"
json->memo = p; json->memo = p;
#line 888 "parser.c" #line 892 "parser.c"
{ {
if ( p == pe ) if ( p == pe )
goto _test_eof; goto _test_eof;
@ -918,14 +922,14 @@ case 3:
goto st0; goto st0;
goto tr4; goto tr4;
tr4: tr4:
#line 288 "parser.rl" #line 292 "parser.rl"
{ p--; {p++; cs = 4; goto _out;} } { p--; {p++; cs = 4; goto _out;} }
goto st4; goto st4;
st4: st4:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof4; goto _test_eof4;
case 4: case 4:
#line 929 "parser.c" #line 933 "parser.c"
goto st0; goto st0;
st5: st5:
if ( ++p == pe ) if ( ++p == pe )
@ -944,7 +948,7 @@ case 5:
_out: {} _out: {}
} }
#line 300 "parser.rl" #line 304 "parser.rl"
if (cs >= JSON_integer_first_final) { if (cs >= JSON_integer_first_final) {
long len = p - json->memo; long len = p - json->memo;
@ -959,7 +963,7 @@ case 5:
} }
#line 963 "parser.c" #line 967 "parser.c"
enum {JSON_float_start = 1}; enum {JSON_float_start = 1};
enum {JSON_float_first_final = 8}; enum {JSON_float_first_final = 8};
enum {JSON_float_error = 0}; enum {JSON_float_error = 0};
@ -967,7 +971,7 @@ enum {JSON_float_error = 0};
enum {JSON_float_en_main = 1}; enum {JSON_float_en_main = 1};
#line 325 "parser.rl" #line 329 "parser.rl"
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result) static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
@ -975,15 +979,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL; int cs = EVIL;
#line 979 "parser.c" #line 983 "parser.c"
{ {
cs = JSON_float_start; cs = JSON_float_start;
} }
#line 332 "parser.rl" #line 336 "parser.rl"
json->memo = p; json->memo = p;
#line 987 "parser.c" #line 991 "parser.c"
{ {
if ( p == pe ) if ( p == pe )
goto _test_eof; goto _test_eof;
@ -1041,14 +1045,14 @@ case 8:
goto st0; goto st0;
goto tr9; goto tr9;
tr9: tr9:
#line 319 "parser.rl" #line 323 "parser.rl"
{ p--; {p++; cs = 9; goto _out;} } { p--; {p++; cs = 9; goto _out;} }
goto st9; goto st9;
st9: st9:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof9; goto _test_eof9;
case 9: case 9:
#line 1052 "parser.c" #line 1056 "parser.c"
goto st0; goto st0;
st5: st5:
if ( ++p == pe ) if ( ++p == pe )
@ -1109,7 +1113,7 @@ case 7:
_out: {} _out: {}
} }
#line 334 "parser.rl" #line 338 "parser.rl"
if (cs >= JSON_float_first_final) { if (cs >= JSON_float_first_final) {
VALUE mod = Qnil; VALUE mod = Qnil;
@ -1160,7 +1164,7 @@ case 7:
#line 1164 "parser.c" #line 1168 "parser.c"
enum {JSON_array_start = 1}; enum {JSON_array_start = 1};
enum {JSON_array_first_final = 17}; enum {JSON_array_first_final = 17};
enum {JSON_array_error = 0}; enum {JSON_array_error = 0};
@ -1168,7 +1172,7 @@ enum {JSON_array_error = 0};
enum {JSON_array_en_main = 1}; enum {JSON_array_en_main = 1};
#line 412 "parser.rl" #line 416 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting) static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@ -1182,14 +1186,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); *result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
#line 1186 "parser.c" #line 1190 "parser.c"
{ {
cs = JSON_array_start; cs = JSON_array_start;
} }
#line 425 "parser.rl" #line 429 "parser.rl"
#line 1193 "parser.c" #line 1197 "parser.c"
{ {
if ( p == pe ) if ( p == pe )
goto _test_eof; goto _test_eof;
@ -1228,7 +1232,7 @@ case 2:
goto st2; goto st2;
goto st0; goto st0;
tr2: tr2:
#line 389 "parser.rl" #line 393 "parser.rl"
{ {
VALUE v = Qnil; VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v, current_nesting); char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
@ -1248,7 +1252,7 @@ st3:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof3; goto _test_eof3;
case 3: case 3:
#line 1252 "parser.c" #line 1256 "parser.c"
switch( (*p) ) { switch( (*p) ) {
case 13: goto st3; case 13: goto st3;
case 32: goto st3; case 32: goto st3;
@ -1348,14 +1352,14 @@ case 12:
goto st3; goto st3;
goto st12; goto st12;
tr4: tr4:
#line 404 "parser.rl" #line 408 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} } { p--; {p++; cs = 17; goto _out;} }
goto st17; goto st17;
st17: st17:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof17; goto _test_eof17;
case 17: case 17:
#line 1359 "parser.c" #line 1363 "parser.c"
goto st0; goto st0;
st13: st13:
if ( ++p == pe ) if ( ++p == pe )
@ -1411,7 +1415,7 @@ case 16:
_out: {} _out: {}
} }
#line 426 "parser.rl" #line 430 "parser.rl"
if(cs >= JSON_array_first_final) { if(cs >= JSON_array_first_final) {
return p + 1; return p + 1;
@ -1578,7 +1582,7 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
} }
#line 1582 "parser.c" #line 1586 "parser.c"
enum {JSON_string_start = 1}; enum {JSON_string_start = 1};
enum {JSON_string_first_final = 8}; enum {JSON_string_first_final = 8};
enum {JSON_string_error = 0}; enum {JSON_string_error = 0};
@ -1586,7 +1590,7 @@ enum {JSON_string_error = 0};
enum {JSON_string_en_main = 1}; enum {JSON_string_en_main = 1};
#line 610 "parser.rl" #line 614 "parser.rl"
static int static int
@ -1607,15 +1611,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
VALUE match_string; VALUE match_string;
#line 1611 "parser.c" #line 1615 "parser.c"
{ {
cs = JSON_string_start; cs = JSON_string_start;
} }
#line 630 "parser.rl" #line 634 "parser.rl"
json->memo = p; json->memo = p;
#line 1619 "parser.c" #line 1623 "parser.c"
{ {
if ( p == pe ) if ( p == pe )
goto _test_eof; goto _test_eof;
@ -1640,7 +1644,7 @@ case 2:
goto st0; goto st0;
goto st2; goto st2;
tr2: tr2:
#line 597 "parser.rl" #line 601 "parser.rl"
{ {
*result = json_string_unescape(json->memo + 1, p, json->parsing_name || json-> freeze, json->parsing_name && json->symbolize_names); *result = json_string_unescape(json->memo + 1, p, json->parsing_name || json-> freeze, json->parsing_name && json->symbolize_names);
if (NIL_P(*result)) { if (NIL_P(*result)) {
@ -1650,14 +1654,14 @@ tr2:
{p = (( p + 1))-1;} {p = (( p + 1))-1;}
} }
} }
#line 607 "parser.rl" #line 611 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} } { p--; {p++; cs = 8; goto _out;} }
goto st8; goto st8;
st8: st8:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof8; goto _test_eof8;
case 8: case 8:
#line 1661 "parser.c" #line 1665 "parser.c"
goto st0; goto st0;
st3: st3:
if ( ++p == pe ) if ( ++p == pe )
@ -1733,7 +1737,7 @@ case 7:
_out: {} _out: {}
} }
#line 632 "parser.rl" #line 636 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) { if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass; VALUE klass;
@ -1767,18 +1771,20 @@ case 7:
static VALUE convert_encoding(VALUE source) static VALUE convert_encoding(VALUE source)
{ {
#ifdef HAVE_RUBY_ENCODING_H int encindex = RB_ENCODING_GET(source);
rb_encoding *enc = rb_enc_get(source);
if (enc == rb_ascii8bit_encoding()) { if (encindex == utf8_encindex) {
return source;
}
if (encindex == binary_encindex) {
if (OBJ_FROZEN(source)) { if (OBJ_FROZEN(source)) {
source = rb_str_dup(source); source = rb_str_dup(source);
} }
FORCE_UTF8(source); return rb_enc_associate_index(source, utf8_encindex);
} else {
source = rb_str_conv_enc(source, rb_enc_get(source), rb_utf8_encoding());
} }
#endif
return source; return rb_str_conv_enc(source, rb_enc_from_index(encindex), rb_utf8_encoding());
} }
/* /*
@ -1906,7 +1912,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
} }
#line 1910 "parser.c" #line 1916 "parser.c"
enum {JSON_start = 1}; enum {JSON_start = 1};
enum {JSON_first_final = 10}; enum {JSON_first_final = 10};
enum {JSON_error = 0}; enum {JSON_error = 0};
@ -1914,7 +1920,7 @@ enum {JSON_error = 0};
enum {JSON_en_main = 1}; enum {JSON_en_main = 1};
#line 818 "parser.rl" #line 824 "parser.rl"
/* /*
@ -1932,16 +1938,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER; GET_PARSER;
#line 1936 "parser.c" #line 1942 "parser.c"
{ {
cs = JSON_start; cs = JSON_start;
} }
#line 835 "parser.rl" #line 841 "parser.rl"
p = json->source; p = json->source;
pe = p + json->len; pe = p + json->len;
#line 1945 "parser.c" #line 1951 "parser.c"
{ {
if ( p == pe ) if ( p == pe )
goto _test_eof; goto _test_eof;
@ -1975,7 +1981,7 @@ st0:
cs = 0; cs = 0;
goto _out; goto _out;
tr2: tr2:
#line 810 "parser.rl" #line 816 "parser.rl"
{ {
char *np = JSON_parse_value(json, p, pe, &result, 0); char *np = JSON_parse_value(json, p, pe, &result, 0);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;} if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@ -1985,7 +1991,7 @@ st10:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof10; goto _test_eof10;
case 10: case 10:
#line 1989 "parser.c" #line 1995 "parser.c"
switch( (*p) ) { switch( (*p) ) {
case 13: goto st10; case 13: goto st10;
case 32: goto st10; case 32: goto st10;
@ -2074,7 +2080,7 @@ case 9:
_out: {} _out: {}
} }
#line 838 "parser.rl" #line 844 "parser.rl"
if (cs >= JSON_first_final && p == pe) { if (cs >= JSON_first_final && p == pe) {
return result; return result;
@ -2186,6 +2192,9 @@ void Init_parser(void)
i_try_convert = rb_intern("try_convert"); i_try_convert = rb_intern("try_convert");
i_freeze = rb_intern("freeze"); i_freeze = rb_intern("freeze");
i_uminus = rb_intern("-@"); i_uminus = rb_intern("-@");
binary_encindex = rb_ascii8bit_encindex();
utf8_encindex = rb_utf8_encindex();
} }
/* /*

View File

@ -76,6 +76,10 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_deep_const_get, i_match, i_match_string, i_aset, i_aref, i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
i_leftshift, i_new, i_try_convert, i_freeze, i_uminus; i_leftshift, i_new, i_try_convert, i_freeze, i_uminus;
static int binary_encindex;
static int utf8_encindex;
%%{ %%{
machine JSON_common; machine JSON_common;
@ -662,18 +666,20 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
static VALUE convert_encoding(VALUE source) static VALUE convert_encoding(VALUE source)
{ {
#ifdef HAVE_RUBY_ENCODING_H int encindex = RB_ENCODING_GET(source);
rb_encoding *enc = rb_enc_get(source);
if (enc == rb_ascii8bit_encoding()) { if (encindex == utf8_encindex) {
return source;
}
if (encindex == binary_encindex) {
if (OBJ_FROZEN(source)) { if (OBJ_FROZEN(source)) {
source = rb_str_dup(source); source = rb_str_dup(source);
} }
FORCE_UTF8(source); return rb_enc_associate_index(source, utf8_encindex);
} else {
source = rb_str_conv_enc(source, rb_enc_get(source), rb_utf8_encoding());
} }
#endif
return source; return rb_str_conv_enc(source, rb_enc_from_index(encindex), rb_utf8_encoding());
} }
/* /*
@ -946,6 +952,9 @@ void Init_parser(void)
i_try_convert = rb_intern("try_convert"); i_try_convert = rb_intern("try_convert");
i_freeze = rb_intern("freeze"); i_freeze = rb_intern("freeze");
i_uminus = rb_intern("-@"); i_uminus = rb_intern("-@");
binary_encindex = rb_ascii8bit_encindex();
utf8_encindex = rb_utf8_encindex();
} }
/* /*