Permalink
Browse files

Merge pull request #1342 from wrozka/string_slice

String slice
  • Loading branch information...
2 parents 7d79a58 + 61207e8 commit df45c821bb600cf31f7a95a48ecdab17ed6a97de @rue rue committed Oct 23, 2011
Showing with 9 additions and 18 deletions.
  1. +9 −13 kernel/common/string.rb
  2. +0 −5 spec/tags/19/ruby/core/string/slice_tags.txt
View
@@ -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)
@@ -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

0 comments on commit df45c82

Please sign in to comment.