Function: byte-optimize-concat

byte-optimize-concat is a byte-compiled function defined in byte-opt.el.gz.

Signature

(byte-optimize-concat FORM)

Documentation

Merge adjacent constant arguments to concat and flatten nested forms.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/byte-opt.el.gz
(defun byte-optimize-concat (form)
  "Merge adjacent constant arguments to `concat' and flatten nested forms."
  (let ((args (cdr form))
        (newargs nil))
    (while args
      (let ((strings nil))
        (while
            (and args
                 (let ((arg (car args)))
                   (pcase arg
                     ;; Merge consecutive constant arguments.
                     ((pred macroexp-const-p)
                      (let ((val (byteopt--eval-const arg)))
                        (and (or (stringp val)
                                 (and (or (listp val) (vectorp val))
                                      (not (memq nil
                                                 (mapcar #'characterp val)))))
                             (progn
                               (push val strings)
                               (setq args (cdr args))
                               t))))
                     ;; Flatten nested `concat' form.
                     (`(concat . ,nested-args)
                      (setq args (append nested-args (cdr args)))
                      t)))))

        (when strings
          (let ((s (apply #'concat (nreverse strings))))
            (when (not (zerop (length s)))
              (push s newargs)))))
      (when args
        (push (car args) newargs)
        (setq args (cdr args))))
    (if (= (length newargs) (length (cdr form)))
        form          ; No improvement.
      (cons 'concat (nreverse newargs)))))