aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias EngdegÄrd2019-06-29 11:10:36 +0200
committerMattias EngdegÄrd2019-06-29 11:12:27 +0200
commitf1d414b98f2df3d31abfa710ecbbc0223f73aed1 (patch)
tree5a7ce1d19a0d32a6af2825501766e88d63bc9914
parent1dfb2f361595076d1a3e61a46b80470caf259b41 (diff)
downloademacs-f1d414b98f2df3d31abfa710ecbbc0223f73aed1.tar.gz
emacs-f1d414b98f2df3d31abfa710ecbbc0223f73aed1.zip
Allow empty argument to `regexp-opt-charset'
* test/lisp/emacs-lisp/regexp-opt-tests.el (regexp-opt-charset): Handle nil argument, and use regexp-quote for singletons. * lisp/emacs-lisp/regexp-opt.el (regexp-opt-charset): Expand tests.
-rw-r--r--lisp/emacs-lisp/regexp-opt.el20
-rw-r--r--test/lisp/emacs-lisp/regexp-opt-tests.el29
2 files changed, 36 insertions, 13 deletions
diff --git a/lisp/emacs-lisp/regexp-opt.el b/lisp/emacs-lisp/regexp-opt.el
index 00f72e284ad..b6104f22e7d 100644
--- a/lisp/emacs-lisp/regexp-opt.el
+++ b/lisp/emacs-lisp/regexp-opt.el
@@ -279,7 +279,9 @@ Merges keywords to avoid backtracking in Emacs's regexp matcher."
279 279
280(defun regexp-opt-charset (chars) 280(defun regexp-opt-charset (chars)
281 "Return a regexp to match a character in CHARS. 281 "Return a regexp to match a character in CHARS.
282CHARS should be a list of characters." 282CHARS should be a list of characters.
283If CHARS is the empty list, the return value is a regexp that
284never matches anything."
283 ;; The basic idea is to find character ranges. Also we take care in the 285 ;; The basic idea is to find character ranges. Also we take care in the
284 ;; position of character set meta characters in the character set regexp. 286 ;; position of character set meta characters in the character set regexp.
285 ;; 287 ;;
@@ -326,13 +328,15 @@ CHARS should be a list of characters."
326 (while (>= end start) 328 (while (>= end start)
327 (setq charset (format "%s%c" charset start)) 329 (setq charset (format "%s%c" charset start))
328 (setq start (1+ start))))) 330 (setq start (1+ start)))))
329 ;; 331
330 ;; Make sure a caret is not first and a dash is first or last. 332 ;; Make sure that ] is first, ^ is not first, - is first or last.
331 (if (and (string-equal charset "") (string-equal bracket "")) 333 (let ((all (concat bracket charset caret dash)))
332 (if (string-equal dash "") 334 (pcase (length all)
333 "\\^" ; [^] is not a valid regexp 335 (0 regexp-unmatchable)
334 (concat "[" dash caret "]")) 336 (1 (regexp-quote all))
335 (concat "[" bracket charset caret dash "]")))) 337 (_ (if (string-equal all "^-")
338 "[-^]"
339 (concat "[" all "]")))))))
336 340
337 341
338(defun regexp-opt--contains-prefix (strings) 342(defun regexp-opt--contains-prefix (strings)
diff --git a/test/lisp/emacs-lisp/regexp-opt-tests.el b/test/lisp/emacs-lisp/regexp-opt-tests.el
index 1fc49909d3e..927de8c6a5f 100644
--- a/test/lisp/emacs-lisp/regexp-opt-tests.el
+++ b/test/lisp/emacs-lisp/regexp-opt-tests.el
@@ -1,4 +1,4 @@
1;;; regexp-tests.el --- Test suite for regular expression handling. 1;;; regexp-opt-tests.el --- Tests for regexp-opt.el
2 2
3;; Copyright (C) 2013-2019 Free Software Foundation, Inc. 3;; Copyright (C) 2013-2019 Free Software Foundation, Inc.
4 4
@@ -25,9 +25,28 @@
25 25
26(require 'regexp-opt) 26(require 'regexp-opt)
27 27
28(ert-deftest regexp-test-regexp-opt () 28(ert-deftest regexp-opt-charset ()
29 "Test the `compilation-error-regexp-alist' regexps. 29 (should (equal (regexp-opt-charset '(?a ?b ?a)) "[ab]"))
30The test data is in `compile-tests--test-regexps-data'." 30 (should (equal (regexp-opt-charset '(?D ?d ?B ?a ?b ?C ?7 ?a ?c ?A))
31 (should (string-match (regexp-opt-charset '(?^)) "a^b"))) 31 "[7A-Da-d]"))
32 (should (equal (regexp-opt-charset '(?a)) "a"))
33
34 (should (equal (regexp-opt-charset '(?^)) "\\^"))
35 (should (equal (regexp-opt-charset '(?-)) "-"))
36 (should (equal (regexp-opt-charset '(?\])) "]"))
37 (should (equal (regexp-opt-charset '(?^ ?\])) "[]^]"))
38 (should (equal (regexp-opt-charset '(?^ ?-)) "[-^]"))
39 (should (equal (regexp-opt-charset '(?- ?\])) "[]-]"))
40 (should (equal (regexp-opt-charset '(?- ?\] ?^)) "[]^-]"))
41
42 (should (equal (regexp-opt-charset '(?^ ?a)) "[a^]"))
43 (should (equal (regexp-opt-charset '(?- ?a)) "[a-]"))
44 (should (equal (regexp-opt-charset '(?\] ?a)) "[]a]"))
45 (should (equal (regexp-opt-charset '(?^ ?\] ?a)) "[]a^]"))
46 (should (equal (regexp-opt-charset '(?^ ?- ?a)) "[a^-]"))
47 (should (equal (regexp-opt-charset '(?- ?\] ?a)) "[]a-]"))
48 (should (equal (regexp-opt-charset '(?- ?\] ?^ ?a)) "[]a^-]"))
49
50 (should (equal (regexp-opt-charset '()) regexp-unmatchable)))
32 51
33;;; regexp-tests.el ends here. 52;;; regexp-tests.el ends here.