Function: cl-nsubstitute

cl-nsubstitute is an autoloaded and byte-compiled function defined in cl-seq.el.gz.

Signature

(cl-nsubstitute NEW OLD SEQ [KEYWORD VALUE]...)

Documentation

Substitute NEW for OLD in SEQ.

This is a destructive function; it reuses the storage of SEQ whenever possible.

Keywords supported: :test :test-not :key :count :start :end :from-end

View in manual

Aliases

nsubstitute (obsolete since 27.1)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/cl-seq.el.gz
;;;###autoload
(defun cl-nsubstitute (new old seq &rest cl-keys)
  "Substitute NEW for OLD in SEQ.
This is a destructive function; it reuses the storage of SEQ whenever possible.
\nKeywords supported:  :test :test-not :key :count :start :end :from-end
\n(fn NEW OLD SEQ [KEYWORD VALUE]...)"
  (declare (important-return-value t))
  (cl--parsing-keywords ( :test :test-not :key :if :if-not :count
                          (:start 0) :end :from-end) ()
    (let* ((stringp (stringp seq))
           (seq (if (stringp seq) (string-to-vector seq) seq))
           (len (length seq)))
      (or (eq old new) (<= (or cl-count (setq cl-count len)) 0)
          (if (and (listp seq) (or (not cl-from-end) (> cl-count (/ len 2))))
              (let ((p (nthcdr cl-start seq)))
                (setq cl-end (- (or cl-end len) cl-start))
                (while (and p (> cl-end 0) (> cl-count 0))
                  (if (cl--check-test old (car p))
                      (progn
                        (setcar p new)
                        (setq cl-count (1- cl-count))))
                  (setq p (cdr p) cl-end (1- cl-end))))
	    (or cl-end (setq cl-end len))
            (if cl-from-end
                (while (and (< cl-start cl-end) (> cl-count 0))
                  (setq cl-end (1- cl-end))
                  (if (cl--check-test old (elt seq cl-end))
                      (progn
                        (setf (elt seq cl-end) new)
                        (setq cl-count (1- cl-count)))))
              (while (and (< cl-start cl-end) (> cl-count 0))
                (if (cl--check-test old (aref seq cl-start))
                    (progn
                      (aset seq cl-start new)
                      (setq cl-count (1- cl-count))))
                (setq cl-start (1+ cl-start))))))
      (if stringp (concat seq) seq))))