[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#36444: [PATCH] Improved regexp-opt KEEP-ORDER check

From: Mattias Engdegård
Subject: bug#36444: [PATCH] Improved regexp-opt KEEP-ORDER check
Date: Thu, 4 Jul 2019 13:52:31 +0200

3 juli 2019 kl. 21.29 skrev Noam Postavsky <address@hidden>:
> You don't actually need this save-match-data, right?  Because there is
> already one at the top level of the function (which I'm also not sure is
> really needed, but probably best not to touch that).

Thank you! I don't know how I missed the existing save-match-data. Removed.

> IMO, a dolist + catch & throw would be a bit more readable; it took me
> some puzzling to realize that the early exit was the "non-optimized"
> case.
>  (and keep-order
>       (let* ((case-fold-search nil)
>              (alts (mapconcat #'regexp-quote strings "\\|")))
>         (and (catch 'has-prefix
>                (dolist (s strings)
>                  (unless (and (string-match alts s)
>                               (= (match-end 0) (length s)))
>                    (throw 'has-prefix s))))
>              (concat (or open "\\(?:") alts "\\)"))))

Not too fond of that either, really; catch/throw somehow seems more heavyweight 
than warranted by the situation.
Initially I used cl-every, but ran into the usual bootstrap problems.
Here are two alternatives:

(1) Same as before, but with a comment about what tripped you up:

>         (and (let ((s strings))
>                (while (and s
>                            (string-match alts (car s))
>                            (= (match-end 0) (length (car s))))
>                  (setq s (cdr s)))
>                ;; If we exited early, we found evidence that
>                ;; regexp-opt-group cannot be used.
>                s)
>              (concat (or open "\\(?:") alts "\\)")))))

(2) Using cl-loop:

>         (and (not (cl-loop
>                    for s in strings
>                    always (and (string-match alts s)
>                                (= (match-end 0) (length s)))))
>              (concat (or open "\\(?:") alts "\\)")))))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]