Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 11aa11d6ca1f
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 599c80cf867d
Choose a head ref
  • 5 commits
  • 4 files changed
  • 2 contributors

Commits on May 1, 2015

  1. Run all MatchData rubyspecs

    vais committed May 1, 2015
    Copy the full SHA
    6d21bca View commit details
  2. Copy the full SHA
    9fcc6e6 View commit details
  3. Copy the full SHA
    18c6180 View commit details
  4. MatchData#offset/#begin/#end will only ever support 0th element becau…

    …se JS
    
    It is theoretically impossible to support these methods correctly given
    how JS regular expressions engine works. I.e. there is no way to get the
    index of each captured group. Faking it with substring search works in
    some cases, but not in all cases. It is better to explicitly fail for
    n > 0 than to give correct results *some* of the time.
    vais committed May 1, 2015
    Copy the full SHA
    1d49859 View commit details
  5. Merge pull request #823 from vais/matchdata

    Finish MatchData
    meh committed May 1, 2015
    Copy the full SHA
    599c80c View commit details
Showing with 68 additions and 51 deletions.
  1. +36 −27 opal/corelib/match_data.rb
  2. +0 −11 spec/filters/bugs/match_data.rb
  3. +31 −0 spec/filters/unsupported/regular_expressions.rb
  4. +1 −13 spec/rubyspecs
63 changes: 36 additions & 27 deletions opal/corelib/match_data.rb
Original file line number Diff line number Diff line change
@@ -30,34 +30,41 @@ def [](*args)

def offset(n)
%x{
if (n >= #@matches.length) {
#{raise IndexError, "index #{n} out of matches"}
if (n !== 0) {
#{raise ArgumentError, 'MatchData#offset only supports 0th element'}
}
var input = #@string,
match = #@matches[n];
if (match === nil) {
return [nil, nil];
}
return [input.indexOf(match), input.indexOf(match) + match.length];
return [self.begin, self.begin + self.matches[n].length];
}
end

def ==(other)
return false unless MatchData === other

`self.string == other.string` &&
`self.regexp == other.regexp` &&
`self.regexp.toString() == other.regexp.toString()` &&
`self.pre_match == other.pre_match` &&
`self.post_match == other.post_match` &&
`self.begin == other.begin`
end

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

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

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

def captures
@@ -90,27 +97,29 @@ def to_s
`#@matches[0]`
end

def values_at(*indexes)
def values_at(*args)
%x{
var values = [],
match_length = #@matches.length;
var i, a, index, values = [];
for (var i = 0, length = indexes.length; i < length; i++) {
var pos = indexes[i];
for (i = 0; i < args.length; i++) {
if (pos >= 0) {
values.push(#@matches[pos]);
if (args[i].$$is_range) {
a = #{`args[i]`.to_a};
a.unshift(i, 1);
Array.prototype.splice.apply(args, a);
}
else {
pos += match_length;
if (pos > 0) {
values.push(#@matches[pos]);
}
else {
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;
11 changes: 0 additions & 11 deletions spec/filters/bugs/match_data.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,2 @@
opal_filter "MatchData" do
fails "MatchData#regexp returns the pattern used in the match"
fails "MatchData#values_at when passed a Range returns an array of the matching value"

fails "MatchData#[Symbol] returns the corresponding named match when given a Symbol"
fails "MatchData#[Symbol] returns the corresponding named match when given a String"
fails "MatchData#[Symbol] returns the matching version of multiple corresponding named match"
fails "MatchData#[Symbol] returns the last match when multiple named matches exist with the same name"
fails "MatchData#[Symbol] returns nil on non-matching named matches"
fails "MatchData#[Symbol] raises an IndexError if there is no named match corresponding to the Symbol"
fails "MatchData#[Symbol] raises an IndexError if there is no named match corresponding to the String"
fails "MatchData#[Symbol] returns matches in the String's encoding"
end
31 changes: 31 additions & 0 deletions spec/filters/unsupported/regular_expressions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
opal_filter "regular_expressions" do
fails "MatchData#[Symbol] returns the corresponding named match when given a Symbol"
fails "MatchData#[Symbol] returns the corresponding named match when given a String"
fails "MatchData#[Symbol] returns the matching version of multiple corresponding named match"
fails "MatchData#[Symbol] returns the last match when multiple named matches exist with the same name"
fails "MatchData#[Symbol] returns nil on non-matching named matches"
fails "MatchData#[Symbol] raises an IndexError if there is no named match corresponding to the Symbol"
fails "MatchData#[Symbol] raises an IndexError if there is no named match corresponding to the String"
fails "MatchData#[Symbol] returns matches in the String's encoding"

fails "MatchData#begin returns the offset of the start of the nth element"
fails "MatchData#begin returns nil when the nth match isn't found"
fails "MatchData#begin returns the offset for multi byte strings"
fails "MatchData#begin returns the offset for multi byte strings with unicode regexp"

fails "MatchData#end returns the offset of the end of the nth element"
fails "MatchData#end returns nil when the nth match isn't found"
fails "MatchData#end returns the offset for multi byte strings"
fails "MatchData#end returns the offset for multi byte strings with unicode regexp"

fails "MatchData#names returns an Array"
fails "MatchData#names sets each element to a String"
fails "MatchData#names returns the names of the named capture groups"
fails "MatchData#names returns [] if there were no named captures"
fails "MatchData#names returns each name only once"
fails "MatchData#names equals Regexp#names"

fails "MatchData#offset returns a two element array with the begin and end of the nth match"
fails "MatchData#offset returns [nil, nil] when the nth match isn't found"
fails "MatchData#offset returns the offset for multi byte strings"
fails "MatchData#offset returns the offset for multi byte strings with unicode regexp"

fails "MatchData#regexp returns the pattern used in the match"

fails "String#sub with pattern, replacement supports \\G which matches at the beginning of the string"

fails "String#gsub with pattern and replacement replaces \\k named backreferences with the regexp's corresponding capture"
14 changes: 1 addition & 13 deletions spec/rubyspecs
Original file line number Diff line number Diff line change
@@ -64,19 +64,7 @@ corelib/core/kernel/tap_spec
corelib/core/kernel/to_s_spec
corelib/core/kernel/warn_spec

corelib/core/matchdata/captures_spec
corelib/core/matchdata/element_reference_spec
corelib/core/matchdata/inspect_spec
corelib/core/matchdata/length_spec
corelib/core/matchdata/offset_spec
corelib/core/matchdata/post_match_spec
corelib/core/matchdata/pre_match_spec
corelib/core/matchdata/regexp_spec
corelib/core/matchdata/size_spec
corelib/core/matchdata/string_spec
corelib/core/matchdata/to_a_spec
corelib/core/matchdata/to_s_spec
corelib/core/matchdata/values_at_spec
corelib/core/matchdata

corelib/core/module/class_variable_get_spec
corelib/core/module/class_variable_set_spec