@@ -237,6 +237,7 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
237
237
int escape_len ;
238
238
unsigned char c ;
239
239
char buf [6 ] = { '\\' , 'u' };
240
+ int ascii_only = rb_enc_str_asciionly_p (string );
240
241
241
242
for (start = 0 , end = 0 ; end < len ;) {
242
243
p = ptr + end ;
@@ -281,14 +282,17 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
281
282
break ;
282
283
default :
283
284
{
284
- unsigned short clen = trailingBytesForUTF8 [c ] + 1 ;
285
- if (end + clen > len ) {
286
- rb_raise (rb_path2class ("JSON::GeneratorError" ),
287
- "partial character in source, but hit end" );
288
- }
289
- if (!isLegalUTF8 ((UTF8 * ) p , clen )) {
290
- rb_raise (rb_path2class ("JSON::GeneratorError" ),
291
- "source sequence is illegal/malformed utf-8" );
285
+ unsigned short clen = 1 ;
286
+ if (!ascii_only ) {
287
+ clen += trailingBytesForUTF8 [c ];
288
+ if (end + clen > len ) {
289
+ rb_raise (rb_path2class ("JSON::GeneratorError" ),
290
+ "partial character in source, but hit end" );
291
+ }
292
+ if (!isLegalUTF8 ((UTF8 * ) p , clen )) {
293
+ rb_raise (rb_path2class ("JSON::GeneratorError" ),
294
+ "source sequence is illegal/malformed utf-8" );
295
+ }
292
296
}
293
297
end += clen ;
294
298
}
0 commit comments