1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
=begin
= strscan user manual
== What is this library?
strscan is a lexical scan library.
Here is the simplest usage example of this library:
s = StringScanner.new('This is an example string')
s.eos? #=> false
p s.scan(/\w+/) #=> "This"
p s.scan(/\w+/) #=> nil
p s.scan(/\s+/) #=> " "
p s.scan(/\s+/) #=> nil
p s.scan(/\w+/) #=> "is"
s.eos? #=> false
p s.scan(/\s+/) #=> " "
p s.scan(/\w+/) #=> "an"
p s.scan(/\s+/) #=> " "
p s.scan(/\w+/) #=> "example"
p s.scan(/\s+/) #=> " "
p s.scan(/\w+/) #=> "string"
s.eos? #=> true
p s.scan(/\s+/) #=> nil
p s.scan(/\w+/) #=> nil
In a word, StringScanner is a set of "scan pointer" and
a string. "scan pointer" is, in another word, an index.
## a string and a scan pointer ("_" = scan pointer)
s = StringScanner.new('This is an example string')
_This is an example string s.eos? = false
s.scan(/\w+/)
This_ is an example string s.eos? = false
s.scan(/\s+/)
This _is an example string s.eos? = false
s.scan(/\w+/)
This is_ an example string s.eos? = false
s.scan(/\s+/)
This is _an example string s.eos? = false
s.scan(/\w+/)
This is an_ example string s.eos? = false
s.scan(/\s+/)
This is an _example string s.eos? = false
s.scan(/\w+/)
This is an example_ string s.eos? = false
s.scan(/\s+/)
This is an example _string s.eos? = false
s.scan(/\w+/)
This is an example string_ s.eos? = true
== WARNING!!!!!
StringScanner DOES NOT set regexp variables like $~, $&, $1, $2, etc.
Use StringScanner's methods instead (e.g. scanner[n], scanner.matched?).
== class StringScanner
=== Class Methods
: new( str: String, dup: bool = true ) -> StringScanner
creates a new StringScanner object. STR is a string to scan.
If DUP is true, duplicates STR and use freeze it.
If DUP is false, does not duplicate and freeze it.
=== Instance Methods
: scan( pattern: Regexp ) -> String
tries matching with PATTERN only on the scan pointer.
If matched, the scanner advances "scan pointer" and
returns a matched string. Else, the scanner returns nil.
s = StringScanner.new('test string')
p s.scan(/\w+/) #=> "test"
p s.scan(/\w+/) #=> nil
p s.scan(/\s+/) #=> " "
p s.scan(/\w+/) #=> "string"
p s.scan(/./) #=> nil
: skip( pattern: Regexp ) -> Integer
tries matching with PATTERN, only on the scan pointer.
If matched, the scanner advances "scan pointer" and
returns length of matched string. Else, the scanner
returns nil.
s = StringScanner.new('test string')
p s.skip(/\w+/) #=> 4
p s.skip(/\w+/) #=> nil
p s.skip(/\s+/) #=> 1
p s.skip(/\w+/) #=> 6
p s.skip(/./) #=> nil
: match?( pattern: Regexp ) -> Integer
tries matching with PATTERN, only on the scan pointer.
If matched, the scanner keep "scan pointer" un-touched
and returns the length of the matched string. Else, the
scanner returns nil.
s = StringScanner.new('test string')
p s.match?(/\w+/) #=> 4
p s.match?(/\w+/) #=> 4
p s.match?(/\s+/) #=> nil
: getch -> String
scans one character and returns it.
: get_byte -> String
scans one byte and returns it.
: peek( len: Integer ) -> String
extracts a string which is corresponding to string[pos, len]
: eos? -> bool
returns true if the scan pointer is on the end of the string.
: string -> String
returns the scanning string itself.
: matched? -> bool
returns true if last matching had been successed.
: pre_match -> String
: post_match -> String
: self[ n: Integer ] -> String
returns N-th content of regexp register (indecated by paren in regexp).
If previous scanning had been failed, the scanner raises ScanError.
: matched -> String
returns the entire matched string.
: reset
reset scan pointer (index 0) and clear matching data.
: terminate
set scan pointer the end of the string and clear matching data.
: unscan
set scan pointer the previous position.
This method is valid only once for one scanning method call.
=end
|