[ruby/json] Sync changes

Some changes were missed in the automatic sync.
This commit is contained in:
Peter Zhu 2024-10-17 13:44:40 -04:00 committed by Jean Boussier
parent e4330536d2
commit 48899d56a9
Notes: git 2024-10-17 19:08:13 +00:00
2 changed files with 34 additions and 24 deletions

View File

@ -117,7 +117,29 @@ static void convert_UTF8_to_JSON(FBuffer *out_buffer, VALUE in_string, bool out_
RB_GC_GUARD(in_string);
}
static void convert_ASCII_to_JSON(FBuffer *out_buffer, VALUE str, bool out_script_safe)
static const bool escape_table[256] = {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* '"' */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0, /* '\\' */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
};
static const bool script_safe_escape_table[256] = {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* '"' and '/' */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0, /* '\\' */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
};
static void convert_ASCII_to_JSON(FBuffer *out_buffer, VALUE str, const bool escape_table[256])
{
const char *hexdig = "0123456789abcdef";
char scratch[12] = { '\\', 'u', 0, 0, 0, 0, '\\', 'u' };
@ -129,17 +151,8 @@ static void convert_ASCII_to_JSON(FBuffer *out_buffer, VALUE str, bool out_scrip
for (pos = 0; pos < len;) {
unsigned char ch = ptr[pos];
bool should_escape;
/* JSON policy */
should_escape =
(ch < 0x20) ||
(ch == '"') ||
(ch == '\\') ||
(out_script_safe && (ch == '/'));
/* JSON encoding */
if (should_escape) {
if (escape_table[ch]) {
if (pos > beg) {
fbuffer_append(out_buffer, &ptr[beg], pos - beg);
}
@ -717,7 +730,7 @@ static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
switch(rb_enc_str_coderange(obj)) {
case ENC_CODERANGE_7BIT:
convert_ASCII_to_JSON(buffer, obj, state->script_safe);
convert_ASCII_to_JSON(buffer, obj, state->script_safe ? script_safe_escape_table : escape_table);
break;
case ENC_CODERANGE_VALID:
if (RB_UNLIKELY(state->ascii_only)) {

View File

@ -1778,10 +1778,7 @@ static VALUE convert_encoding(VALUE source)
}
if (encindex == binary_encindex) {
if (OBJ_FROZEN(source)) {
source = rb_str_dup(source);
}
return rb_enc_associate_index(source, utf8_encindex);
return rb_enc_associate_index(rb_str_dup(source), utf8_encindex);
}
return rb_str_conv_enc(source, rb_enc_from_index(encindex), rb_utf8_encoding());
@ -1912,7 +1909,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
#line 1916 "parser.c"
#line 1913 "parser.c"
enum {JSON_start = 1};
enum {JSON_first_final = 10};
enum {JSON_error = 0};
@ -1920,7 +1917,7 @@ enum {JSON_error = 0};
enum {JSON_en_main = 1};
#line 824 "parser.rl"
#line 821 "parser.rl"
/*
@ -1938,16 +1935,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER;
#line 1942 "parser.c"
#line 1939 "parser.c"
{
cs = JSON_start;
}
#line 841 "parser.rl"
#line 838 "parser.rl"
p = json->source;
pe = p + json->len;
#line 1951 "parser.c"
#line 1948 "parser.c"
{
if ( p == pe )
goto _test_eof;
@ -1981,7 +1978,7 @@ st0:
cs = 0;
goto _out;
tr2:
#line 816 "parser.rl"
#line 813 "parser.rl"
{
char *np = JSON_parse_value(json, p, pe, &result, 0);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@ -1991,7 +1988,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
#line 1995 "parser.c"
#line 1992 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@ -2080,7 +2077,7 @@ case 9:
_out: {}
}
#line 844 "parser.rl"
#line 841 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;