From 9b9dafabad26a093dfadb970c303394f0197771c Mon Sep 17 00:00:00 2001 From: tompng Date: Mon, 15 Jan 2024 23:54:10 +0900 Subject: [PATCH] Fix ascii_only? flag in strio_write --- ext/stringio/stringio.c | 10 +--------- test/stringio/test_stringio.rb | 6 ++++++ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 4eb1511..27c7f65 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -915,9 +915,6 @@ strio_extend(struct StringIO *ptr, long pos, long len) if (pos > olen) MEMZERO(RSTRING_PTR(ptr->string) + olen, char, pos - olen); } - else { - rb_str_modify(ptr->string); - } } /* @@ -1464,14 +1461,9 @@ strio_write(VALUE self, VALUE str) } } else { - int cr0 = ENC_CODERANGE(ptr->string); - int cr = ENC_CODERANGE_UNKNOWN; - if (rb_enc_asciicompat(enc) && rb_enc_asciicompat(enc2)) { - cr = ENC_CODERANGE_AND(cr0, ENC_CODERANGE(str)); - } strio_extend(ptr, ptr->pos, len); + rb_str_modify(ptr->string); memmove(RSTRING_PTR(ptr->string)+ptr->pos, RSTRING_PTR(str), len); - if (cr != cr0) ENC_CODERANGE_SET(ptr->string, cr); } RB_GC_GUARD(str); ptr->pos += len; diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 15fcc21..8afbcf3 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -961,6 +961,12 @@ def test_coderange_after_overwrite assert_predicate(s.string, :ascii_only?) s.write "\u{431 43e 433 443 441}" assert_not_predicate(s.string, :ascii_only?) + + s = StringIO.new("\u{3042}") + s.rewind + assert_not_predicate(s.string, :ascii_only?) + s.write('aaaa') + assert_predicate(s.string, :ascii_only?) end def assert_string(content, encoding, str, mesg = nil)