From 5d71eed1a7f0a70db013de59cd7e95bdca0d5c0e Mon Sep 17 00:00:00 2001 From: Kasumi Hanazuki Date: Thu, 13 Aug 2020 03:37:32 +0000 Subject: 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] --- test/ruby/test_string.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test') 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 -- cgit v1.2.3