Skip to content

Commit

Permalink
[stringio] fix stringio codepoint enumerator off by one error
Browse files Browse the repository at this point in the history
  • Loading branch information
ylecuyer authored and nobu committed Aug 27, 2020
1 parent 96d701f commit b3c1c76
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ext/stringio/stringio.c
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions test/ruby/test_io.rb
Expand Up @@ -405,6 +405,19 @@ def test_each_codepoint
}
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]'
Expand Down
10 changes: 10 additions & 0 deletions test/stringio/test_stringio.rb
Expand Up @@ -525,6 +525,16 @@ def test_each_codepoint
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))
Expand Down

0 comments on commit b3c1c76

Please sign in to comment.