Class: Rufio::MultibyteInputReader
- Inherits:
-
Object
- Object
- Rufio::MultibyteInputReader
- Defined in:
- lib/rufio/multibyte_input_reader.rb
Overview
ターミナルからのマルチバイト入力(UTF-8)を読み込むクラス。read_nonblock(1) は1バイトずつしか読まないため、日本語などのマルチバイト文字が複数のイベントに分割される問題を解決する。
Instance Method Summary collapse
-
#initialize(io = STDIN) ⇒ MultibyteInputReader
constructor
A new instance of MultibyteInputReader.
-
#read_char ⇒ Object
1文字(マルチバイト対応)を読み込む。 読み込めない場合は nil を返す。.
Constructor Details
#initialize(io = STDIN) ⇒ MultibyteInputReader
Returns a new instance of MultibyteInputReader.
8 9 10 |
# File 'lib/rufio/multibyte_input_reader.rb', line 8 def initialize(io = STDIN) @io = io end |
Instance Method Details
#read_char ⇒ Object
1文字(マルチバイト対応)を読み込む。読み込めない場合は nil を返す。
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rufio/multibyte_input_reader.rb', line 14 def read_char byte = @io.read_nonblock(1) return nil if byte.nil? remaining = utf8_remaining_bytes(byte.ord) return byte.force_encoding(Encoding::UTF_8) if remaining == 0 buf = byte.b remaining.times do next_byte = @io.read_nonblock(1) return nil if next_byte.nil? buf << next_byte.b end result = buf.force_encoding(Encoding::UTF_8) result.valid_encoding? ? result : nil rescue IO::WaitReadable, IO::EAGAINWaitReadable, EOFError nil end |