@@ -72,6 +72,10 @@ class StringScanner
7272 end
7373
7474 # Sets the *position* of the scan offset.
75+ #
76+ # NOTE: Moving the scan head with this method can cause performance issues in
77+ # multibyte strings. For a more performant way to move the head, see
78+ # [`#skip(Int)`](#skip%28pattern%3AInt%29%3AInt32%7CNil-instance-method).
7579 def offset = (position : Int )
7680 raise IndexError .new unless position >= 0
7781 @byte_offset = @str .char_index_to_byte_index(position) || @str .bytesize
@@ -84,7 +88,11 @@ class StringScanner
8488
8589 # Tries to match with *pattern* at the current position. If there's a match,
8690 # the scanner advances the scan offset, the last match is saved, and it
87- # returns the matched string. Otherwise, the scanner returns `nil`.
91+ # returns the matched string. Otherwise, the scanner returns `nil`. If *pattern*
92+ # is an Int, the scanner will be advanced by that number of characters, and the
93+ # scanned string returned. If *pattern* is an Int, then it is considered a match
94+ # if there are at least that many characters left in the string, and will return
95+ # nil otherwise.
8896 #
8997 # ```
9098 # require "string_scanner"
@@ -94,7 +102,9 @@ class StringScanner
94102 # s.scan(/\w+/) # => nil
95103 # s.scan(/\s\w/) # => " s"
96104 # s.scan('t') # => "t"
97- # s.scan("ring") # => "ring"
105+ # s.scan(2) # => "ri"
106+ # s.scan(5) # => nil
107+ # s.scan("ng") # => "ng"
98108 # s.scan(/.*/) # => ""
99109 # ```
100110 def scan (pattern : Regex , * , options : Regex ::MatchOptions = Regex ::MatchOptions ::None ) : String ?
@@ -111,7 +121,11 @@ class StringScanner
111121 match(pattern, advance: true , anchored: true )
112122 end
113123
114- # :ditto:
124+ # Advances the offset by *len* chars, and returns a string of that length.
125+ #
126+ # NOTE: If there are less than the requested number of characters
127+ # remaining in the string, this method will return nil and _not advance
128+ # the scan head_. To obtain the entire rest of the input string, use `#rest`.
115129 def scan (len : Int ) : String ?
116130 byte_len = lookahead_byte_length(len)
117131
@@ -219,7 +233,7 @@ class StringScanner
219233 match.size if match
220234 end
221235
222- # Advances the offset by ` len` chars. Prefer this to `scanner.offset += len`,
236+ # Advances the offset by * len* chars. Prefer this to `scanner.offset += len`,
223237 # since that can cause a full scan of the string in the case of multibyte
224238 # characters.
225239 #
0 commit comments