Skip to content

Commit 3c52ddc

Browse files
authored
Extract internal part as the function str_chilled_p (#136)
1 parent 6b1fc8a commit 3c52ddc

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

ext/stringio/stringio.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ STRINGIO_VERSION = "3.1.8";
3636
# define rb_class_new_instance_kw(argc, argv, klass, kw_splat) rb_class_new_instance(argc, argv, klass)
3737
#endif
3838

39+
static inline bool
40+
str_chilled_p(VALUE str)
41+
{
42+
#if (RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR >= 4) || RUBY_API_VERSION_MAJOR >= 4
43+
// Do not attempt to modify chilled strings on Ruby 3.4+
44+
// RUBY_FL_USER2 == STR_CHILLED_LITERAL
45+
// RUBY_FL_USER3 == STR_CHILLED_SYMBOL_TO_S
46+
return FL_TEST_RAW(str, RUBY_FL_USER2 | RUBY_FL_USER3);
47+
#else
48+
return false;
49+
#endif
50+
}
51+
3952
#ifndef HAVE_TYPE_RB_IO_MODE_T
4053
typedef int rb_io_mode_t;
4154
#endif
@@ -1865,14 +1878,7 @@ strio_set_encoding(int argc, VALUE *argv, VALUE self)
18651878
}
18661879
}
18671880
ptr->enc = enc;
1868-
if (!NIL_P(ptr->string) && WRITABLE(self)
1869-
#if (RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR >= 4) || RUBY_API_VERSION_MAJOR >= 4
1870-
// Do not attempt to modify chilled strings on Ruby 3.4+
1871-
// RUBY_FL_USER2 == STR_CHILLED_LITERAL
1872-
// RUBY_FL_USER3 == STR_CHILLED_SYMBOL_TO_S
1873-
&& !FL_TEST_RAW(ptr->string, RUBY_FL_USER2 | RUBY_FL_USER3)
1874-
#endif
1875-
) {
1881+
if (!NIL_P(ptr->string) && WRITABLE(self) && !str_chilled_p(ptr->string)) {
18761882
rb_enc_associate(ptr->string, enc);
18771883
}
18781884

0 commit comments

Comments
 (0)