From 32ed00ab188cf46d0d4bae116a48dac91731af32 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 14 Mar 2010 19:41:32 +0000 Subject: [PATCH] * io.c (rb_io_each_codepoint): read directly when readconv is needed but internal encoding is not set. [ruby-core:28650] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26926 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ io.c | 11 ++++++++--- test/ruby/test_io.rb | 10 ++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index e97f836499..72f4a52ee5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Mar 15 04:41:25 2010 Nobuyoshi Nakada + + * io.c (rb_io_each_codepoint): read directly when readconv is + needed but internal encoding is not set. [ruby-core:28650] + Mon Mar 15 04:18:31 2010 Nobuyoshi Nakada * tool/file2lastrev.rb (VCS::{SVN,GIT}#get_revisions): diff --git a/io.c b/io.c index dbe39cd95b..6fd9f54d10 100644 --- a/io.c +++ b/io.c @@ -2895,9 +2895,14 @@ rb_io_each_codepoint(VALUE io) rb_enc_name(fptr->encs.enc)); } n = MBCLEN_CHARFOUND_LEN(r); - c = rb_enc_codepoint(fptr->cbuf+fptr->cbuf_off, - fptr->cbuf+fptr->cbuf_off+fptr->cbuf_len, - fptr->encs.enc); + if (fptr->encs.enc) { + c = rb_enc_codepoint(fptr->cbuf+fptr->cbuf_off, + fptr->cbuf+fptr->cbuf_off+fptr->cbuf_len, + fptr->encs.enc); + } + else { + c = (unsigned char)fptr->cbuf[fptr->cbuf_off]; + } fptr->cbuf_off += n; fptr->cbuf_len -= n; rb_yield(UINT2NUM(c)); diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 5fc3b133dd..a8860e1654 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -150,6 +150,16 @@ class TestIO < Test::Unit::TestCase r.close end + def test_each_codepoint + t = make_tempfile + bug2959 = '[ruby-core:28650]' + a = "" + File.open(t, 'rt') {|f| + f.each_codepoint {|c| a << c} + } + assert_equal("foo\nbar\nbaz\n", a, bug2959) + end + def test_rubydev33072 assert_raise(Errno::ENOENT, "[ruby-dev:33072]") do File.read("empty", nil, nil, {})