PoParser::Scanner
Inherits StringScanner < Reference < Object
Polyfill for 1.21 StringScanner methods missing on older Crystal versions
Instance methods
Returns the value that #scan would return, without advancing the scan
offset. The last match is still saved, however.
require "string_scanner"
s = StringScanner.new("this is a string")
s.offset = 5
s.check(/\w+/) # => "is"
s.check(/\w+/) # => "is"
Returns the character at the scan head, and errors if at the end. Does not move the scan head. This will properly decode the next character from the string, and may return a multi-byte character.
Returns the character at the scan head, or nil if at the end. Does not move the scan head. This will properly decode the next character from the string, and may return a multi-byte character.
Rewinds the scan head by len characters.
Raises IndexError if this would go off the beginning of the stream.
Advances the offset by len chars, and returns a string of that length.
NOTE: If there are less than the requested number of characters
remaining in the string, this method will return nil and not advance
the scan head. To obtain the entire rest of the input string, use #rest.
require "string_scanner"
s = StringScanner.new("あいうえお")
s.scan(3) # => "あいう"
s.scan(100) # => nil
s.scan(2) # => "えお"
s.scan(0) # => ""
Tries to match with pattern at the current position. If there's a match,
the scanner advances the scan offset, the last match is saved, and it
returns the matched string. Otherwise, the scanner returns nil.
require "string_scanner"
s = StringScanner.new("test string")
s.scan(/\w+/) # => "test"
s.scan(/\w+/) # => nil
s.scan(/\s\w/) # => " s"
s.scan('t') # => "t"
s.scan("ring") # => "ring"
s.scan(/.*/) # => ""
Advances the offset by len chars.
Prefer this to scanner.offset += len, since that can cause a full
scan of the string in the case of multibyte characters.
NOTE: If there are less than the requested number of characters
remaining in the string, this method will return nil and not advance
the scan head. To move the scan head to the very end, use #terminate.
Attempts to skip over the given pattern beginning with the scan offset.
If there's a match, the scanner advances the scan offset, the last match is
saved, and it returns the size of the skipped match. Otherwise it returns
nil and does not advance the offset.
This method is the same as #scan, but without returning the matched
string.