ruby-changes:62558
From: Kasumi <ko1@a...>
Date: Wed, 12 Aug 2020 10:01:58 +0900 (JST)
Subject: [ruby-changes:62558] e79cdcf61b (master): string.c(rb_str_split_m): Handle /\K/ correctly
https://git.ruby-lang.org/ruby.git/commit/?id=e79cdcf61b From e79cdcf61b0665d8a9bb309a607227de43e95673 Mon Sep 17 00:00:00 2001 From: Kasumi Hanazuki <kasumi@r...> Date: Tue, 11 Aug 2020 09:32:02 +0000 Subject: string.c(rb_str_split_m): Handle /\K/ correctly Use BEG(0) instead of the result of rb_reg_search to handle the cases when the separator Regexp contains /\K/ (lookbehind) operator. Fixes [Bug #17113] diff --git a/string.c b/string.c index d4ec257..c2cdd00 100644 --- a/string.c +++ b/string.c @@ -8216,11 +8216,12 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L8216 struct re_registers *regs; VALUE match = 0; - for (; (end = rb_reg_search(spat, str, start, 0)) >= 0; + for (; rb_reg_search(spat, str, start, 0) >= 0; (match ? (rb_match_unbusy(match), rb_backref_set(match)) : (void)0)) { match = rb_backref_get(); if (!result) rb_match_busy(match); regs = RMATCH_REGS(match); + end = BEG(0); if (start == end && BEG(0) == END(0)) { if (!ptr) { SPLIT_STR(0, 0); diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 89f5b6c..c4c7d55 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1838,6 +1838,11 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L1838 assert_equal("abc", s) end + def test_split_lookbehind + assert_equal([S("ab"), S("d")], S("abcd").split(/(?<=b)c/)) + assert_equal([S("ab"), S("d")], S("abcd").split(/b\Kc/)) + end + def test_squeeze assert_equal(S("abc"), S("aaabbbbccc").squeeze) assert_equal(S("aa bb cc"), S("aa bb cc").squeeze(S(" "))) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/