Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

String slice #1342

Merged
merged 2 commits into from

2 participants

@wrozka

Fixed failing specs around String.slice when regexp and group name are passed.

@rue rue merged commit df45c82 into rubinius:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 23, 2011
  1. @wrozka

    string#slice fixed for a regexp,string arguments

    Pawel Pierzchala authored wrozka committed
  2. @wrozka
This page is out of date. Refresh to see the latest.
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)
View
5 spec/tags/19/ruby/core/string/slice_tags.txt
@@ -1,9 +1,4 @@
-fails:String#slice with Regexp, group returns the capture for the given name
fails:String#slice with Regexp, group returns the last capture for duplicate names
fails:String#slice with Regexp, group returns the innermost capture for nested duplicate names
-fails:String#slice with Regexp, group always taints resulting strings when self or regexp is tainted
-fails:String#slice with Regexp, group returns nil if there is no match
fails:String#slice with Regexp, group raises an IndexError if there is no capture for the given name
fails:String#slice with Regexp, group raises an IndexError when given the empty String as a group name
-fails:String#slice with Regexp, group returns subclass instances
-fails:String#slice with Regexp, group sets $~ to MatchData when there is a match and nil when there's none
Something went wrong with that request. Please try again.