Class: MatchData

Inherits:
Object
  • Object
show all
Defined in:
opal/opal/corelib/match_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp, match_groups) ⇒ MatchData

Returns a new instance of MatchData



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'opal/opal/corelib/match_data.rb', line 4

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



2
3
4
# File 'opal/opal/corelib/match_data.rb', line 2

def post_match
  @post_match
end

#pre_matchObject (readonly)

Returns the value of attribute pre_match



2
3
4
# File 'opal/opal/corelib/match_data.rb', line 2

def pre_match
  @pre_match
end

#regexpObject (readonly)

Returns the value of attribute regexp



2
3
4
# File 'opal/opal/corelib/match_data.rb', line 2

def regexp
  @regexp
end

#stringObject (readonly)

Returns the value of attribute string



2
3
4
# File 'opal/opal/corelib/match_data.rb', line 2

def string
  @string
end

Instance Method Details

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



40
41
42
43
44
45
46
47
48
# File 'opal/opal/corelib/match_data.rb', line 40

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



27
28
29
# File 'opal/opal/corelib/match_data.rb', line 27

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

#begin(n) ⇒ Object



52
53
54
55
56
57
58
59
# File 'opal/opal/corelib/match_data.rb', line 52

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

#capturesObject



70
71
72
# File 'opal/opal/corelib/match_data.rb', line 70

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

#end(n) ⇒ Object



61
62
63
64
65
66
67
68
# File 'opal/opal/corelib/match_data.rb', line 61

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

#inspectObject



74
75
76
77
78
79
80
81
82
83
84
# File 'opal/opal/corelib/match_data.rb', line 74

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



86
87
88
# File 'opal/opal/corelib/match_data.rb', line 86

def length
  `#@matches.length`
end

#offset(n) ⇒ Object



31
32
33
34
35
36
37
38
# File 'opal/opal/corelib/match_data.rb', line 31

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



92
93
94
# File 'opal/opal/corelib/match_data.rb', line 92

def to_a
  @matches
end

#to_sObject



96
97
98
# File 'opal/opal/corelib/match_data.rb', line 96

def to_s
  `#@matches[0]`
end

#values_at(*args) ⇒ Object



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
# File 'opal/opal/corelib/match_data.rb', line 100

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