diff options
author | Yoann Lecuyer <yoann.lecuyer@gmail.com> | 2020-08-26 22:46:58 +0200 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2020-08-27 23:41:22 +0900 |
commit | b3c1c767ea64ce435c115cece5074274b7aefadc (patch) | |
tree | 03b1c4cfa9f426df1bac008556f96952afe3ce8a | |
parent | 96d701f73780638ffd3c8fa91c43f33f1a207d65 (diff) | |
download | ruby-b3c1c767ea64ce435c115cece5074274b7aefadc.tar.gz |
[stringio] fix stringio codepoint enumerator off by one error
-rw-r--r-- | ext/stringio/stringio.c | 2 | ||||
-rw-r--r-- | test/ruby/test_io.rb | 13 | ||||
-rw-r--r-- | test/stringio/test_stringio.rb | 10 |
3 files changed, 24 insertions, 1 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 01309decc9..357e4f12c1 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -1095,8 +1095,8 @@ strio_each_codepoint(VALUE self) c = rb_enc_codepoint_len(RSTRING_PTR(ptr->string)+ptr->pos, RSTRING_END(ptr->string), &n, enc); - rb_yield(UINT2NUM(c)); ptr->pos += n; + rb_yield(UINT2NUM(c)); } return self; } diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 82f122489a..fafb082154 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -405,6 +405,19 @@ class TestIO < Test::Unit::TestCase } end + def test_each_codepoint_enumerator + make_tempfile {|t| + a = "" + b = "" + File.open(t, 'rt') {|f| + a = f.each_codepoint.take(4).pack('U*') + b = f.read(8) + } + assert_equal("foo\n", a) + assert_equal("bar\nbaz\n", b) + } + end + def test_codepoints make_tempfile {|t| bug2959 = '[ruby-core:28650]' diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 54f57165a4..a49326119f 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -525,6 +525,16 @@ class TestStringIO < Test::Unit::TestCase assert_equal([49, 50, 51, 52], f.each_codepoint.to_a) end + def test_each_codepoint_enumerator + io = StringIO.new('你好построить') + + chinese_part = io.each_codepoint.take(2).pack('U*') + russian_part = io.read(40).force_encoding('UTF-8') + + assert_equal("你好", chinese_part) + assert_equal("построить", russian_part) + end + def test_gets2 f = StringIO.new("foo\nbar\nbaz\n") assert_equal("fo", f.gets(2)) |