aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorKasumi Hanazuki <kasumi@rollingapple.net>2020-08-13 03:37:32 +0000
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-08-13 20:50:50 +0900
commit5d71eed1a7f0a70db013de59cd7e95bdca0d5c0e (patch)
tree99bc9c8b9bf059d8db50721841cb43bba8ca12d1 /test
parent69b5241c360aa180550538dba1c7bf0a6cc8cc22 (diff)
downloadruby-5d71eed1a7f0a70db013de59cd7e95bdca0d5c0e.tar.gz
rb_str_{partition,rpartition}_m: Handle /\K/ in pattern
When the pattern given to String#partition and String#rpartition contain a /\K/ (lookbehind) operator, the methods return strings sliced at incorrect positions. ``` # without patch "abcdbce".partition(/b\Kc/) # => ["a", "c", "cdbce"] "abcdbce".rpartition(/b\Kc/) # => ["abcd", "c", "ce"] ``` This patch fixes the problem by using BEG(0) instead of the return value of rb_reg_search. ``` # with patch "abcdbce".partition(/b\Kc/) # => ["ab", "c", "dbce"] "abcdbce".rpartition(/b\Kc/) # => ["abcdb", "c", "e"] ``` As a side-effect this patch makes String#partition 2x faster when the pattern is a costly Regexp by performing Regexp search only once, which was unexpectedly done twice in the original implementation. Fixes [Bug #17119]
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_string.rb4
1 files changed, 4 insertions, 0 deletions
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index c4c7d55f00..308ba80373 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -2603,6 +2603,8 @@ CODE
assert_equal("hello", hello, bug)
assert_equal(["", "", "foo"], "foo".partition(/^=*/))
+
+ assert_equal([S("ab"), S("c"), S("dbce")], S("abcdbce").partition(/b\Kc/))
end
def test_rpartition
@@ -2627,6 +2629,8 @@ CODE
hello = "hello"
hello.rpartition("hi").map(&:upcase!)
assert_equal("hello", hello, bug)
+
+ assert_equal([S("abcdb"), S("c"), S("e")], S("abcdbce").rpartition(/b\Kc/))
end
def test_setter