[PATCH] Re: replace-in-string infloops
Stephen J. Turnbull
stephen at xemacs.org
Sun Feb 20 22:47:13 EST 2005
Moving discussion to xemacs-beta. Reply-To & MFT set.
Aidan> lisp/ChangeLog addition:
Aidan> 2005-02-20 Aidan Kehoe <kehoea at parhasard.net>
Aidan> * subr.el (replace-in-string): Throw an
Aidan> invalid-argument error if the pattern matches the empty
Aidan> string, preventing an infinite loop in that case.
The semantics of a regexp that matches only the empty string at point
are well-defined: the replacement string should be inserted once.
OTOH, if it matches a non-empty string, it should replace that string
_once_ (and due to the default greedy semantics, will prefer it to the
Note that replace-regexp-in-string (recently? synched to GNU 21.2)
just advances by one after doing the replacement for a null match. I
didn't understand this when it was suggested (in either this context
or a similar one) a couple months ago. I still think this is
incorrect: note that
(replace-regexp-in-string "a*" "b" "ac") => "bbc"
and I'm pretty sure that with a similar adjustment
(replace-in-string "ac" "a*" "b") => "bbc"
(or maybe "bbcb") but (IMO) the correct result for both is "bcb". The
reason for the actual result is that after finding and replacing the
match "a", we'll be looking at "c", and match and replace the null
string, and only then move past the ?c.
But I would argue the algorithm should match the null string at point
iff it has moved since the last replacement. So it matches "a"
(because it's greedy) and replaces it. Now looking at "c" it matches
the null string, but it hasn't moved so it should ignore that match.
Skipping past ?c, it now is looking at "", which it does match, and
should be replaced by "b", giving "bcb".
We should review the C code for this bug (the lack of trailing "b" is
arguably convenient, but the replacement of a single "a" with "bb" is
definitely a bug, right?)
We should review buffer code for this bug.
We should check current GNU and notify them if they're busted.
21.4 will need these patches, I'm pretty sure.
Institute of Policy and Planning Sciences http://turnbull.sk.tsukuba.ac.jp
University of Tsukuba Tennodai 1-1-1 Tsukuba 305-8573 JAPAN
Ask not how you can "do" free software business;
ask what your business can "do for" free software.
More information about the XEmacs-Beta