summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean byroot Boussier <jean.boussier+github@shopify.com>2024-04-17 03:29:09 +0200
committergit <svn-admin@ruby-lang.org>2024-04-17 01:29:18 +0000
commit75154dec73e1329693866e3a88cb9febb7635417 (patch)
treede4ed05b715f2ce51f322cf2fce13eacaa019b46
parentf34409bf8782481deabec6c577abd66373134af9 (diff)
[ruby/stringio] strio_read: preserve buffer encoding on partial
reads (https://github.com/ruby/stringio/pull/95) [[Bug #20418]](https://bugs.ruby-lang.org/issues/20418) Ruby IO#read preserves the encoding on partial read, but change it when reading the whole IO from commit https://github.com/ruby/ruby/commit/0ca7036682da: > * io.c (read_all): should associate default external encoding. > * io.c (io_read): should NOT associate default external encoding. https://github.com/ruby/stringio/commit/073172da31 Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
-rw-r--r--ext/stringio/stringio.c5
-rw-r--r--test/stringio/test_stringio.rb20
2 files changed, 18 insertions, 7 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index f0e5ee4e85..820b8228a3 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -1623,10 +1623,9 @@ strio_read(int argc, VALUE *argv, VALUE self)
if (len > rest) len = rest;
rb_str_resize(str, len);
MEMCPY(RSTRING_PTR(str), RSTRING_PTR(ptr->string) + ptr->pos, char, len);
- if (binary)
- rb_enc_associate(str, rb_ascii8bit_encoding());
- else
+ if (!binary) {
rb_enc_copy(str, ptr->string);
+ }
}
ptr->pos += RSTRING_LEN(str);
return str;
diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb
index e17cd0abb1..9031650581 100644
--- a/test/stringio/test_stringio.rb
+++ b/test/stringio/test_stringio.rb
@@ -700,6 +700,18 @@ class TestStringIO < Test::Unit::TestCase
s.force_encoding(Encoding::US_ASCII)
assert_same(s, f.read(nil, s))
assert_string("", Encoding::UTF_8, s, bug13806)
+
+ bug20418 = '[Bug #20418] ™€®'.b
+ f = StringIO.new(bug20418)
+ s = ""
+ assert_equal(Encoding::UTF_8, s.encoding, bug20418)
+ f.read(4, s)
+ assert_equal(Encoding::UTF_8, s.encoding, bug20418)
+
+ f.rewind
+ s = ""
+ f.read(nil, s)
+ assert_equal(Encoding::ASCII_8BIT, s.encoding, bug20418)
end
def test_readpartial
@@ -711,8 +723,8 @@ class TestStringIO < Test::Unit::TestCase
assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size))
f.rewind
# not empty buffer
- s = '0123456789'
- assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size, s))
+ s = '0123456789'.b
+ assert_equal("\u3042\u3044".b, f.readpartial(f.size, s))
end
def test_read_nonblock
@@ -736,8 +748,8 @@ class TestStringIO < Test::Unit::TestCase
assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size))
f.rewind
# not empty buffer
- s = '0123456789'
- assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size, s))
+ s = '0123456789'.b
+ assert_equal("\u3042\u3044".b, f.read_nonblock(f.size, s))
end
def test_sysread