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: 1e7559a6f641
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: adf9d71093ed
Choose a head ref
  • 4 commits
  • 3 files changed
  • 2 contributors

Commits on Mar 25, 2015

  1. Copy the full SHA
    2a89a7c View commit details
  2. Copy the full SHA
    2c779fd View commit details
  3. Copy the full SHA
    47bf927 View commit details
  4. Merge pull request #776 from vais/rindex

    String#rindex fully compliant with rubyspec
    elia committed Mar 25, 2015
    Copy the full SHA
    adf9d71 View commit details
Showing with 30 additions and 45 deletions.
  1. +29 −31 opal/corelib/string.rb
  2. +1 −1 spec/corelib
  3. +0 −13 spec/filters/bugs/string.rb
60 changes: 29 additions & 31 deletions opal/corelib/string.rb
Original file line number Diff line number Diff line change
@@ -711,48 +711,46 @@ def reverse

alias reverse! <<

# TODO handle case where search is regexp
def rindex(search, offset = undefined)
%x{
var search_type = (search == null ? Opal.NilClass : search.constructor);
if (search_type != String && search_type != RegExp) {
var msg = "type mismatch: " + search_type + " given";
#{raise TypeError.new(`msg`)};
}
if (self.length == 0) {
return search.length == 0 ? 0 : nil;
}
var i, m, r, _m;
var result = -1;
if (offset != null) {
if (offset === undefined) {
offset = self.length;
} else {
offset = #{Opal.coerce_to(`offset`, Integer, :to_int)};
if (offset < 0) {
offset = self.length + offset;
}
if (search_type == String) {
result = self.lastIndexOf(search, offset);
}
else {
result = self.substr(0, offset + 1).$reverse().search(search);
if (result !== -1) {
result = offset - result;
offset += self.length;
if (offset < 0) {
return nil;
}
}
}
else {
if (search_type == String) {
result = self.lastIndexOf(search);
}
else {
result = self.$reverse().search(search);
if (result !== -1) {
result = self.length - 1 - result;
if (search.$$is_regexp) {
m = null;
r = new RegExp(search.source, 'g' + (search.multiline ? 'm' : '') + (search.ignoreCase ? 'i' : ''));
while (true) {
_m = r.exec(self);
if (_m === null || _m.index > offset) {
break;
}
m = _m;
r.lastIndex = m.index + 1;
}
if (m === null) {
#{$~ = nil}
i = -1;
} else {
#{MatchData.new `r`, `m`};
i = m.index;
}
} else {
search = #{Opal.coerce_to(`search`, String, :to_str)};
i = self.lastIndexOf(search, offset);
}
return result === -1 ? nil : result;
return i === -1 ? nil : i;
}
end

2 changes: 1 addition & 1 deletion spec/corelib
13 changes: 0 additions & 13 deletions spec/filters/bugs/string.rb
Original file line number Diff line number Diff line change
@@ -40,19 +40,6 @@
fails "String#lines uses $/ as the separator when none is given"
fails "String#lines yields subclass instances for subclasses"

fails "String#rindex with object raises a TypeError if obj isn't a String, Fixnum or Regexp"
fails "String#rindex with object tries to convert obj to a string via to_str"
fails "String#rindex with String ignores string subclasses"
fails "String#rindex with String returns nil if the substring isn't found"
fails "String#rindex with String raises a TypeError when given offset is nil"
fails "String#rindex with Regexp behaves the same as String#rindex(string) for escaped string regexps"
fails "String#rindex with Regexp returns the index of the first match from the end of string of regexp"
fails "String#rindex with Regexp sets $~ to MatchData of match and nil when there's none"
fails "String#rindex with Regexp starts the search at the given offset"
fails "String#rindex with Regexp supports \\G which matches at the given start offset"
fails "String#rindex with Regexp tries to convert start_offset to an integer via to_int"
fails "String#rindex with Regexp raises a TypeError when given offset is nil"

fails "String#slice with Range calls to_int on range arguments"

fails "String#split with String returns subclass instances based on self"