From f1d414b98f2df3d31abfa710ecbbc0223f73aed1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Sat, 29 Jun 2019 11:10:36 +0200 Subject: [PATCH] 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. --- lisp/emacs-lisp/regexp-opt.el | 20 +++++++++------- test/lisp/emacs-lisp/regexp-opt-tests.el | 29 ++++++++++++++++++++---- 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." (defun regexp-opt-charset (chars) "Return a regexp to match a character in CHARS. -CHARS should be a list of characters." +CHARS should be a list of characters. +If CHARS is the empty list, the return value is a regexp that +never matches anything." ;; The basic idea is to find character ranges. Also we take care in the ;; position of character set meta characters in the character set regexp. ;; @@ -326,13 +328,15 @@ CHARS should be a list of characters." (while (>= end start) (setq charset (format "%s%c" charset start)) (setq start (1+ start))))) - ;; - ;; Make sure a caret is not first and a dash is first or last. - (if (and (string-equal charset "") (string-equal bracket "")) - (if (string-equal dash "") - "\\^" ; [^] is not a valid regexp - (concat "[" dash caret "]")) - (concat "[" bracket charset caret dash "]")))) + + ;; Make sure that ] is first, ^ is not first, - is first or last. + (let ((all (concat bracket charset caret dash))) + (pcase (length all) + (0 regexp-unmatchable) + (1 (regexp-quote all)) + (_ (if (string-equal all "^-") + "[-^]" + (concat "[" all "]"))))))) (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 @@ -;;; regexp-tests.el --- Test suite for regular expression handling. +;;; regexp-opt-tests.el --- Tests for regexp-opt.el ;; Copyright (C) 2013-2019 Free Software Foundation, Inc. @@ -25,9 +25,28 @@ (require 'regexp-opt) -(ert-deftest regexp-test-regexp-opt () - "Test the `compilation-error-regexp-alist' regexps. -The test data is in `compile-tests--test-regexps-data'." - (should (string-match (regexp-opt-charset '(?^)) "a^b"))) +(ert-deftest regexp-opt-charset () + (should (equal (regexp-opt-charset '(?a ?b ?a)) "[ab]")) + (should (equal (regexp-opt-charset '(?D ?d ?B ?a ?b ?C ?7 ?a ?c ?A)) + "[7A-Da-d]")) + (should (equal (regexp-opt-charset '(?a)) "a")) + + (should (equal (regexp-opt-charset '(?^)) "\\^")) + (should (equal (regexp-opt-charset '(?-)) "-")) + (should (equal (regexp-opt-charset '(?\])) "]")) + (should (equal (regexp-opt-charset '(?^ ?\])) "[]^]")) + (should (equal (regexp-opt-charset '(?^ ?-)) "[-^]")) + (should (equal (regexp-opt-charset '(?- ?\])) "[]-]")) + (should (equal (regexp-opt-charset '(?- ?\] ?^)) "[]^-]")) + + (should (equal (regexp-opt-charset '(?^ ?a)) "[a^]")) + (should (equal (regexp-opt-charset '(?- ?a)) "[a-]")) + (should (equal (regexp-opt-charset '(?\] ?a)) "[]a]")) + (should (equal (regexp-opt-charset '(?^ ?\] ?a)) "[]a^]")) + (should (equal (regexp-opt-charset '(?^ ?- ?a)) "[a^-]")) + (should (equal (regexp-opt-charset '(?- ?\] ?a)) "[]a-]")) + (should (equal (regexp-opt-charset '(?- ?\] ?^ ?a)) "[]a^-]")) + + (should (equal (regexp-opt-charset '()) regexp-unmatchable))) ;;; regexp-tests.el ends here. -- 2.39.2