Bug #3179

getc with text mode returns ASCII-8BIT

Added by mame (Yusuke Endoh) about 2 years ago. Updated about 1 year ago.

[ruby-dev:41023]
Status:Closed Start date:04/20/2010
Priority:Normal Due date:
Assignee:naruse (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

Revision 27426
Added by naruse (Yui NARUSE) about 2 years ago

* io.c (io_getc): set read_encoding to resulted one character string. [ruby-dev:41023]

History

Updated by naruse (Yui NARUSE) about 2 years ago

  • 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.

Updated by matz (Yukihiro Matsumoto) about 2 years ago

まつもと ゆきひろです

私のところでは"あ" "い" "う"が返りますね。

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 です。

Also available in: Atom PDF