Class: StringScanner
- Inherits:
-
Object
- Object
- StringScanner
- Defined in:
- lib/rubysl/strscan/strscan.rb
Constant Summary collapse
- Id =
"None$Id".freeze
- Version =
"1.0.0".freeze
Instance Attribute Summary collapse
-
#match ⇒ Object
readonly
Returns the value of attribute match.
-
#pos ⇒ Object
(also: #pointer)
Returns the value of attribute pos.
-
#prev_pos ⇒ Object
readonly
Returns the value of attribute prev_pos.
Class Method Summary collapse
Instance Method Summary collapse
- #[](n) ⇒ Object
- #bol? ⇒ Boolean (also: #beginning_of_line?)
- #check(pattern) ⇒ Object
- #check_until(pattern) ⇒ Object
- #clear ⇒ Object
- #concat(str) ⇒ Object (also: #<<)
- #empty? ⇒ Boolean
- #eos? ⇒ Boolean
- #exist?(pattern) ⇒ Boolean
- #get_byte ⇒ Object
- #getbyte ⇒ Object
- #getch ⇒ Object
-
#initialize(string, dup = false) ⇒ StringScanner
constructor
A new instance of StringScanner.
- #inspect ⇒ Object
- #match?(pattern) ⇒ Boolean
- #matched ⇒ Object
- #matched? ⇒ Boolean
- #matched_size ⇒ Object
- #matchedsize ⇒ Object
- #peek(len) ⇒ Object
- #peep(len) ⇒ Object
- #post_match ⇒ Object
- #pre_match ⇒ Object
- #reset ⇒ Object
- #rest ⇒ Object
- #rest? ⇒ Boolean
- #rest_size ⇒ Object
- #restsize ⇒ Object
- #scan(pattern) ⇒ Object
- #scan_full(pattern, advance_pos, getstr) ⇒ Object
- #scan_until(pattern) ⇒ Object
- #search_full(pattern, advance_pos, getstr) ⇒ Object
- #skip(pattern) ⇒ Object
- #skip_until(pattern) ⇒ Object
- #string ⇒ Object
- #string=(string) ⇒ Object
- #terminate ⇒ Object
- #unscan ⇒ Object
Constructor Details
#initialize(string, dup = false) ⇒ StringScanner
Returns a new instance of StringScanner.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rubysl/strscan/strscan.rb', line 80 def initialize(string, dup=false) if string.instance_of? String @original = string @string = string else @original = StringValue(string) @string = String.new @original end reset_state end |
Instance Attribute Details
#match ⇒ Object (readonly)
Returns the value of attribute match.
7 8 9 |
# File 'lib/rubysl/strscan/strscan.rb', line 7 def match @match end |
#pos ⇒ Object Also known as: pointer
Returns the value of attribute pos.
7 8 9 |
# File 'lib/rubysl/strscan/strscan.rb', line 7 def pos @pos end |
#prev_pos ⇒ Object (readonly)
Returns the value of attribute prev_pos.
7 8 9 |
# File 'lib/rubysl/strscan/strscan.rb', line 7 def prev_pos @prev_pos end |
Class Method Details
.must_C_version ⇒ Object
196 197 198 |
# File 'lib/rubysl/strscan/strscan.rb', line 196 def self.must_C_version self end |
Instance Method Details
#[](n) ⇒ Object
24 25 26 27 |
# File 'lib/rubysl/strscan/strscan.rb', line 24 def [](n) raise TypeError, "Bad argument #{n.inspect}" unless n.respond_to? :to_int @match[n] if @match end |
#bol? ⇒ Boolean Also known as: beginning_of_line?
29 30 31 |
# File 'lib/rubysl/strscan/strscan.rb', line 29 def bol? @pos == 0 or @string.getbyte(pos-1) == 10 end |
#check(pattern) ⇒ Object
35 36 37 |
# File 'lib/rubysl/strscan/strscan.rb', line 35 def check(pattern) _scan pattern, false, true, true end |
#check_until(pattern) ⇒ Object
39 40 41 |
# File 'lib/rubysl/strscan/strscan.rb', line 39 def check_until(pattern) _scan pattern, false, true, false end |
#clear ⇒ Object
43 44 45 46 |
# File 'lib/rubysl/strscan/strscan.rb', line 43 def clear warn "StringScanner#clear is obsolete; use #terminate instead" if $VERBOSE terminate end |
#concat(str) ⇒ Object Also known as: <<
48 49 50 51 |
# File 'lib/rubysl/strscan/strscan.rb', line 48 def concat(str) @string << StringValue(str) self end |
#empty? ⇒ Boolean
54 55 56 57 |
# File 'lib/rubysl/strscan/strscan.rb', line 54 def empty? warn "StringScanner#empty? is obsolete; use #eos? instead?" if $VERBOSE eos? end |
#eos? ⇒ Boolean
59 60 61 |
# File 'lib/rubysl/strscan/strscan.rb', line 59 def eos? @pos >= @string.bytesize end |
#exist?(pattern) ⇒ Boolean
63 64 65 |
# File 'lib/rubysl/strscan/strscan.rb', line 63 def exist?(pattern) _scan pattern, false, false, false end |
#get_byte ⇒ Object
67 68 69 |
# File 'lib/rubysl/strscan/strscan.rb', line 67 def get_byte scan(/./mn) end |
#getbyte ⇒ Object
71 72 73 74 |
# File 'lib/rubysl/strscan/strscan.rb', line 71 def getbyte warn "StringScanner#getbyte is obsolete; use #get_byte instead" if $VERBOSE get_byte end |
#getch ⇒ Object
76 77 78 |
# File 'lib/rubysl/strscan/strscan.rb', line 76 def getch scan(/./m) end |
#inspect ⇒ Object
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 |
# File 'lib/rubysl/strscan/strscan.rb', line 92 def inspect if defined? @string if eos? str = "#<StringScanner fin>" else if string.bytesize - pos > 5 rest = "#{string[pos..pos+4]}..." else rest = string[pos..string.bytesize] end if pos > 0 if pos > 5 prev = "...#{string[pos-5...pos]}" else prev = string[0...pos] end str = "#<StringScanner #{pos}/#{string.bytesize} #{prev.inspect} @ #{rest.inspect}>" else str = "#<StringScanner #{pos}/#{string.bytesize} @ #{rest.inspect}>" end end str.taint if @string.tainted? return str else "#<StringScanner (uninitialized)>" end end |
#match?(pattern) ⇒ Boolean
123 124 125 |
# File 'lib/rubysl/strscan/strscan.rb', line 123 def match?(pattern) _scan pattern, false, false, true end |
#matched ⇒ Object
127 128 129 |
# File 'lib/rubysl/strscan/strscan.rb', line 127 def matched @match.to_s if @match end |
#matched? ⇒ Boolean
131 132 133 |
# File 'lib/rubysl/strscan/strscan.rb', line 131 def matched? !!@match end |
#matched_size ⇒ Object
135 136 137 |
# File 'lib/rubysl/strscan/strscan.rb', line 135 def matched_size @match.to_s.bytesize if @match end |
#matchedsize ⇒ Object
139 140 141 142 |
# File 'lib/rubysl/strscan/strscan.rb', line 139 def matchedsize warn "StringScanner#matchedsize is obsolete; use #matched_size instead" if $VERBOSE matched_size end |
#peek(len) ⇒ Object
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/rubysl/strscan/strscan.rb', line 238 def peek(len) raise ArgumentError if len < 0 return "" if len.zero? begin return @string[pos, len] rescue TypeError raise RangeError, "offset outside of possible range" end end |
#peep(len) ⇒ Object
249 250 251 252 |
# File 'lib/rubysl/strscan/strscan.rb', line 249 def peep(len) warn "StringScanner#peep is obsolete; use #peek instead" if $VERBOSE peek len end |
#post_match ⇒ Object
144 145 146 |
# File 'lib/rubysl/strscan/strscan.rb', line 144 def post_match @match.post_match if @match end |
#pre_match ⇒ Object
148 149 150 |
# File 'lib/rubysl/strscan/strscan.rb', line 148 def pre_match @string.byteslice(0, match.full.at(0)) if @match end |
#reset ⇒ Object
158 159 160 161 |
# File 'lib/rubysl/strscan/strscan.rb', line 158 def reset reset_state self end |
#rest ⇒ Object
163 164 165 |
# File 'lib/rubysl/strscan/strscan.rb', line 163 def rest @string.byteslice(@pos, @string.bytesize - @pos) end |
#rest? ⇒ Boolean
167 168 169 |
# File 'lib/rubysl/strscan/strscan.rb', line 167 def rest? return !eos? end |
#rest_size ⇒ Object
171 172 173 |
# File 'lib/rubysl/strscan/strscan.rb', line 171 def rest_size @string.bytesize - @pos end |
#restsize ⇒ Object
175 176 177 178 |
# File 'lib/rubysl/strscan/strscan.rb', line 175 def restsize warn "StringScanner#restsize is obsolete; use #rest_size instead" if $VERBOSE rest_size end |
#scan(pattern) ⇒ Object
180 181 182 |
# File 'lib/rubysl/strscan/strscan.rb', line 180 def scan(pattern) _scan pattern, true, true, true end |
#scan_full(pattern, advance_pos, getstr) ⇒ Object
188 189 190 |
# File 'lib/rubysl/strscan/strscan.rb', line 188 def scan_full(pattern, advance_pos, getstr) _scan pattern, advance_pos, getstr, true end |
#scan_until(pattern) ⇒ Object
184 185 186 |
# File 'lib/rubysl/strscan/strscan.rb', line 184 def scan_until(pattern) _scan pattern, true, true, false end |
#search_full(pattern, advance_pos, getstr) ⇒ Object
192 193 194 |
# File 'lib/rubysl/strscan/strscan.rb', line 192 def search_full(pattern, advance_pos, getstr) _scan pattern, advance_pos, getstr, false end |
#skip(pattern) ⇒ Object
200 201 202 |
# File 'lib/rubysl/strscan/strscan.rb', line 200 def skip(pattern) _scan pattern, true, false, true end |
#skip_until(pattern) ⇒ Object
204 205 206 |
# File 'lib/rubysl/strscan/strscan.rb', line 204 def skip_until(pattern) _scan pattern, true, false, false end |
#string ⇒ Object
208 209 210 |
# File 'lib/rubysl/strscan/strscan.rb', line 208 def string @original end |
#string=(string) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/rubysl/strscan/strscan.rb', line 212 def string=(string) reset_state if string.instance_of? String @original = string @string = string else @original = StringValue(string) @string = String.new @original end end |
#terminate ⇒ Object
224 225 226 227 228 |
# File 'lib/rubysl/strscan/strscan.rb', line 224 def terminate @match = nil @pos = string.bytesize self end |
#unscan ⇒ Object
230 231 232 233 234 235 236 |
# File 'lib/rubysl/strscan/strscan.rb', line 230 def unscan raise ScanError if @match.nil? @pos = @prev_pos @prev_pos = nil @match = nil self end |