mirror of
https://github.com/ruby/ruby.git
synced 2025-08-24 05:25:34 +02:00

(https://github.com/ruby/strscan/pull/126)
Split off from https://github.com/ruby/ruby/pull/12322
9bee37e0f5
43 lines
1.5 KiB
Markdown
43 lines
1.5 KiB
Markdown
call-seq:
|
|
getch -> character or nil
|
|
|
|
Returns the next (possibly multibyte) character,
|
|
if available:
|
|
|
|
- If the [position][2]
|
|
is at the beginning of a character:
|
|
|
|
- Returns the character.
|
|
- Increments the [character position][7] by 1.
|
|
- Increments the [byte position][2]
|
|
by the size (in bytes) of the character.
|
|
|
|
```rb
|
|
scanner = StringScanner.new(HIRAGANA_TEXT)
|
|
scanner.string # => "こんにちは"
|
|
[scanner.getch, scanner.pos, scanner.charpos] # => ["こ", 3, 1]
|
|
[scanner.getch, scanner.pos, scanner.charpos] # => ["ん", 6, 2]
|
|
[scanner.getch, scanner.pos, scanner.charpos] # => ["に", 9, 3]
|
|
[scanner.getch, scanner.pos, scanner.charpos] # => ["ち", 12, 4]
|
|
[scanner.getch, scanner.pos, scanner.charpos] # => ["は", 15, 5]
|
|
[scanner.getch, scanner.pos, scanner.charpos] # => [nil, 15, 5]
|
|
```
|
|
|
|
- If the [position][2] is within a multi-byte character
|
|
(that is, not at its beginning),
|
|
behaves like #get_byte (returns a 1-byte character):
|
|
|
|
```rb
|
|
scanner.pos = 1
|
|
[scanner.getch, scanner.pos, scanner.charpos] # => ["\x81", 2, 2]
|
|
[scanner.getch, scanner.pos, scanner.charpos] # => ["\x93", 3, 1]
|
|
[scanner.getch, scanner.pos, scanner.charpos] # => ["ん", 6, 2]
|
|
```
|
|
|
|
- If the [position][2] is at the end of the [stored string][1],
|
|
returns `nil` and does not modify the positions:
|
|
|
|
```rb
|
|
scanner.terminate
|
|
[scanner.getch, scanner.pos, scanner.charpos] # => [nil, 15, 5]
|
|
```
|