Class: Regexp

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

Constant Summary

IGNORECASE =
1
MULTILINE =
4

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape(string) ⇒ Object Also known as: quote



9
10
11
12
13
14
15
16
17
# File 'opal/opal/corelib/regexp.rb', line 9

def escape(string)
  %x{
    return string.replace(/([-[\]\/{}()*+?.^$\\| ])/g, '\\$1')
                 .replace(/[\n]/g, '\\n')
                 .replace(/[\r]/g, '\\r')
                 .replace(/[\f]/g, '\\f')
                 .replace(/[\t]/g, '\\t');
  }
end

.last_match(n = nil) ⇒ Object



19
20
21
22
23
24
25
# File 'opal/opal/corelib/regexp.rb', line 19

def last_match(n=nil)
  if n.nil?
    $~
  else
    $~[n]
  end
end

.new(regexp, options = undefined) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'opal/opal/corelib/regexp.rb', line 68

def new(regexp, options = undefined)      
  %x{
    // Play nice with IE8
    if (regexp.$$is_string && regexp.substr(regexp.length-1, 1) == "\\") {
      #{raise RegexpError, "too short escape sequence: /#{regexp}/"}
    }
    
    if (options == undefined || #{!options}) {
      options = undefined;
    }
    
    if (options != undefined) {
      if (regexp.$$is_regexp) {
        // options are already in regex
        options = undefined;
      }
      else if (options.$$is_number) {
        var result = '';
        if (#{IGNORECASE} & options) {
          result += 'i';
        }
        if (#{MULTILINE} & options) {
          result += 'm';
        }
        options = result;
      }
      else {
        options = 'i';
      }
    }       
    
    return new RegExp(regexp, options);
  }
end

.union(*parts) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'opal/opal/corelib/regexp.rb', line 29

def union(*parts)
  %x{
    var is_first_part_array, quoted_validated, part, options, each_part_options;
    if (parts.length == 0) {
      return /(?!)/;
    }
    // cover the 2 arrays passed as arguments case
    is_first_part_array = parts[0].$$is_array;
    if (parts.length > 1 && is_first_part_array) {
      #{raise TypeError, 'no implicit conversion of Array into String'}
    }        
    // deal with splat issues (related to https://github.com/opal/opal/issues/858)
    if (is_first_part_array) {
      parts = parts[0];
    }
    options = undefined;
    quoted_validated = [];
    for (var i=0; i < parts.length; i++) {
      part = parts[i];
      if (part.$$is_string) {
        quoted_validated.push(#{escape(`part`)});
      }
      else if (part.$$is_regexp) { 
        each_part_options = #{`part`.options};   
        if (options != undefined && options != each_part_options) {
          #{raise TypeError, 'All expressions must use the same options'}
        }
        options = each_part_options;
        quoted_validated.push('('+part.source+')');
      }
      else {
        quoted_validated.push(#{escape(`part`.to_str)});
      }
    }
  }
  # Take advantage of logic that can parse options from JS Regex
  new(`quoted_validated`.join('|'), `options`) 
end

Instance Method Details

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



104
105
106
# File 'opal/opal/corelib/regexp.rb', line 104

def ==(other)
  `other.constructor == RegExp && self.toString() === other.toString()`
end

#===(string) ⇒ Object



108
109
110
# File 'opal/opal/corelib/regexp.rb', line 108

def ===(string)
  `#{match(string)} !== nil`
end

#=~(string) ⇒ Object



112
113
114
# File 'opal/opal/corelib/regexp.rb', line 112

def =~(string)
  match(string) && $~.begin(0)
end

#inspectObject



118
119
120
# File 'opal/opal/corelib/regexp.rb', line 118

def inspect
  `self.toString()`
end

#match(string, pos = undefined, &block) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'opal/opal/corelib/regexp.rb', line 122

def match(string, pos = undefined, &block)
  %x{
    if (pos === undefined) {
      pos = 0;
    } else {
      pos = #{Opal.coerce_to(pos, Integer, :to_int)};
    }

    if (string === nil) {
      return #{$~ = nil};
    }

    string = #{Opal.coerce_to(string, String, :to_str)};

    if (pos < 0) {
      pos += string.length;
      if (pos < 0) {
        return #{$~ = nil};
      }
    }

    // global RegExp maintains state, so not using self/this
    var md, re = new RegExp(self.source, 'gm' + (self.ignoreCase ? 'i' : ''));

    while (true) {
      md = re.exec(string);
      if (md === null) {
        return #{$~ = nil};
      }
      if (md.index >= pos) {
        #{$~ = MatchData.new(`re`, `md`)}
        return block === nil ? #{$~} : #{block.call($~)};
      }
      re.lastIndex = md.index + 1;
    }
  }
end

#optionsObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'opal/opal/corelib/regexp.rb', line 168

def options
  # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags is still experimental
  # we need the flags and source does not give us that
  %x{
    var as_string, text_flags, result, text_flag;
    as_string = self.toString();
    if (as_string == "/(?:)/") {
      #{raise TypeError, 'uninitialized Regexp'}
    }
    text_flags = as_string.replace(self.source, '').match(/\w+/);
    result = 0;
    // may have no flags
    if (text_flags == null) {
      return result;
    }
    // first match contains all of our flags
    text_flags = text_flags[0];
    for (var i=0; i < text_flags.length; i++) {
      text_flag = text_flags[i];
      switch(text_flag) {
        case 'i':
          result |= #{IGNORECASE};
          break;
        case 'm':
          result |= #{MULTILINE};
          break;
        default:
          #{raise "RegExp flag #{`text_flag`} does not have a match in Ruby"}
      }
    }
    
    return result;
  }  
end

#sourceObject Also known as: to_s



164
165
166
# File 'opal/opal/corelib/regexp.rb', line 164

def source
  `self.source`
end

#~Object



160
161
162
# File 'opal/opal/corelib/regexp.rb', line 160

def ~
  self =~ $_
end