Skip to content

Commit

Permalink
Update String#sub to operate on dup of self
Browse files Browse the repository at this point in the history
bjfish committed Jun 16, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 07cecbb commit eb33d89
Showing 2 changed files with 3 additions and 3 deletions.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/string/sub_tags.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
fails(regexp):String#sub! with pattern and block raises a RuntimeError if the string is modified while substituting
fails:String#sub with pattern, replacement returns a copy of self when no modification is made
5 changes: 3 additions & 2 deletions truffle/src/main/ruby/core/string.rb
Original file line number Diff line number Diff line change
@@ -744,6 +744,7 @@ def upto(stop, exclusive=false)
def sub(pattern, replacement=undefined)
# Because of the behavior of $~, this is duplicated from sub! because
# if we call sub! from sub, the last_match can't be updated properly.
dup = self.dup

unless valid_encoding?
raise ArgumentError, "invalid byte sequence in #{encoding}"
@@ -769,7 +770,7 @@ def sub(pattern, replacement=undefined)
end

pattern = Rubinius::Type.coerce_to_regexp(pattern, true) unless pattern.kind_of? Regexp
match = pattern.match_from(self, 0)
match = pattern.match_from(dup, 0)

Regexp.last_match = match

@@ -799,7 +800,7 @@ def sub(pattern, replacement=undefined)
ret.append(match.post_match)
tainted ||= val.tainted?
else
return self
return dup
end

ret.taint if tainted

0 comments on commit eb33d89

Please sign in to comment.