Class: MatchData

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp, match_groups) ⇒ MatchData

Returns a new instance of MatchData



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'opal/opal/corelib/match_data.rb', line 14

def initialize(regexp, match_groups)
  @regexp     = regexp
  @begin      = `match_groups.index`
  @string     = `match_groups.input`
  @pre_match  = `#@string.substr(0, regexp.lastIndex - match_groups[0].length)`
  @post_match = `#@string.substr(regexp.lastIndex)`
  @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

Class Method Details

.new(regexp, match_groups) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'opal/opal/corelib/match_data.rb', line 4

def self.new(regexp, match_groups)
  data = super(regexp, match_groups)

  $` = data.pre_match
  $' = data.post_match
  $~ = data

  data
end

Instance Method Details

#==(other) ⇒ Object



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 == other.regexp` &&
  `self.pre_match == other.pre_match` &&
  `self.post_match == other.post_match` &&
  `self.begin == other.begin`
end

#[](*args) ⇒ Object



36
37
38
# File 'opal/opal/corelib/match_data.rb', line 36

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

#begin(pos) ⇒ Object



50
51
52
53
54
55
56
# File 'opal/opal/corelib/match_data.rb', line 50

def begin(pos)
  if pos != 0 && pos != 1
    raise ArgumentError, 'MatchData#begin only supports 0th element'
  end

  @begin
end

#capturesObject



58
59
60
# File 'opal/opal/corelib/match_data.rb', line 58

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

#inspectObject



62
63
64
65
66
67
68
69
70
71
72
# File 'opal/opal/corelib/match_data.rb', line 62

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



74
75
76
# File 'opal/opal/corelib/match_data.rb', line 74

def length
  `#@matches.length`
end

#to_aObject



80
81
82
# File 'opal/opal/corelib/match_data.rb', line 80

def to_a
  @matches
end

#to_sObject



84
85
86
# File 'opal/opal/corelib/match_data.rb', line 84

def to_s
  `#@matches[0]`
end

#values_at(*indexes) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'opal/opal/corelib/match_data.rb', line 88

def values_at(*indexes)
  %x{
    var values       = [],
        match_length = #@matches.length;

    for (var i = 0, length = indexes.length; i < length; i++) {
      var pos = indexes[i];

      if (pos >= 0) {
        values.push(#@matches[pos]);
      }
      else {
        pos += match_length;

        if (pos > 0) {
          values.push(#@matches[pos]);
        }
        else {
          values.push(nil);
        }
      }
    }

    return values;
  }
end