Class: StringScanner
Instance Attribute Summary collapse
-
#matched ⇒ Object
readonly
Returns the value of attribute matched.
-
#pos ⇒ Object
Returns the value of attribute pos.
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
- #[](idx) ⇒ Object
- #beginning_of_line? ⇒ Boolean (also: #bol?)
- #check(pattern) ⇒ Object
- #check_until(pattern) ⇒ Object
- #eos? ⇒ Boolean
- #exist?(pattern) ⇒ Boolean
- #get_byte ⇒ Object (also: #getch)
-
#initialize(string) ⇒ StringScanner
constructor
A new instance of StringScanner.
- #match?(pattern) ⇒ Boolean
- #matched_size ⇒ Object
- #peek(length) ⇒ Object
- #post_match ⇒ Object
- #pre_match ⇒ Object
- #reset ⇒ Object
- #rest ⇒ Object
- #rest? ⇒ Boolean
- #rest_size ⇒ Object
- #scan(pattern) ⇒ Object
- #scan_until(pattern) ⇒ Object
- #skip(pattern) ⇒ Object
- #skip_until(pattern) ⇒ Object
- #terminate ⇒ Object
- #unscan ⇒ Object
Constructor Details
#initialize(string) ⇒ StringScanner
Returns a new instance of StringScanner
5 6 7 8 9 10 11 |
# File 'opal/stdlib/strscan.rb', line 5 def initialize(string) @string = string @pos = 0 @matched = nil @working = string @match = [] end |
Instance Attribute Details
#matched ⇒ Object (readonly)
Returns the value of attribute matched
3 4 5 |
# File 'opal/stdlib/strscan.rb', line 3 def matched @matched end |
#pos ⇒ Object
Returns the value of attribute pos
2 3 4 |
# File 'opal/stdlib/strscan.rb', line 2 def pos @pos end |
#string ⇒ Object (readonly)
Returns the value of attribute string
13 14 15 |
# File 'opal/stdlib/strscan.rb', line 13 def string @string end |
Instance Method Details
#[](idx) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'opal/stdlib/strscan.rb', line 82 def [](idx) %x{ var match = #{@match}; if (idx < 0) { idx += match.length; } if (idx < 0 || idx >= match.length) { return nil; } if (match[idx] == null) { return nil; } return match[idx]; } end |
#beginning_of_line? ⇒ Boolean Also known as: bol?
15 16 17 |
# File 'opal/stdlib/strscan.rb', line 15 def beginning_of_line? `#{@pos} === 0 || #{@string}.charAt(#{@pos} - 1) === "\n"` end |
#check(pattern) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'opal/stdlib/strscan.rb', line 102 def check(pattern) pattern = anchor(pattern) %x{ var result = pattern.exec(#{@working}); if (result == null) { return #{@matched} = nil; } return #{@matched} = result[0]; } end |
#check_until(pattern) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'opal/stdlib/strscan.rb', line 116 def check_until(pattern) %x{ var prev_pos = #{@prev_pos}, pos = #{@pos}; var result = #{scan_until(pattern)}; if (result !== nil) { #{@matched} = result.substr(-1); #{@working} = #{@string}.substr(pos); } #{@prev_pos} = prev_pos; #{@pos} = pos; return result; } end |
#eos? ⇒ Boolean
139 140 141 |
# File 'opal/stdlib/strscan.rb', line 139 def eos? `#{@working}.length === 0` end |
#exist?(pattern) ⇒ Boolean
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'opal/stdlib/strscan.rb', line 143 def exist?(pattern) %x{ var result = pattern.exec(#{@working}); if (result == null) { return nil; } else if (result.index == 0) { return 0; } else { return result.index + 1; } } end |
#get_byte ⇒ Object Also known as: getch
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'opal/stdlib/strscan.rb', line 197 def get_byte %x{ var result = nil; if (#{@pos} < #{@string}.length) { #{@prev_pos} = #{@pos}; #{@pos} += 1; result = #{@matched} = #{@working}.substring(0, 1); #{@working} = #{@working}.substring(1); } else { #{@matched} = nil; } return result; } end |
#match?(pattern) ⇒ Boolean
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'opal/stdlib/strscan.rb', line 218 def match?(pattern) pattern = anchor(pattern) %x{ var result = pattern.exec(#{@working}); if (result == null) { return nil; } else { #{@prev_pos} = #{@pos}; return result[0].length; } } end |
#matched_size ⇒ Object
246 247 248 249 250 251 252 253 254 |
# File 'opal/stdlib/strscan.rb', line 246 def matched_size %x{ if (#{@matched} === nil) { return nil; } return #{@matched}.length } end |
#peek(length) ⇒ Object
135 136 137 |
# File 'opal/stdlib/strscan.rb', line 135 def peek(length) `#{@working}.substring(0, length)` end |
#post_match ⇒ Object
256 257 258 259 260 261 262 263 264 |
# File 'opal/stdlib/strscan.rb', line 256 def post_match %x{ if (#{@matched} === nil) { return nil; } return #{@string}.substr(#{@pos}); } end |
#pre_match ⇒ Object
266 267 268 269 270 271 272 273 274 |
# File 'opal/stdlib/strscan.rb', line 266 def pre_match %x{ if (#{@matched} === nil) { return nil; } return #{@string}.substr(0, #{@prev_pos}); } end |
#reset ⇒ Object
276 277 278 279 280 |
# File 'opal/stdlib/strscan.rb', line 276 def reset @working = @string @matched = nil @pos = 0 end |
#rest ⇒ Object
282 283 284 |
# File 'opal/stdlib/strscan.rb', line 282 def rest @working end |
#rest? ⇒ Boolean
286 287 288 |
# File 'opal/stdlib/strscan.rb', line 286 def rest? `#{@working}.length !== 0` end |
#rest_size ⇒ Object
290 291 292 |
# File 'opal/stdlib/strscan.rb', line 290 def rest_size rest.size end |
#scan(pattern) ⇒ Object
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 |
# File 'opal/stdlib/strscan.rb', line 21 def scan(pattern) pattern = anchor(pattern) %x{ var result = pattern.exec(#{@working}); if (result == null) { return #{@matched} = nil; } else if (typeof(result) === 'object') { #{@prev_pos} = #{@pos}; #{@pos} += result[0].length; #{@working} = #{@working}.substring(result[0].length); #{@matched} = result[0]; #{@match} = result; return result[0]; } else if (typeof(result) === 'string') { #{@pos} += result.length; #{@working} = #{@working}.substring(result.length); return result; } else { return nil; } } end |
#scan_until(pattern) ⇒ Object
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 |
# File 'opal/stdlib/strscan.rb', line 51 def scan_until(pattern) pattern = anchor(pattern) %x{ var pos = #{@pos}, working = #{@working}, result; while (true) { result = pattern.exec(working); pos += 1; working = working.substr(1); if (result == null) { if (working.length === 0) { return #{@matched} = nil; } continue; } #{@matched} = #{@string}.substr(#{@pos}, pos - #{@pos} - 1 + result[0].length); #{@prev_pos} = pos - 1; #{@pos} = pos; #{@working} = working.substr(result[0].length); return #{@matched}; } } end |
#skip(pattern) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'opal/stdlib/strscan.rb', line 159 def skip(pattern) pattern = anchor(pattern) %x{ var result = pattern.exec(#{@working}); if (result == null) { return #{@matched} = nil; } else { var match_str = result[0]; var match_len = match_str.length; #{@matched} = match_str; #{@prev_pos} = #{@pos}; #{@pos} += match_len; #{@working} = #{@working}.substring(match_len); return match_len; } } end |
#skip_until(pattern) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'opal/stdlib/strscan.rb', line 182 def skip_until(pattern) %x{ var result = #{scan_until(pattern)}; if (result === nil) { return nil; } else { #{@matched} = result.substr(-1); return result.length; } } end |
#terminate ⇒ Object
294 295 296 297 |
# File 'opal/stdlib/strscan.rb', line 294 def terminate @match = nil self.pos = @string.length end |
#unscan ⇒ Object
299 300 301 302 303 304 305 |
# File 'opal/stdlib/strscan.rb', line 299 def unscan @pos = @prev_pos @prev_pos = nil @match = nil self end |