Class: Regexp

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allocateObject



7
8
9
10
11
# File 'opal/opal/rspec/fixes/opal/corelib/regexp.rb', line 7

def allocate
  allocated = super
  `#{allocated}.un_initialized = true`
  allocated
end

Instance Method Details

#===(string) ⇒ Object



34
35
36
# File 'opal/opal/rspec/fixes/opal/corelib/regexp.rb', line 34

def ===(string)
  `#{match(Opal.coerce_to?(string, String, :to_str))} !== nil`
end

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



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'opal/opal/rspec/fixes/opal/corelib/regexp.rb', line 41

def match(string, pos = undefined, &block)
  %x{
    if (self.un_initialized) {
      #{raise TypeError, 'uninitialized Regexp'}
    }

    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};
      }
    }

    var source = self.source;
    var flags = 'g';
    // m flag + a . in Ruby will match white space, but in JS, it only matches beginning/ending of lines, so we get the equivalent here
    if (self.multiline) {
      source = source.replace('.', "[\\s\\S]");
      flags += 'm';
    }

    // global RegExp maintains state, so not using self/this
    var md, re = new RegExp(source, flags + (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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'opal/opal/rspec/fixes/opal/corelib/regexp.rb', line 14

def options
  %x{
      if (self.un_initialized) {
        #{raise TypeError, 'uninitialized Regexp'}
      }
      var result = 0;
      // should be supported in IE6 according to https://msdn.microsoft.com/en-us/library/7f5z26w4(v=vs.94).aspx
      if (self.multiline) {
        result |= #{MULTILINE};
      }
      if (self.ignoreCase) {
        result |= #{IGNORECASE};
      }
      return result;
    }
end