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
|