Function: rx--optimise-or-args

rx--optimise-or-args is a byte-compiled function defined in rx.el.gz.

Signature

(rx--optimise-or-args ARGS)

Documentation

Optimise or arguments. Return a new rx form.

Each element of ARGS should have been normalised using rx--normalise-char-pattern.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/rx.el.gz
(defun rx--optimise-or-args (args)
  "Optimise `or' arguments.  Return a new rx form.
Each element of ARGS should have been normalised using
`rx--normalise-char-pattern'."
  (if (null args)
      ;; No arguments.
      '(rx--char-alt nil . nil)         ; FIXME: not `unmatchable'?
    ;; Join consecutive single-char branches into a char alt where possible.
    ;; Ideally we should collect all single-char branches but that might
    ;; alter matching order in some cases.
    (let ((branches nil)
          (prev-char nil))
      (while args
        (let* ((item (car args))
               (item-char (rx--reduce-to-char-alt item)))
          (if item-char
              (setq prev-char (if prev-char
                                  (rx--char-alt-union prev-char item-char)
                                item-char))
            (when prev-char
              (push (cons 'rx--char-alt prev-char) branches)
              (setq prev-char nil))
            (push item branches)))
        (setq args (cdr args)))
      (when prev-char
        (push (cons 'rx--char-alt prev-char) branches))
      (if (cdr branches)
          (cons 'or (nreverse branches))
        (car branches)))))