diff options
author | Stefan Monnier <[email protected]> | 2000-03-08 23:49:09 +0000 |
---|---|---|
committer | Stefan Monnier <[email protected]> | 2000-03-08 23:49:09 +0000 |
commit | a9853251a342e86260db1005fb0b55639ad7a426 (patch) | |
tree | ee3ce4b4be508f00b3125fded8633b797494bca7 /lisp | |
parent | 433d16a1961e32b95c0299650f4222765415bbb4 (diff) |
(replace-regexps-in-string): Properly handle the case where
we match an empty string.
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/subr.el | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/lisp/subr.el b/lisp/subr.el index 2ae458e57f..29af26732d 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -1229,23 +1229,22 @@ To replace a single match, make REGEXP match up to \\'." (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))))) |