diff options
| author | Mattias EngdegÄrd | 2023-07-30 15:30:38 +0200 |
|---|---|---|
| committer | Mattias EngdegÄrd | 2023-07-30 18:12:19 +0200 |
| commit | 2b8796eea1979fe6891ab9d80cd126fe8980167a (patch) | |
| tree | 0e49ad64bf11942baf797aecf87723ecc8842f00 | |
| parent | ba60070b81c4b507b856269031a17b99e9f5e77c (diff) | |
| download | emacs-2b8796eea1979fe6891ab9d80cd126fe8980167a.tar.gz emacs-2b8796eea1979fe6891ab9d80cd126fe8980167a.zip | |
Fix rx wrong-code bug: ranges starting with ^
(rx (in (?^ . ?a))) was incorrectly translated to "[^-a]".
Change it so that we get "[_-a^]" instead.
* lisp/emacs-lisp/rx.el (rx--generate-alt): Split ranges starting with
`^` occurring first in a non-negated character alternative.
* test/lisp/emacs-lisp/rx-tests.el (rx-any): Add and adapt tests.
(cherry picked from commit 5f5d668ac7917d61e9366fe0c3efd7b542671c3d)
| -rw-r--r-- | lisp/emacs-lisp/rx.el | 20 | ||||
| -rw-r--r-- | test/lisp/emacs-lisp/rx-tests.el | 20 |
2 files changed, 28 insertions, 12 deletions
diff --git a/lisp/emacs-lisp/rx.el b/lisp/emacs-lisp/rx.el index 46f61c26bc4..30195cbae32 100644 --- a/lisp/emacs-lisp/rx.el +++ b/lisp/emacs-lisp/rx.el | |||
| @@ -445,13 +445,19 @@ classes." | |||
| 445 | (setcar dash-l ?.)) ; Reduce --x to .-x | 445 | (setcar dash-l ?.)) ; Reduce --x to .-x |
| 446 | (setq items (nconc items '((?- . ?-)))))) | 446 | (setq items (nconc items '((?- . ?-)))))) |
| 447 | 447 | ||
| 448 | ;; Deal with leading ^ and range ^-x. | 448 | ;; Deal with leading ^ and range ^-x in non-negated set. |
| 449 | (when (and (consp (car items)) | 449 | (when (and (eq (car-safe (car items)) ?^) |
| 450 | (eq (caar items) ?^) | 450 | (not negated)) |
| 451 | (cdr items)) | 451 | (if (eq (cdar items) ?^) |
| 452 | ;; Move ^ and ^-x to second place. | 452 | ;; single leading ^ |
| 453 | (setq items (cons (cadr items) | 453 | (when (cdr items) |
| 454 | (cons (car items) (cddr items))))) | 454 | ;; Move the ^ to second place. |
| 455 | (setq items (cons (cadr items) | ||
| 456 | (cons (car items) (cddr items))))) | ||
| 457 | ;; Split ^-x to _-x^ | ||
| 458 | (setq items (cons (cons ?_ (cdar items)) | ||
| 459 | (cons '(?^ . ?^) | ||
| 460 | (cdr items)))))) | ||
| 455 | 461 | ||
| 456 | (cond | 462 | (cond |
| 457 | ;; Empty set: if negated, any char, otherwise match-nothing. | 463 | ;; Empty set: if negated, any char, otherwise match-nothing. |
diff --git a/test/lisp/emacs-lisp/rx-tests.el b/test/lisp/emacs-lisp/rx-tests.el index 028250b7352..9c8628a8f26 100644 --- a/test/lisp/emacs-lisp/rx-tests.el +++ b/test/lisp/emacs-lisp/rx-tests.el | |||
| @@ -112,23 +112,33 @@ | |||
| 112 | (should (equal (rx (any "]" "^") (any "]" "-") (any "-" "^") | 112 | (should (equal (rx (any "]" "^") (any "]" "-") (any "-" "^") |
| 113 | (not (any "]" "^")) (not (any "]" "-")) | 113 | (not (any "]" "^")) (not (any "]" "-")) |
| 114 | (not (any "-" "^"))) | 114 | (not (any "-" "^"))) |
| 115 | "[]^][]-][-^][^]^][^]-][^-^]")) | 115 | "[]^][]-][-^][^]^][^]-][^^-]")) |
| 116 | (should (equal (rx (any "]" "^" "-") (not (any "]" "^" "-"))) | 116 | (should (equal (rx (any "]" "^" "-") (not (any "]" "^" "-"))) |
| 117 | "[]^-][^]^-]")) | 117 | "[]^-][^]^-]")) |
| 118 | (should (equal (rx (any "^-f") (any "^-f" "-") | ||
| 119 | (any "^-f" "z") (any "^-f" "z" "-")) | ||
| 120 | "[_-f^][_-f^-][_-f^z][_-f^z-]")) | ||
| 121 | (should (equal (rx (not (any "^-f")) (not (any "^-f" "-")) | ||
| 122 | (not (any "^-f" "z")) (not (any "^-f" "z" "-"))) | ||
| 123 | "[^^-f][^^-f-][^^-fz][^^-fz-]")) | ||
| 124 | (should (equal (rx (any "^-f" word) (any "^-f" "-" word)) | ||
| 125 | "[_-f^[:word:]][_-f^[:word:]-]")) | ||
| 126 | (should (equal (rx (not (any "^-f" word)) (not (any "^-f" "-" word))) | ||
| 127 | "[^^-f[:word:]][^^-f[:word:]-]")) | ||
| 118 | (should (equal (rx (any "-" ascii) (any "^" ascii) (any "]" ascii)) | 128 | (should (equal (rx (any "-" ascii) (any "^" ascii) (any "]" ascii)) |
| 119 | "[[:ascii:]-][[:ascii:]^][][:ascii:]]")) | 129 | "[[:ascii:]-][[:ascii:]^][][:ascii:]]")) |
| 120 | (should (equal (rx (not (any "-" ascii)) (not (any "^" ascii)) | 130 | (should (equal (rx (not (any "-" ascii)) (not (any "^" ascii)) |
| 121 | (not (any "]" ascii))) | 131 | (not (any "]" ascii))) |
| 122 | "[^[:ascii:]-][^[:ascii:]^][^][:ascii:]]")) | 132 | "[^[:ascii:]-][^^[:ascii:]][^][:ascii:]]")) |
| 123 | (should (equal (rx (any "-]" ascii) (any "^]" ascii) (any "-^" ascii)) | 133 | (should (equal (rx (any "-]" ascii) (any "^]" ascii) (any "-^" ascii)) |
| 124 | "[][:ascii:]-][]^[:ascii:]][[:ascii:]^-]")) | 134 | "[][:ascii:]-][]^[:ascii:]][[:ascii:]^-]")) |
| 125 | (should (equal (rx (not (any "-]" ascii)) (not (any "^]" ascii)) | 135 | (should (equal (rx (not (any "-]" ascii)) (not (any "^]" ascii)) |
| 126 | (not (any "-^" ascii))) | 136 | (not (any "-^" ascii))) |
| 127 | "[^][:ascii:]-][^]^[:ascii:]][^[:ascii:]^-]")) | 137 | "[^][:ascii:]-][^]^[:ascii:]][^^[:ascii:]-]")) |
| 128 | (should (equal (rx (any "-]^" ascii) (not (any "-]^" ascii))) | 138 | (should (equal (rx (any "-]^" ascii) (not (any "-]^" ascii))) |
| 129 | "[]^[:ascii:]-][^]^[:ascii:]-]")) | 139 | "[]^[:ascii:]-][^]^[:ascii:]-]")) |
| 130 | (should (equal (rx (any "^" lower upper) (not (any "^" lower upper))) | 140 | (should (equal (rx (any "^" lower upper) (not (any "^" lower upper))) |
| 131 | "[[:lower:]^[:upper:]][^[:lower:]^[:upper:]]")) | 141 | "[[:lower:]^[:upper:]][^^[:lower:][:upper:]]")) |
| 132 | (should (equal (rx (any "-" lower upper) (not (any "-" lower upper))) | 142 | (should (equal (rx (any "-" lower upper) (not (any "-" lower upper))) |
| 133 | "[[:lower:][:upper:]-][^[:lower:][:upper:]-]")) | 143 | "[[:lower:][:upper:]-][^[:lower:][:upper:]-]")) |
| 134 | (should (equal (rx (any "]" lower upper) (not (any "]" lower upper))) | 144 | (should (equal (rx (any "]" lower upper) (not (any "]" lower upper))) |
| @@ -143,7 +153,7 @@ | |||
| 143 | "[]-a-][^]-a-]")) | 153 | "[]-a-][^]-a-]")) |
| 144 | (should (equal (rx (any "--]") (not (any "--]")) | 154 | (should (equal (rx (any "--]") (not (any "--]")) |
| 145 | (any "-" "^-a") (not (any "-" "^-a"))) | 155 | (any "-" "^-a") (not (any "-" "^-a"))) |
| 146 | "[].-\\-][^].-\\-][-^-a][^-^-a]")) | 156 | "[].-\\-][^].-\\-][_-a^-][^^-a-]")) |
| 147 | (should (equal (rx (not (any "!a" "0-8" digit nonascii))) | 157 | (should (equal (rx (not (any "!a" "0-8" digit nonascii))) |
| 148 | "[^!0-8a[:digit:][:nonascii:]]")) | 158 | "[^!0-8a[:digit:][:nonascii:]]")) |
| 149 | (should (equal (rx (any) (not (any))) | 159 | (should (equal (rx (any) (not (any))) |