Skip to content

Commit 4ef7a45

Browse files
samyronbyroot
authored andcommitted
Use RB_ENC_CODERANGE to first check the cached coderange before calling rb_enc_str_coderange if the coderange is unknown.
1 parent 7dd6b63 commit 4ef7a45

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

ext/json/ext/generator/generator.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,9 +819,17 @@ static VALUE encode_json_string_rescue(VALUE str, VALUE exception)
819819
return Qundef;
820820
}
821821

822+
static inline int json_str_coderange(VALUE str) {
823+
int coderange = RB_ENC_CODERANGE(str);
824+
if (coderange == RUBY_ENC_CODERANGE_UNKNOWN) {
825+
coderange = rb_enc_str_coderange(str);
826+
}
827+
return coderange;
828+
}
829+
822830
static inline bool valid_json_string_p(VALUE str)
823831
{
824-
int coderange = rb_enc_str_coderange(str);
832+
int coderange = json_str_coderange(str);
825833

826834
if (RB_LIKELY(coderange == ENC_CODERANGE_7BIT)) {
827835
return true;
@@ -893,7 +901,7 @@ static void raw_generate_json_string(FBuffer *buffer, struct generate_json_data
893901
search.chunk_end = NULL;
894902
#endif /* HAVE_SIMD */
895903

896-
switch (rb_enc_str_coderange(obj)) {
904+
switch (json_str_coderange(obj)) {
897905
case ENC_CODERANGE_7BIT:
898906
case ENC_CODERANGE_VALID:
899907
if (RB_UNLIKELY(data->state->ascii_only)) {

0 commit comments

Comments
 (0)