Class: MatchData

Inherits:
Object show all
Defined in:
opal/opal/corelib/regexp.rb,
opal/opal/corelib/marshal/write_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp, match_groups) ⇒ MatchData

Returns a new instance of MatchData.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'opal/opal/corelib/regexp.rb', line 283

def initialize(regexp, match_groups)
  $~          = self
  @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_matchObject (readonly)

Returns the value of attribute post_match.



281
282
283
# File 'opal/opal/corelib/regexp.rb', line 281

def post_match
  @post_match
end

#pre_matchObject (readonly)

Returns the value of attribute pre_match.



281
282
283
# File 'opal/opal/corelib/regexp.rb', line 281

def pre_match
  @pre_match
end

#regexpObject (readonly)

Returns the value of attribute regexp.



281
282
283
# File 'opal/opal/corelib/regexp.rb', line 281

def regexp
  @regexp
end

#stringObject (readonly)

Returns the value of attribute string.



281
282
283
# File 'opal/opal/corelib/regexp.rb', line 281

def string
  @string
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



319
320
321
322
323
324
325
326
327
# File 'opal/opal/corelib/regexp.rb', line 319

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



306
307
308
# File 'opal/opal/corelib/regexp.rb', line 306

def [](*args)
  @matches[*args]
end

#__marshal__(buffer) ⇒ Object

Raises:



107
108
109
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 107

def __marshal__(buffer)
  raise TypeError, "no _dump_data is defined for class #{self.class}"
end

#begin(n) ⇒ Object



331
332
333
334
335
336
337
338
# File 'opal/opal/corelib/regexp.rb', line 331

def begin(n)
  %x{
    if (n !== 0) {
      #{raise ArgumentError, 'MatchData#begin only supports 0th element'}
    }
    return self.begin;
  }
end

#capturesObject



349
350
351
# File 'opal/opal/corelib/regexp.rb', line 349

def captures
  `#{@matches}.slice(1)`
end

#end(n) ⇒ Object



340
341
342
343
344
345
346
347
# File 'opal/opal/corelib/regexp.rb', line 340

def end(n)
  %x{
    if (n !== 0) {
      #{raise ArgumentError, 'MatchData#end only supports 0th element'}
    }
    return self.begin + self.matches[n].length;
  }
end

#inspectObject



353
354
355
356
357
358
359
360
361
362
363
# File 'opal/opal/corelib/regexp.rb', line 353

def inspect
  %x{
    var str = "#<MatchData " + #{`#{@matches}[0]`.inspect};

    for (var i = 1, length = #{@matches}.length; i < length; i++) {
      str += " " + i + ":" + #{`#{@matches}[i]`.inspect};
    }

    return str + ">";
  }
end

#lengthObject Also known as: size



365
366
367
# File 'opal/opal/corelib/regexp.rb', line 365

def length
  `#{@matches}.length`
end

#offset(n) ⇒ Object



310
311
312
313
314
315
316
317
# File 'opal/opal/corelib/regexp.rb', line 310

def offset(n)
  %x{
    if (n !== 0) {
      #{raise ArgumentError, 'MatchData#offset only supports 0th element'}
    }
    return [self.begin, self.begin + self.matches[n].length];
  }
end

#to_aObject



371
372
373
# File 'opal/opal/corelib/regexp.rb', line 371

def to_a
  @matches
end

#to_sObject



375
376
377
# File 'opal/opal/corelib/regexp.rb', line 375

def to_s
  `#{@matches}[0]`
end

#values_at(*args) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'opal/opal/corelib/regexp.rb', line 379

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