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 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,
138
+ 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 ,
139
+ 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
140
+ };
141
+
142
+ static void convert_ASCII_to_JSON (FBuffer * out_buffer , VALUE str , const bool escape_table [256 ])
121
143
{
122
144
const char * hexdig = "0123456789abcdef" ;
123
145
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
129
151
130
152
for (pos = 0 ; pos < len ;) {
131
153
unsigned char ch = ptr [pos ];
132
- bool should_escape ;
133
-
134
- /* JSON policy */
135
- should_escape =
136
- (ch < 0x20 ) ||
137
- (ch == '"' ) ||
138
- (ch == '\\' ) ||
139
- (out_script_safe && (ch == '/' ));
140
-
141
154
/* JSON encoding */
142
- if (should_escape ) {
155
+ if (escape_table [ ch ] ) {
143
156
if (pos > beg ) {
144
157
fbuffer_append (out_buffer , & ptr [beg ], pos - beg );
145
158
}
@@ -717,7 +730,7 @@ static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
717
730
718
731
switch (rb_enc_str_coderange (obj )) {
719
732
case ENC_CODERANGE_7BIT :
720
- convert_ASCII_to_JSON (buffer , obj , state -> script_safe );
733
+ convert_ASCII_to_JSON (buffer , obj , state -> script_safe ? script_safe_escape_table : escape_table );
721
734
break ;
722
735
case ENC_CODERANGE_VALID :
723
736
if (RB_UNLIKELY (state -> ascii_only )) {
0 commit comments