summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/stringio/stringio.c2
-rw-r--r--test/ruby/test_io.rb13
-rw-r--r--test/stringio/test_stringio.rb10
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))