mirror of
https://github.com/ruby/ruby.git
synced 2025-08-28 15:36:16 +02:00
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]
This commit is contained in:
parent
69b5241c36
commit
5d71eed1a7
Notes:
git
2020-08-13 20:51:24 +09:00
2 changed files with 26 additions and 23 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue