ruby-changes:15523
From: naruse <ko1@a...>
Date: Wed, 21 Apr 2010 03:19:22 +0900 (JST)
Subject: [ruby-changes:15523] Ruby:r27426 (trunk): * io.c (io_getc): set read_encoding to resulted one character
naruse 2010-04-21 03:19:01 +0900 (Wed, 21 Apr 2010) New Revision: 27426 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=27426 Log: * io.c (io_getc): set read_encoding to resulted one character string. [ruby-dev:41023] Modified files: trunk/ChangeLog trunk/io.c trunk/test/ruby/test_io_m17n.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 27425) +++ ChangeLog (revision 27426) @@ -1,3 +1,8 @@ +Wed Apr 21 03:17:17 2010 NARUSE, Yui <naruse@r...> + + * io.c (io_getc): set read_encoding to resulted one character + string. [ruby-dev:41023] + Wed Apr 21 00:29:39 2010 Yusuke Endoh <mame@t...> * bignum.c (bigmul1_karatsuba): fix calculation order to prevent Index: io.c =================================================================== --- io.c (revision 27425) +++ io.c (revision 27426) @@ -2739,17 +2739,15 @@ if (NEED_READCONV(fptr)) { VALUE str = Qnil; + rb_encoding *read_enc = io_read_encoding(fptr); make_readconv(fptr, 0); while (1) { if (fptr->cbuf_len) { - if (fptr->encs.enc) - r = rb_enc_precise_mbclen(fptr->cbuf+fptr->cbuf_off, - fptr->cbuf+fptr->cbuf_off+fptr->cbuf_len, - fptr->encs.enc); - else - r = ONIGENC_CONSTRUCT_MBCLEN_CHARFOUND(1); + r = rb_enc_precise_mbclen(fptr->cbuf+fptr->cbuf_off, + fptr->cbuf+fptr->cbuf_off+fptr->cbuf_len, + read_enc); if (!MBCLEN_NEEDMORE_P(r)) break; if (fptr->cbuf_len == fptr->cbuf_capa) { @@ -2761,17 +2759,30 @@ clear_readconv(fptr); if (fptr->cbuf_len == 0) return Qnil; - /* return an incomplete character just before EOF */ - return io_shift_cbuf(fptr, fptr->cbuf_len, &str); + /* return an unit of an incomplete character just before EOF */ + r = rb_enc_mbclen(fptr->cbuf+fptr->cbuf_off, + fptr->cbuf+fptr->cbuf_off+fptr->cbuf_len, + read_enc); + io_shift_cbuf(fptr, r, &str); + str = io_enc_str(str, fptr); + ENC_CODERANGE_SET(str, ENC_CODERANGE_BROKEN); + return str; } } if (MBCLEN_INVALID_P(r)) { r = rb_enc_mbclen(fptr->cbuf+fptr->cbuf_off, fptr->cbuf+fptr->cbuf_off+fptr->cbuf_len, - fptr->encs.enc); - return io_shift_cbuf(fptr, r, &str); - } - return io_shift_cbuf(fptr, MBCLEN_CHARFOUND_LEN(r), &str); + read_enc); + io_shift_cbuf(fptr, r, &str); + cr = ENC_CODERANGE_BROKEN; + } + else { + io_shift_cbuf(fptr, MBCLEN_CHARFOUND_LEN(r), &str); + cr = ENC_CODERANGE_VALID; + } + str = io_enc_str(str, fptr); + ENC_CODERANGE_SET(str, cr); + return str; } if (io_fillbuf(fptr) < 0) { Index: test/ruby/test_io_m17n.rb =================================================================== --- test/ruby/test_io_m17n.rb (revision 27425) +++ test/ruby/test_io_m17n.rb (revision 27426) @@ -385,6 +385,21 @@ } end + def test_getc_newlineconv + with_tmpdir { + src = "\u3042" + generate_file('tmp', src) + defext = Encoding.default_external + Encoding.default_external = Encoding::UTF_8 + open("tmp", "rt") {|f| + s = f.getc + assert_equal(true, s.valid_encoding?) + assert_equal("\u3042", s) + } + Encoding.default_external = defext + } + end + def test_ungetc_stateful_conversion with_tmpdir { src = "before \e$B\x23\x30\x23\x31\e(B after".force_encoding("iso-2022-jp") -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/