1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[stringio] fix stringio codepoint enumerator off by one error

This commit is contained in:
Yoann Lecuyer 2020-08-26 22:46:58 +02:00 committed by Nobuyoshi Nakada
parent 96d701f737
commit b3c1c767ea
Notes: git 2020-08-27 23:41:46 +09:00
3 changed files with 24 additions and 1 deletions

View file

@ -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;
}

View file

@ -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]'

View file

@ -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))