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'
.
Creates a reader with the specified string positioned at byte index pos.
Creates a reader that will be positioned at the last char of the given string.
Returns the current character.
Returns the size of the #current_char
(in bytes) as if it were encoded in UTF-8.
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 true
if there is a character left to read.
Returns true
if there are characters before the current one.
Reads the next character in the string, #pos
is incremented.
Returns the position of the current character.
Sets #pos
to pos.
Returns the previous character, #pos
is decremented.
Returns the reader's String.
Enumerable(Char)
Struct
Value
Object
Object
Creates a reader with the specified string positioned at byte index pos.
Creates a reader that will be positioned at the last char of the given string.
Returns the current character.
reader = Char::Reader.new("ab") reader.current_char # => 'a' reader.next_char reader.current_char # => 'b'
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
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
.
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'
Returns true
if there are characters before the current one.
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'
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'
Returns the position of the current character.
reader = Char::Reader.new("ab") reader.pos # => 0 reader.next_char reader.pos # => 1
Sets #pos
to pos.
reader = Char::Reader.new("abc") reader.next_char reader.next_char reader.pos = 0 reader.current_char # => 'a'
Returns the previous character, #pos
is decremented. Raises IndexError
if the reader is at the beginning of the #string
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/Char/Reader.html