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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
<?xml version="1.0" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled</title>
</head>
<body>
<h1><a name="label:0" id="label:0">strscan user manual</a></h1><!-- RDLabel: "strscan user manual" -->
<h2><a name="label:1" id="label:1">What is this library?</a></h2><!-- RDLabel: "What is this library?" -->
<p>strscan is a lexical scan library.
Here is the simplest usage example of this library:</p>
<pre>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</pre>
<p>In a word, StringScanner is a set of "scan pointer" and
a string. "scan pointer" is, in another word, an index.</p>
<pre>## 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</pre>
<h2><a name="label:2" id="label:2">WARNING!!!!!</a></h2><!-- RDLabel: "WARNING!!!!!" -->
<p>StringScanner DOES NOT set regexp variables like $~, $&, $1, $2, etc.
Use StringScanner's methods instead (e.g. scanner[n], scanner.matched?).</p>
<h2><a name="label:3" id="label:3">class StringScanner</a></h2><!-- RDLabel: "class StringScanner" -->
<h3><a name="label:4" id="label:4">Class Methods</a></h3><!-- RDLabel: "Class Methods" -->
<dl>
<dt><a name="label:5" id="label:5">new( str: String, dup: bool = true ) -> StringScanner
</a></dt><!-- RDLabel: "new( str: String, dup: bool = true ) -> StringScanner
" -->
<dd>
<p>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.</p>
</dd>
</dl>
<h3><a name="label:6" id="label:6">Instance Methods</a></h3><!-- RDLabel: "Instance Methods" -->
<dl>
<dt><a name="label:7" id="label:7">scan( pattern: Regexp ) -> String
</a></dt><!-- RDLabel: "scan( pattern: Regexp ) -> String
" -->
<dd>
<p>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.</p>
<pre>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</pre>
</dd>
<dt><a name="label:8" id="label:8">skip( pattern: Regexp ) -> Integer
</a></dt><!-- RDLabel: "skip( pattern: Regexp ) -> Integer
" -->
<dd>
<p>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.</p>
<pre>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</pre>
</dd>
<dt><a name="label:9" id="label:9">match?( pattern: Regexp ) -> Integer
</a></dt><!-- RDLabel: "match?( pattern: Regexp ) -> Integer
" -->
<dd>
<p>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.</p>
<pre>s = StringScanner.new('test string')
p s.match?(/\w+/) #=> 4
p s.match?(/\w+/) #=> 4
p s.match?(/\s+/) #=> nil</pre>
</dd>
<dt><a name="label:10" id="label:10">getch -> String
</a></dt><!-- RDLabel: "getch -> String
" -->
<dd>
<p>scans one character and returns it.</p>
</dd>
<dt><a name="label:11" id="label:11">get_byte -> String
</a></dt><!-- RDLabel: "get_byte -> String
" -->
<dd>
<p>scans one byte and returns it.</p>
</dd>
<dt><a name="label:12" id="label:12">peek( len: Integer ) -> String
</a></dt><!-- RDLabel: "peek( len: Integer ) -> String
" -->
<dd>
<p>extracts a string which is corresponding to string[pos, len]</p>
</dd>
<dt><a name="label:13" id="label:13">eos? -> bool
</a></dt><!-- RDLabel: "eos? -> bool
" -->
<dd>
<p>returns true if the scan pointer is on the end of the string.</p>
</dd>
<dt><a name="label:14" id="label:14">string -> String
</a></dt><!-- RDLabel: "string -> String
" -->
<dd>
<p>returns the scanning string itself.</p>
</dd>
<dt><a name="label:15" id="label:15">matched? -> bool
</a></dt><!-- RDLabel: "matched? -> bool
" -->
<dd>
<p>returns true if last matching had been successed.</p>
</dd>
<dt><a name="label:16" id="label:16">pre_match -> String
</a></dt><!-- RDLabel: "pre_match -> String
" -->
<dt><a name="label:17" id="label:17">post_match -> String
</a></dt><!-- RDLabel: "post_match -> String
" -->
<dt><a name="label:18" id="label:18">self[ n: Integer ] -> String
</a></dt><!-- RDLabel: "self[ n: Integer ] -> String
" -->
<dd>
<p>returns N-th content of regexp register (indecated by paren in regexp).
If previous scanning had been failed, the scanner raises ScanError.</p>
</dd>
<dt><a name="label:19" id="label:19">matched -> String
</a></dt><!-- RDLabel: "matched -> String
" -->
<dd>
<p>returns the entire matched string.</p>
</dd>
<dt><a name="label:20" id="label:20">reset
</a></dt><!-- RDLabel: "reset
" -->
<dd>
<p>reset scan pointer (index 0) and clear matching data.</p>
</dd>
<dt><a name="label:21" id="label:21">terminate
</a></dt><!-- RDLabel: "terminate
" -->
<dd>
<p>set scan pointer the end of the string and clear matching data.</p>
</dd>
<dt><a name="label:22" id="label:22">unscan
</a></dt><!-- RDLabel: "unscan
" -->
<dd>
<p>set scan pointer the previous position.
This method is valid only once for one scanning method call.</p>
</dd>
</dl>
</body>
</html>
|