summaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authorLars Ingebrigtsen <larsi@gnus.org>2020-09-25 01:53:07 +0200
committerLars Ingebrigtsen <larsi@gnus.org>2020-09-25 01:53:16 +0200
commit7f9ad5980ce2e998ef57a95c2283d1a87d5613d1 (patch)
tree8d55519c7fb29bd8220b8450da4953e517b54a45 /lisp
parente51a98b0c2d35648c2d054486f7ba5869e24e4cf (diff)
downloademacs-7f9ad5980ce2e998ef57a95c2283d1a87d5613d1.tar.gz
Fix replace-in-string multibyteness problems with string-search
* lisp/subr.el (replace-in-string): Simplify by using the new string-search function (bug#43598).
Diffstat (limited to 'lisp')
-rw-r--r--lisp/subr.el43
1 files changed, 12 insertions, 31 deletions
diff --git a/lisp/subr.el b/lisp/subr.el
index 377d914718a..0f72b382fe8 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -4430,37 +4430,18 @@ Unless optional argument INPLACE is non-nil, return a new string."
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)))
- (if (not (= if (length fromstring)))
- ;; We didn't have a match after all.
- (setq i (1+ i))
- ;; We had one, so gather the previous part and the
- ;; substitution.
- (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))
+ "Replace FROMSTRING with TOSTRING in INSTRING each time it occurs."
+ (declare (pure t))
+ (let ((start 0)
+ (result nil)
+ pos)
+ (while (setq pos (string-search fromstring instring start))
+ (unless (= start pos)
+ (push (substring instring start pos) result))
+ (push tostring result)
+ (setq start (+ start (length fromstring))))
+ (unless (= start pos)
+ (push (substring instring start pos) result))
(apply #'concat (nreverse result))))
(defun replace-regexp-in-string (regexp rep string &optional