The following message is a courtesy copy of an article
that has been posted to comp.emacs.xemacs as well.
Ar an fichiú lá de mí Feabhra, scríobh Dirk Joos:
> > (length (replace-in-string (replace-in-string
(number-to-string number)
> > "[0-9]+\\." "") "0$" ""))
[...]
Thanks for your quick answer. That explains why my function is working.
But I still don't understand why "0*$" leads to an infinite loop.
Well, "0*$" means “zero or more 0 characters, anchored to the end of the
string.” That will match any string, much like "$", "^" or
".*" So the loop
on line 751 of subr.el never gets the chance to terminate, because the match
never runs out of results.
Perhaps we should check that the regexp never matches the empty string?
replace-regexp-in-string jumps through hoops to advance the match data
pointer by one char on each replace, but I believe it would be healthier to
give an error on such a regexp.
It should work too. Why doesn't it?
Hmm, that “it should work” is arguable, honestly ;-) .
lisp/ChangeLog addition:
2005-02-20 Aidan Kehoe <kehoea(a)parhasard.net>
* subr.el (replace-in-string):
Throw an invalid-argument error if the pattern matches the empty
string, preventing an infinite loop in that case.
Index: subr.el
===================================================================
RCS file: /pack/xemacscvs/XEmacs/xemacs/lisp/subr.el,v
retrieving revision 1.35
diff -u -r1.35 subr.el
--- subr.el 2004/06/17 11:29:39 1.35
+++ subr.el 2005/02/20 18:51:02
@@ -738,6 +738,9 @@
`\\E' means terminate the effect of any `\\U' or `\\L'."
(check-argument-type 'stringp str)
(check-argument-type 'stringp newtext)
+ (if (string-match regexp "")
+ (error 'invalid-argument
+ "Can't replace the empty string. Check caller's regexp."))
(if (> (length str) 50)
(let ((cfs case-fold-search))
(with-temp-buffer
--
“Ah come on now Ted, a Volkswagen with a mind of its own, driving all over
the place and going mad, if that’s not scary I don’t know what is.”