Class: MatchData
- Defined in:
- opal/opal/corelib/marshal/write_buffer.rb,
opal/opal/corelib/regexp.rb
Instance Attribute Summary collapse
-
#post_match ⇒ Object
readonly
Returns the value of attribute post_match.
-
#pre_match ⇒ Object
readonly
Returns the value of attribute pre_match.
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #[](*args) ⇒ Object
- #__marshal__(buffer) ⇒ Object
- #begin(n) ⇒ Object
- #captures ⇒ Object
- #end(n) ⇒ Object
-
#initialize(regexp, match_groups, no_matchdata: false) ⇒ MatchData
constructor
A new instance of MatchData.
- #inspect ⇒ Object
- #length ⇒ Object (also: #size)
- #match(idx) ⇒ Object
- #match_length(idx) ⇒ Object
- #named_captures ⇒ Object
- #names ⇒ Object
- #offset(n) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #values_at(*args) ⇒ Object
Constructor Details
#initialize(regexp, match_groups, no_matchdata: false) ⇒ MatchData
Returns a new instance of MatchData.
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'opal/opal/corelib/regexp.rb', line 315 def initialize(regexp, match_groups, no_matchdata: false) $~ = self unless no_matchdata @regexp = regexp @begin = `match_groups.index` @string = `match_groups.input` @pre_match = `match_groups.input.slice(0, match_groups.index)` @post_match = `match_groups.input.slice(match_groups.index + match_groups[0].length)` @matches = [] %x{ for (var i = 0, length = match_groups.length; i < length; i++) { var group = match_groups[i]; if (group == null) { #{@matches}.push(nil); } else { #{@matches}.push(group); } } } end |
Instance Attribute Details
#post_match ⇒ Object (readonly)
Returns the value of attribute post_match.
313 314 315 |
# File 'opal/opal/corelib/regexp.rb', line 313 def post_match @post_match end |
#pre_match ⇒ Object (readonly)
Returns the value of attribute pre_match.
313 314 315 |
# File 'opal/opal/corelib/regexp.rb', line 313 def pre_match @pre_match end |
#regexp ⇒ Object (readonly)
Returns the value of attribute regexp.
313 314 315 |
# File 'opal/opal/corelib/regexp.rb', line 313 def regexp @regexp end |
#string ⇒ Object (readonly)
Returns the value of attribute string.
313 314 315 |
# File 'opal/opal/corelib/regexp.rb', line 313 def string @string end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
373 374 375 376 377 378 379 380 381 |
# File 'opal/opal/corelib/regexp.rb', line 373 def ==(other) return false unless ::MatchData === other `self.string == other.string` && `self.regexp.toString() == other.regexp.toString()` && `self.pre_match == other.pre_match` && `self.post_match == other.post_match` && `self.begin == other.begin` end |
#[](*args) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'opal/opal/corelib/regexp.rb', line 350 def [](*args) %x{ if (args[0].$$is_string) { if (#{!regexp.names.include?(args[0])}) { #{::Kernel.raise ::IndexError, "undefined group name reference: #{args[0]}"} } return #{named_captures[args[0]]} } else { return #{@matches[*args]} } } end |
#__marshal__(buffer) ⇒ Object
109 110 111 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 109 def __marshal__(buffer) ::Kernel.raise ::TypeError, "no _dump_data is defined for class #{self.class}" end |
#begin(n) ⇒ Object
383 384 385 386 387 388 389 390 |
# File 'opal/opal/corelib/regexp.rb', line 383 def begin(n) %x{ if (n !== 0) { #{::Kernel.raise ::ArgumentError, 'MatchData#begin only supports 0th element'} } return self.begin; } end |
#captures ⇒ Object
401 402 403 |
# File 'opal/opal/corelib/regexp.rb', line 401 def captures `#{@matches}.slice(1)` end |
#end(n) ⇒ Object
392 393 394 395 396 397 398 399 |
# File 'opal/opal/corelib/regexp.rb', line 392 def end(n) %x{ if (n !== 0) { #{::Kernel.raise ::ArgumentError, 'MatchData#end only supports 0th element'} } return self.begin + self.matches[n].length; } end |
#inspect ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'opal/opal/corelib/regexp.rb', line 416 def inspect %x{ var str = "#<MatchData " + #{`#{@matches}[0]`.inspect}; if (#{regexp.names.empty?}) { for (var i = 1, length = #{@matches}.length; i < length; i++) { str += " " + i + ":" + #{`#{@matches}[i]`.inspect}; } } else { #{ named_captures.each do |k, v| %x{ str += " " + #{k} + ":" + #{v.inspect} } end } } return str + ">"; } end |
#length ⇒ Object Also known as: size
437 438 439 |
# File 'opal/opal/corelib/regexp.rb', line 437 def length `#{@matches}.length` end |
#match(idx) ⇒ Object
338 339 340 341 342 343 344 |
# File 'opal/opal/corelib/regexp.rb', line 338 def match(idx) if (match = self[idx]) match elsif idx.is_a?(Integer) && idx >= length ::Kernel.raise ::IndexError, "index #{idx} out of matches" end end |
#match_length(idx) ⇒ Object
346 347 348 |
# File 'opal/opal/corelib/regexp.rb', line 346 def match_length(idx) match(idx)&.length end |
#named_captures ⇒ Object
405 406 407 408 409 410 |
# File 'opal/opal/corelib/regexp.rb', line 405 def named_captures matches = captures regexp.named_captures.transform_values do |i| matches[i.last - 1] end end |
#names ⇒ Object
412 413 414 |
# File 'opal/opal/corelib/regexp.rb', line 412 def names regexp.names end |
#offset(n) ⇒ Object
364 365 366 367 368 369 370 371 |
# File 'opal/opal/corelib/regexp.rb', line 364 def offset(n) %x{ if (n !== 0) { #{::Kernel.raise ::ArgumentError, 'MatchData#offset only supports 0th element'} } return [self.begin, self.begin + self.matches[n].length]; } end |
#to_a ⇒ Object
441 442 443 |
# File 'opal/opal/corelib/regexp.rb', line 441 def to_a @matches end |
#to_s ⇒ Object
445 446 447 |
# File 'opal/opal/corelib/regexp.rb', line 445 def to_s `#{@matches}[0]` end |
#values_at(*args) ⇒ Object
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'opal/opal/corelib/regexp.rb', line 449 def values_at(*args) %x{ var i, a, index, values = []; for (i = 0; i < args.length; i++) { if (args[i].$$is_range) { a = #{`args[i]`.to_a}; a.unshift(i, 1); Array.prototype.splice.apply(args, a); } index = #{::Opal.coerce_to!(`args[i]`, ::Integer, :to_int)}; if (index < 0) { index += #{@matches}.length; if (index < 0) { values.push(nil); continue; } } values.push(#{@matches}[index]); } return values; } end |