Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

string#slice fixed for a regexp,string arguments

  • Loading branch information...
commit 66c03898aa3abf4af26767a4d0f171dae8821923 1 parent 018a9de
Pawel Pierzchala authored wrozka committed
Showing with 9 additions and 13 deletions.
  1. +9 −13 kernel/common/string.rb
View
22 kernel/common/string.rb
@@ -209,13 +209,12 @@ def =~(pattern)
# a["bye"] #=> nil
def [](index, other = undefined)
unless other.equal?(undefined)
- length = Rubinius::Type.coerce_to(other, Fixnum, :to_int)
-
if index.kind_of? Regexp
- match, str = subpattern(index, length)
+ match, str = subpattern(index, other)
Regexp.last_match = match
return str
else
+ length = Rubinius::Type.coerce_to(other, Fixnum, :to_int)
start = Rubinius::Type.coerce_to(index, Fixnum, :to_int)
return substring(start, length)
end
@@ -2095,23 +2094,20 @@ def modify!
end
def subpattern(pattern, capture)
- # TODO: A part of the functionality here should go into MatchData#[]
match = pattern.match(self)
- if !match or capture >= match.size
- return nil
- end
- if capture < 0
- capture += match.size
- return nil if capture <= 0
+ return nil unless match
+
+ if index = Rubinius::Type.check_convert_type(capture, Fixnum, :to_int)
+ return nil if index >= match.size || -index >= match.size
+ capture = index
end
- start = match.begin(capture)
- count = match.end(capture) - match.begin(capture)
- str = self.substring(start, count)
+ str = match[capture]
str.taint if pattern.tainted?
[match, str]
end
+ private :subpattern
def subpattern_set(pattern, capture, replacement)
unless match = pattern.match(self)
Please sign in to comment.
Something went wrong with that request. Please try again.