1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-01-03 02:31:03 -08:00

rx: fix `or' ordering by adding argument to regexp-opt

The rx `or' form may reorder its arguments in an unpredictable way,
contrary to user expectation, since it sometimes uses `regexp-opt'.
Add a NOREORDER option to `regexp-opt' for preventing it from
producing a reordered regexp (Bug#34641).

* doc/lispref/searching.texi (Regular Expression Functions):
* etc/NEWS (Lisp Changes in Emacs 27.1):
Describe the new regexp-opt NOREORDER argument.
* lisp/emacs-lisp/regexp-opt.el (regexp-opt): Add NOREORDER.
Make no attempt at regexp improvement if the set of strings contains
a prefix of another string.
(regexp-opt--contains-prefix): New.
* lisp/emacs-lisp/rx.el (rx-or): Call regexp-opt with NOREORDER.
* test/lisp/emacs-lisp/rx-tests.el: Test rx `or' form match order.
This commit is contained in:
Mattias Engdegård 2019-02-24 22:12:52 +01:00
parent dbffbe0881
commit da758046da
5 changed files with 65 additions and 8 deletions

View file

@ -92,5 +92,18 @@
(*? "e") (+? "f") (\?? "g") (?? "h"))))
"a*b+c?d?e*?f+?g??h??")))
(ert-deftest rx-or ()
;; Test or-pattern reordering (Bug#34641).
(let ((s "abc"))
(should (equal (and (string-match (rx (or "abc" "ab" "a")) s)
(match-string 0 s))
"abc"))
(should (equal (and (string-match (rx (or "ab" "abc" "a")) s)
(match-string 0 s))
"ab"))
(should (equal (and (string-match (rx (or "a" "ab" "abc")) s)
(match-string 0 s))
"a"))))
(provide 'rx-tests)
;; rx-tests.el ends here.