Skip to content

struct Char::Reader
inherits Struct

A Char::Reader allows iterating a String by Chars.

As soon as you instantiate a Char::Reader it will decode the first char in the String, which can be accessed by invoking current_char. At this point pos, the current position in the string, will equal zero. Successive calls to next_char return the next chars in the string, advancing pos.

Note that the null character '\0' will be returned in current_char when the end is reached (as well as when the string is empty). Thus, has_next? will return false only when pos is equal to the string's bytesize, in which case current_char will always be '\0'.

Included modules

Enumerable

Class methods

.new(string : String, pos = 0)

Creates a reader with the specified string positioned at byte index pos.

View source

.new(*, at_end string : String)

Creates a reader that will be positioned at the last char of the given string.

View source

Methods

#current_char : Char

Returns the current character.

reader = Char::Reader.new("ab")
reader.current_char # => 'a'
reader.next_char
reader.current_char # => 'b'
View source

#current_char_width : Int32

Returns the size of the #current_char (in bytes) as if it were encoded in UTF-8.

reader = Char::Reader.new("aƩ")
reader.current_char_width # => 1
reader.next_char
reader.current_char_width # => 2
View source

#each(&) : Nil

Yields successive characters from #string starting from #pos.

reader = Char::Reader.new("abc")
reader.next_char
reader.each do |c|
  puts c.upcase
end
B
C
View source

#error : UInt8?

If there was an error decoding the current char because of an invalid UTF-8 byte sequence, returns the byte that produced the invalid encoding. Returns 0 if the char would've been out of bounds. Otherwise returns nil.

View source

#has_next?

Returns true if there is a character left to read. The terminating byte '\0' is considered a valid character by this method.

reader = Char::Reader.new("a")
reader.has_next?      # => true
reader.peek_next_char # => '\0'
View source

#has_previous?

Returns true if there are characters before the current one.

View source

#next_char

Reads the next character in the string, #pos is incremented. Raises IndexError if the reader is at the end of the #string.

reader = Char::Reader.new("ab")
reader.next_char # => 'b'
View source

#peek_next_char

Returns the next character in the #string without incrementing #pos. Raises IndexError if the reader is at the end of the #string.

reader = Char::Reader.new("ab")
reader.peek_next_char # => 'b'
reader.current_char   # => 'a'
View source

#pos : Int32

Returns the position of the current character.

reader = Char::Reader.new("ab")
reader.pos # => 0
reader.next_char
reader.pos # => 1
View source

#pos=(pos)

Sets #pos to pos.

reader = Char::Reader.new("abc")
reader.next_char
reader.next_char
reader.pos = 0
reader.current_char # => 'a'
View source

#previous_char : Char

Returns the previous character, #pos is decremented. Raises IndexError if the reader is at the beginning of the #string

View source

#string : String

Returns the reader's String.

View source