From: daniel@dan42.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:96702] [Ruby master Bug#11014] String#partition doesn't return correct result on zero-width match
Date: Tue, 07 Jan 2020 13:23:47 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-83688.20200107132345.8a73855e89adb963@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11014.20150329145156@ruby-lang.org
Issue #11014 has been updated by Dan0042 (Daniel DeLorme).
IIRC this has to do with zero-length matches being ignored in certain conditions, in particular having to do with repeating/multiple matches.
if `"foo".split(/\A/)` was `["","foo"]`
then `"foo".split(//)` would have to be `["","f","o","o"]`
and `"foo".split(/\G/)` could result in infinite loop matching `["","","","","",..."foo"]`
But I don't understand why `partition` doesn't behave like `match`.
Note that gsub has different behavior:
`"foo".gsub(/\G/,'_') #=> "_f_o_o_"`
`"foo".gsub(//,'_') #=> "_f_o_o_"`
explained better than I ever could:
https://www.regular-expressions.info/zerolength.html
----------------------------------------
Bug #11014: String#partition doesn't return correct result on zero-width match
https://bugs.ruby-lang.org/issues/11014#change-83688
* 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/
next prev parent reply other threads:[~2020-01-07 13:24 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-11014.20150329145156@ruby-lang.org>
2015-03-29 14:51 ` [ruby-core:68675] [Ruby trunk - Bug #11014] [Open] String#partition doesn't return correct result on zero-width match janko.marohnic
2015-04-18 17:09 ` [ruby-core:68927] [Ruby trunk - Bug #11014] [Assigned] " nobu
2020-01-07 10:04 ` [ruby-core:96701] [Ruby master Bug#11014] " sawadatsuyoshi
2020-01-07 13:23 ` daniel [this message]
2020-01-14 2:49 ` [ruby-core:96830] " mame
2020-01-16 5:31 ` [ruby-core:96885] " akr
2020-01-16 5:44 ` [ruby-core:96887] " akr
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-83688.20200107132345.8a73855e89adb963@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).