[ruby-core:96701] [Ruby master Bug#11014] String#partition doesn't return correct result on zero-width match
From:
sawadatsuyoshi@...
Date:
2020-01-07 10:04:41 UTC
List:
ruby-core #96701
Issue #11014 has been updated by sawa (Tsuyoshi Sawada).
The problem is not just for `partition`, but is also involves `split` and `scan`.
I think your regex `/^=*/` is unnecessarily complex. Your point can be shown by `/\A/`, which is simpler.
I tried with four regex patterns `/\A/`, `/\A.*/`, `/\z/`, `/.*\z/`, and compared methods `split`, `partition`, `scan`. The result of the first example in each group below matches the second and the third, and the fourth one matches the middle element. So far, so good.
```ruby
"foo".match(/\z/).then{[_1.pre_match, _1[0], _1.post_match]} # => ["foo", "", ""]
"foo".split(/(\z)/, -1) # => ["foo", "", ""]
"foo".partition(/\z/) # => ["foo", "", ""]
"foo".scan(/\z/) # => [""]
```
```ruby
"foo".match(/\A.*/).then{[_1.pre_match, _1[0], _1.post_match]} # => ["", "foo", ""]
"foo".split(/(\A.*)/, -1) # => ["", "foo", ""]
"foo".partition(/\A.*/) # => ["", "foo", ""]
"foo".scan(/\A.*/) # => ["foo"]
```
In the following, we see problems:
```ruby
"foo".match(/\A/).then{[_1.pre_match, _1[0], _1.post_match]} # => ["", "", "foo"]
"foo".split(/(\A)/, -1) # => ["foo"]
"foo".partition(/\A/) # => ["foo", "", ""]
"foo".scan(/\A/) # => [""]
```
```ruby
"foo".match(/.*\z/).then{[_1.pre_match, _1[0], _1.post_match]} # => ["", "foo", ""]
"foo".split(/(.*\z)/, -1) # => ["", "foo", ""]
"foo".partition(/.*\z/) # => ["", "foo", ""]
"foo".scan(/.*\z/) # => ["foo", ""]
```
The problematic cases and their expected values (in terms of consistency) are:
```ruby
"foo".split(/(\A)/, -1) # => ["foo"], expected [ "", "", "foo"]
"foo".partition(/\A/) # => ["foo", "", ""], expected ["", "", "foo"]
"foo".scan(/.*\z/) # => ["foo", ""], expected ["foo"]
```
The case described in the issue is the second case above.
----------------------------------------
Bug #11014: String#partition doesn't return correct result on zero-width match
https://bugs.ruby-lang.org/issues/11014#change-83687
* Author: janko (Janko Marohnić)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version:
* ruby -v: ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
First, to see how `String#match` works on my example:
~~~ruby
match = "foo".match(/^=*/)
match.pre_match #=> ""
match[0] #=> ""
match.post_match #=> "foo"
~~~
Now, if I used `String#partition` instead of `match`, I'd expect to get `["", "", "foo"]` (pre_match, match, post_match). However
~~~ruby
"foo".partition(/^=*/) #=> ["foo", "", ""]
~~~
`String#rpartition` returns the correct result (with the same regex).
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>