@@ -89,14 +89,14 @@ static void raise_parse_error(const char *format, const char *start)
89
89
rb_enc_raise (rb_utf8_encoding (), rb_path2class ("JSON::ParserError" ), format , ptr );
90
90
}
91
91
92
- static VALUE mJSON , mExt , cParser , eNestingError ;
92
+ static VALUE mJSON , mExt , cParser , eNestingError , Encoding_UTF_8 ;
93
93
static VALUE CNaN , CInfinity , CMinusInfinity ;
94
94
95
95
static ID i_json_creatable_p , i_json_create , i_create_id , i_create_additions ,
96
96
i_chr , i_max_nesting , i_allow_nan , i_symbolize_names ,
97
97
i_object_class , i_array_class , i_decimal_class ,
98
98
i_deep_const_get , i_match , i_match_string , i_aset , i_aref ,
99
- i_leftshift , i_new , i_try_convert , i_freeze , i_uminus ;
99
+ i_leftshift , i_new , i_try_convert , i_freeze , i_uminus , i_encode ;
100
100
101
101
static int binary_encindex ;
102
102
static int utf8_encindex ;
@@ -1797,16 +1797,11 @@ static VALUE convert_encoding(VALUE source)
1797
1797
}
1798
1798
1799
1799
if (encindex == binary_encindex ) {
1800
- // For historical reason, we silently reinterpret binary strings as UTF-8 if it would work.
1801
- VALUE utf8_string = rb_enc_associate_index (rb_str_dup (source ), utf8_encindex );
1802
- switch (rb_enc_str_coderange (utf8_string )) {
1803
- case ENC_CODERANGE_7BIT :
1804
- case ENC_CODERANGE_VALID :
1805
- return utf8_string ;
1806
- }
1800
+ // For historical reason, we silently reinterpret binary strings as UTF-8
1801
+ return rb_enc_associate_index (rb_str_dup (source ), utf8_encindex );
1807
1802
}
1808
1803
1809
- return rb_str_conv_enc (source , rb_enc_from_index ( encindex ), rb_utf8_encoding () );
1804
+ return rb_funcall (source , i_encode , 1 , Encoding_UTF_8 );
1810
1805
}
1811
1806
1812
1807
/*
@@ -1958,15 +1953,15 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1958
1953
}
1959
1954
1960
1955
1961
- #line 1962 "parser.c"
1956
+ #line 1957 "parser.c"
1962
1957
enum {JSON_start = 1 };
1963
1958
enum {JSON_first_final = 10 };
1964
1959
enum {JSON_error = 0 };
1965
1960
1966
1961
enum {JSON_en_main = 1 };
1967
1962
1968
1963
1969
- #line 870 "parser.rl"
1964
+ #line 865 "parser.rl"
1970
1965
1971
1966
1972
1967
/*
@@ -1984,16 +1979,16 @@ static VALUE cParser_parse(VALUE self)
1984
1979
GET_PARSER ;
1985
1980
1986
1981
1987
- #line 1988 "parser.c"
1982
+ #line 1983 "parser.c"
1988
1983
{
1989
1984
cs = JSON_start ;
1990
1985
}
1991
1986
1992
- #line 887 "parser.rl"
1987
+ #line 882 "parser.rl"
1993
1988
p = json -> source ;
1994
1989
pe = p + json -> len ;
1995
1990
1996
- #line 1997 "parser.c"
1991
+ #line 1992 "parser.c"
1997
1992
{
1998
1993
if ( p == pe )
1999
1994
goto _test_eof ;
@@ -2027,7 +2022,7 @@ case 1:
2027
2022
cs = 0 ;
2028
2023
goto _out ;
2029
2024
tr2 :
2030
- #line 862 "parser.rl"
2025
+ #line 857 "parser.rl"
2031
2026
{
2032
2027
char * np = JSON_parse_value (json , p , pe , & result , 0 );
2033
2028
if (np == NULL ) { p -- ; {p ++ ; cs = 10 ; goto _out ;} } else {p = (( np ))- 1 ;}
@@ -2037,7 +2032,7 @@ cs = 0;
2037
2032
if ( ++ p == pe )
2038
2033
goto _test_eof10 ;
2039
2034
case 10 :
2040
- #line 2041 "parser.c"
2035
+ #line 2036 "parser.c"
2041
2036
switch ( (* p ) ) {
2042
2037
case 13 : goto st10 ;
2043
2038
case 32 : goto st10 ;
@@ -2126,7 +2121,7 @@ case 9:
2126
2121
_out : {}
2127
2122
}
2128
2123
2129
- #line 890 "parser.rl"
2124
+ #line 885 "parser.rl"
2130
2125
2131
2126
if (cs >= JSON_first_final && p == pe ) {
2132
2127
return result ;
@@ -2214,6 +2209,9 @@ void Init_parser(void)
2214
2209
CMinusInfinity = rb_const_get (mJSON , rb_intern ("MinusInfinity" ));
2215
2210
rb_gc_register_mark_object (CMinusInfinity );
2216
2211
2212
+ rb_global_variable (& Encoding_UTF_8 );
2213
+ Encoding_UTF_8 = rb_const_get (rb_path2class ("Encoding" ), rb_intern ("UTF_8" ));
2214
+
2217
2215
i_json_creatable_p = rb_intern ("json_creatable?" );
2218
2216
i_json_create = rb_intern ("json_create" );
2219
2217
i_create_id = rb_intern ("create_id" );
@@ -2235,6 +2233,7 @@ void Init_parser(void)
2235
2233
i_try_convert = rb_intern ("try_convert" );
2236
2234
i_freeze = rb_intern ("freeze" );
2237
2235
i_uminus = rb_intern ("-@" );
2236
+ i_encode = rb_intern ("encode" );
2238
2237
2239
2238
binary_encindex = rb_ascii8bit_encindex ();
2240
2239
utf8_encindex = rb_utf8_encindex ();
0 commit comments