# HG changeset patch # User Stefan Monnier # Date 952559349 0 # Node ID 093dcd5f39b21be3a01faabe25f00a936cbeb5ce # Parent 34cb92c8c3e49a6de0dbe92c5aa33b1dc9ecc557 (replace-regexps-in-string): Properly handle the case where we match an empty string. diff -r 34cb92c8c3e4 -r 093dcd5f39b2 lisp/subr.el --- a/lisp/subr.el Wed Mar 08 23:46:09 2000 +0000 +++ b/lisp/subr.el Wed Mar 08 23:49:09 2000 +0000 @@ -1229,23 +1229,22 @@ (while (and (< start l) (string-match regexp string start)) (setq mb (match-beginning 0) me (match-end 0)) - (if (= me mb) - (setq start l ; Matched empty string -- bail out. - matches (list string)) - ;; Generate a replacement for the matched substring. - ;; Operate only on the substring to minimize string consing. - ;; Set up match data for the substring for replacement; - ;; presumably this is likely to be faster than munging the - ;; match data directly in Lisp. - (string-match regexp (setq str (substring string mb me))) - (setq matches - (cons (replace-match (if (stringp rep) - rep - (funcall rep (match-string 0 str))) - fixedcase literal str subexp) - (cons (substring string start mb) ; unmatched prefix - matches))) - (setq start me))) + ;; If we matched the empty string, make sure we advance by one char + (when (= me mb) (setq me (min l (1+ mb)))) + ;; Generate a replacement for the matched substring. + ;; Operate only on the substring to minimize string consing. + ;; Set up match data for the substring for replacement; + ;; presumably this is likely to be faster than munging the + ;; match data directly in Lisp. + (string-match regexp (setq str (substring string mb me))) + (setq matches + (cons (replace-match (if (stringp rep) + rep + (funcall rep (match-string 0 str))) + fixedcase literal str subexp) + (cons (substring string start mb) ; unmatched prefix + matches))) + (setq start me)) ;; Reconstruct a string from the pieces. (setq matches (cons (substring string start l) matches)) ; leftover (apply #'concat (nreverse matches)))))