Bug #3179 [ruby-dev:41023]
getc with text mode returns ASCII-8BIT
| Status : | Closed | Start : | 04/20/2010 | |
| Priority : | Normal | Due date : | ||
| Assigned to : | Yui NARUSE | % Done : | 100% |
|
| Category : | M17N | |||
| Target version : | 1.9.2 | |||
| ruby -v : | ruby 1.9.2dev (2010-04-20 trunk 27405) [i686-linux] |
Description
遠藤です。
以下の動作は意図的でしょうか。
# coding: UTF-8
# "あいう" というファイルを作る
s = "あいう"
open("foo.txt", "wb") {|f| f.write(s) }
# IO#read で一気に読めば期待通り
open("foo.txt", "rt") {|f| p f.read } #=> "あいう"
# getc するとバイナリとして読んだ感じになる
open("foo.txt", "rt") do |f|
p f.getc #=> "\xE3"
p f.getc #=> "\x81"
p f.getc #=> "\x82"
end
各 getc で "あ" "い" "う" が帰ってくることを期待しました。
LANG は ja_JP.UTF-8 です。
--
Yusuke Endoh <mame@tsg.ne.jp>
Associated revisions
- io.c (io_getc): set read_encoding to resulted one character
string.
[ruby-dev:41023]
History
04/21/2010 03:27 AM - Yui NARUSE
- Status changed from Open to Closed
- % Done changed from 0 to 100
This issue was solved with changeset r27426. Yusuke, thank you for reporting this issue. Your contribution to Ruby is greatly appreciated. May Ruby be with you.
04/21/2010 10:23 PM - Yukihiro Matsumoto
まつもと ゆきひろです
私のところでは"あ" "い" "う"が返りますね。
minirubyはLANGを見ないのでexternal_encodingがASCII-8BITにな
り、"\xE3" "\x81" "\x82" が返りますが、これは明示的にUTF-8を
指定して
open("foo.txt", "rt:UTF-8")
とすると、やはりUTF-8の文字単位で動作します。
In message "Re: [ruby-dev:41023] [Bug #3179] getc with text mode returns ASCII-8BIT"
on Tue, 20 Apr 2010 21:58:03 +0900, Yusuke Endoh <redmine@ruby-lang.org> writes:
|以下の動作は意図的でしょうか。
|
| # coding: UTF-8
| # "あいう" というファイルを作る
| s = "あいう"
| open("foo.txt", "wb") {|f| f.write(s) }
|
| # IO#read で一気に読めば期待通り
| open("foo.txt", "rt") {|f| p f.read } #=> "あいう"
|
| # getc するとバイナリとして読んだ感じになる
| open("foo.txt", "rt") do |f|
| p f.getc #=> "\xE3"
| p f.getc #=> "\x81"
| p f.getc #=> "\x82"
| end
|
|各 getc で "あ" "い" "う" が帰ってくることを期待しました。
|LANG は ja_JP.UTF-8 です。