diff options
author | Nobuyoshi Nakada <[email protected]> | 2025-06-05 17:06:46 +0900 |
---|---|---|
committer | git <[email protected]> | 2025-06-05 08:06:51 +0000 |
commit | 8906d55cb58e0e4db5d17d6de25376c67ddd530f (patch) | |
tree | 67b8d883da5415b2ed9371dda3507b0e8e349e98 /ext/stringio/stringio.c | |
parent | 256440a82705ac53e5266b06428209ca375c72f8 (diff) |
[ruby/stringio] Extract internal part as the function
`str_chilled_p`
(https://github.com/ruby/stringio/pull/136)
https://github.com/ruby/stringio/commit/3c52ddc4c8
Diffstat (limited to 'ext/stringio/stringio.c')
-rw-r--r-- | ext/stringio/stringio.c | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 26287dd1b8..3003939e10 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -36,6 +36,19 @@ STRINGIO_VERSION = "3.1.8.dev"; # define rb_class_new_instance_kw(argc, argv, klass, kw_splat) rb_class_new_instance(argc, argv, klass) #endif +static inline bool +str_chilled_p(VALUE str) +{ +#if (RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR >= 4) || RUBY_API_VERSION_MAJOR >= 4 + // Do not attempt to modify chilled strings on Ruby 3.4+ + // RUBY_FL_USER2 == STR_CHILLED_LITERAL + // RUBY_FL_USER3 == STR_CHILLED_SYMBOL_TO_S + return FL_TEST_RAW(str, RUBY_FL_USER2 | RUBY_FL_USER3); +#else + return false; +#endif +} + #ifndef HAVE_TYPE_RB_IO_MODE_T typedef int rb_io_mode_t; #endif @@ -1865,14 +1878,7 @@ strio_set_encoding(int argc, VALUE *argv, VALUE self) } } ptr->enc = enc; - if (!NIL_P(ptr->string) && WRITABLE(self) -#if (RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR >= 4) || RUBY_API_VERSION_MAJOR >= 4 - // Do not attempt to modify chilled strings on Ruby 3.4+ - // RUBY_FL_USER2 == STR_CHILLED_LITERAL - // RUBY_FL_USER3 == STR_CHILLED_SYMBOL_TO_S - && !FL_TEST_RAW(ptr->string, RUBY_FL_USER2 | RUBY_FL_USER3) -#endif - ) { + if (!NIL_P(ptr->string) && WRITABLE(self) && !str_chilled_p(ptr->string)) { rb_enc_associate(ptr->string, enc); } |