diff options
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/subr.el | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lisp/subr.el b/lisp/subr.el index b1537fd27a6..b8331760e34 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -4411,6 +4411,36 @@ Unless optional argument INPLACE is non-nil, return a new string." (aset newstr i tochar))) newstr)) +(defun replace-in-string (fromstring tostring instring) + "Replace FROMSTRING with TOSTRING in INSTRING each time it occurs. +This function returns a freshly created string." + (declare (side-effect-free t)) + (let ((i 0) + (start 0) + (result nil)) + (while (< i (length instring)) + (if (eq (aref instring i) + (aref fromstring 0)) + ;; See if we're in a match. + (let ((ii i) + (if 0)) + (while (and (< ii (length instring)) + (< if (length fromstring)) + (eq (aref instring ii) + (aref fromstring if))) + (setq ii (1+ ii) + if (1+ if))) + (when (= if (length fromstring)) + (when (not (= start i)) + (push (substring instring start i) result)) + (push tostring result) + (setq i ii + start ii))) + (setq i (1+ i)))) + (when (not (= start i)) + (push (substring instring start i) result)) + (apply #'concat (nreverse result)))) + (defun replace-regexp-in-string (regexp rep string &optional fixedcase literal subexp start) "Replace all matches for REGEXP with REP in STRING. |