Mercurial > emacs
comparison lisp/emacs-lisp/regexp-opt.el @ 18014:9492b5c8485a
Initial revision
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 29 May 1997 03:01:51 +0000 |
parents | |
children | 2fec7f622b82 |
comparison
equal
deleted
inserted
replaced
18013:e35b7126595f | 18014:9492b5c8485a |
---|---|
1 ;;; regexp-opt.el --- generate efficient regexps to match strings. | |
2 | |
3 ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Simon Marshall <simon@gnu.ai.mit.edu> | |
6 ;; Keywords: strings, regexps | |
7 ;; Version: 1.04.01 | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
25 | |
26 ;;; Commentary: | |
27 | |
28 ;; The "opt" in "regexp-opt" stands for "optim\\(al\\|i\\(se\\|ze\\)\\)". | |
29 ;; | |
30 ;; This package generates a regexp from a given list of strings (that matches | |
31 ;; one of those strings) that is equivalent to but more efficient than: | |
32 ;; | |
33 ;; (mapconcat 'identity (mapcar 'regexp-quote strings) "\\|") | |
34 ;; | |
35 ;; For example: | |
36 ;; | |
37 ;; (let ((strings '("cond" "if" "when" "unless" "while" | |
38 ;; "let" "let*" "progn" "prog1" "prog2" | |
39 ;; "save-restriction" "save-excursion" "save-window-excursion" | |
40 ;; "save-current-buffer" "save-match-data" | |
41 ;; "catch" "throw" "unwind-protect" "condition-case"))) | |
42 ;; (concat "(" (regexp-opt strings t) "\\>")) | |
43 ;; => "(\\(c\\(atch\\|ond\\(ition-case\\)?\\)\\|if\\|let\\*?\\|prog[12n]\\|save-\\(current-buffer\\|excursion\\|match-data\\|restriction\\|window-excursion\\)\\|throw\\|un\\(less\\|wind-protect\\)\\|wh\\(en\\|ile\\)\\)\\>" | |
44 ;; | |
45 ;; Searching using the above example `regexp-opt' regexp is significantly | |
46 ;; faster than searching using the equivalent `mapconcat' regexp, taking | |
47 ;; approximately two-thirds of the time. | |
48 ;; | |
49 ;; Since this package was written to produce efficient regexps, not regexps | |
50 ;; efficiently, it is probably not a good idea to in-line too many calls in | |
51 ;; your code, unless you use the following trick with `eval-when-compile': | |
52 ;; | |
53 ;; (defvar definition-regexp | |
54 ;; (eval-when-compile | |
55 ;; (concat "^(" | |
56 ;; (regexp-opt '("defun" "defsubst" "defmacro" "defalias" | |
57 ;; "defvar" "defconst") t) | |
58 ;; "\\>"))) | |
59 ;; | |
60 ;; The `byte-compile' code will be as if you had defined the variable thus: | |
61 ;; | |
62 ;; (defvar definition-regexp | |
63 ;; "^(\\(def\\(alias\\|const\\|macro\\|subst\\|un\\|var\\)\\)\\>") | |
64 ;; | |
65 ;; Originally written for font-lock.el, from an idea from Stig's hl319.el. | |
66 ;; Please don't tell me that it doesn't produce optimal regexps; I know that | |
67 ;; already. For example, the above explanation for the meaning of "opt" would | |
68 ;; be more efficient as "optim\\(al\\|i[sz]e\\)", but this requires complex | |
69 ;; forward looking. But (ideas or) code to improve things (are) is welcome. | |
70 | |
71 ;;; Code: | |
72 | |
73 ;;;###autoload | |
74 (defun regexp-opt (strings &optional paren) | |
75 "Return a regexp to match a string in STRINGS. | |
76 If optional PAREN non-nil, ensure that the returned regexp is enclosed by at | |
77 least one regexp grouping construct. | |
78 Each string in STRINGS should be unique and should not contain any regexps. | |
79 The returned regexp is typically more efficient than the equivalent regexp: | |
80 | |
81 (mapconcat 'identity (mapcar 'regexp-quote STRINGS) \"\\\\|\") | |
82 | |
83 but typically contains regexp grouping constructs. Use `regexp-opt-depth' to | |
84 count them." | |
85 (save-match-data | |
86 ;; Recurse on the sorted list. | |
87 (let ((max-lisp-eval-depth (* 1024 1024)) | |
88 (completion-ignore-case nil)) | |
89 (regexp-opt-group (sort (copy-sequence strings) 'string-lessp) paren)))) | |
90 | |
91 ;;;###autoload | |
92 (defun regexp-opt-depth (regexp) | |
93 "Return the depth of REGEXP. | |
94 This means the number of regexp grouping constructs (parenthesised expressions) | |
95 in REGEXP." | |
96 (save-match-data | |
97 ;; Hack to signal an error if REGEXP does not have balanced parentheses. | |
98 (string-match regexp "") | |
99 ;; Count the number of open parentheses in REGEXP. | |
100 (let ((count 0) start) | |
101 (while (string-match "\\\\(" regexp start) | |
102 (setq count (1+ count) start (match-end 0))) | |
103 count))) | |
104 | |
105 ;;; Workhorse functions. | |
106 | |
107 (eval-when-compile | |
108 (require 'cl)) | |
109 | |
110 (unless (fboundp 'make-bool-vector) | |
111 (defalias 'make-bool-vector 'make-vector)) | |
112 | |
113 (defun regexp-opt-group (strings &optional paren lax) | |
114 ;; | |
115 ;; Return a regexp to match a string in STRINGS. | |
116 ;; If PAREN non-nil, output regexp parentheses around returned regexp. | |
117 ;; If LAX non-nil, don't output parentheses if it doesn't require them. | |
118 ;; Merges keywords to avoid backtracking in Emacs' regexp matcher. | |
119 ;; | |
120 ;; The basic idea is to find the shortest common prefix, remove it and | |
121 ;; recurse. If there is no prefix, we divide the list into two so that (at | |
122 ;; least) one half will have at least a one-character common prefix. | |
123 ;; | |
124 ;; Also we delay the addition of grouping parenthesis as long as possible | |
125 ;; until we're sure we need them, and try to remove one-character sequences | |
126 ;; so we can use character sets rather than grouping parenthesis. | |
127 ;; | |
128 (let* ((open-group (if paren "\\(" "")) | |
129 (close-group (if paren "\\)" "")) | |
130 (open-charset (if lax "" open-group)) | |
131 (close-charset (if lax "" close-group))) | |
132 (cond | |
133 ;; | |
134 ;; If there is only one string, just return it. | |
135 ((= (length strings) 1) | |
136 (if (= (length (car strings)) 1) | |
137 (concat open-charset (regexp-quote (car strings)) close-charset) | |
138 (concat open-group (regexp-quote (car strings)) close-group))) | |
139 ;; | |
140 ;; If there is an empty string, remove it and recurse on the rest. | |
141 ((= (length (car strings)) 0) | |
142 (concat open-charset | |
143 (regexp-opt-group (cdr strings) t t) "?" | |
144 close-charset)) | |
145 ;; | |
146 ;; If all are one-character strings, just return a character set. | |
147 ((= (length strings) (apply '+ (mapcar 'length strings))) | |
148 (concat open-charset | |
149 (regexp-opt-charset strings) | |
150 close-charset)) | |
151 ;; | |
152 ;; We have a list of different length strings. | |
153 (t | |
154 (let ((prefix (try-completion "" (mapcar 'list strings))) | |
155 (letters (let ((completion-regexp-list '("^.$"))) | |
156 (all-completions "" (mapcar 'list strings))))) | |
157 (cond | |
158 ;; | |
159 ;; If there is a common prefix, remove it and recurse on the suffixes. | |
160 ((> (length prefix) 0) | |
161 (let* ((length (length prefix)) | |
162 (suffixes (mapcar (lambda (s) (substring s length)) strings))) | |
163 (concat open-group | |
164 (regexp-quote prefix) (regexp-opt-group suffixes t t) | |
165 close-group))) | |
166 ;; | |
167 ;; If there are several one-character strings, remove them and recurse | |
168 ;; on the rest. | |
169 ((> (length letters) 1) | |
170 (let ((rest (let ((completion-regexp-list '("^..+$"))) | |
171 (all-completions "" (mapcar 'list strings))))) | |
172 (concat open-group | |
173 (regexp-opt-charset letters) "\\|" (regexp-opt-group rest) | |
174 close-group))) | |
175 ;; | |
176 ;; Otherwise, divide the list into those that start with a particular | |
177 ;; letter and those that do not, and recurse on them. | |
178 (t | |
179 (let* ((char (substring (car strings) 0 1)) | |
180 (half1 (all-completions char (mapcar 'list strings))) | |
181 (half2 (nthcdr (length half1) strings))) | |
182 (concat open-group | |
183 (regexp-opt-group half1) "\\|" (regexp-opt-group half2) | |
184 close-group))))))))) | |
185 | |
186 (defun regexp-opt-charset (chars) | |
187 ;; | |
188 ;; Return a regexp to match a character in CHARS. | |
189 ;; | |
190 ;; The basic idea is to find character ranges. Also we take care in the | |
191 ;; position of character set meta characters in the character set regexp. | |
192 ;; | |
193 (let* ((charwidth 256) ; Yeah, right. | |
194 (charmap (make-bool-vector charwidth nil)) | |
195 (charset "") | |
196 (bracket "") (dash "") (caret "")) | |
197 ;; | |
198 ;; Make a character map but extract character set meta characters. | |
199 (let (char) | |
200 (while chars | |
201 (setq char (string-to-char (pop chars))) | |
202 (cond ((eq char ?\]) | |
203 (setq bracket "]")) | |
204 ((eq char ?^) | |
205 (setq caret "^")) | |
206 ((eq char ?-) | |
207 (setq dash "-")) | |
208 (t | |
209 (aset charmap char t))))) | |
210 ;; | |
211 ;; Make a character set from the map using ranges where applicable. | |
212 (let ((elt 0) start) | |
213 (while (< elt charwidth) | |
214 (when (aref charmap elt) | |
215 (setq start (1+ elt)) | |
216 (while (and (< start charwidth) (aref charmap start)) | |
217 (incf start)) | |
218 (if (< (- start elt) 4) | |
219 (setq charset (format "%s%c" charset elt)) | |
220 (setq charset (format "%s%c-%c" charset elt (1- start)) | |
221 elt start))) | |
222 (incf elt))) | |
223 ;; | |
224 ;; Make sure a caret is not first and a dash is first or last. | |
225 (if (and (string-equal charset "") (string-equal bracket "")) | |
226 (concat "[" dash caret "]") | |
227 (concat "[" bracket charset caret dash "]")))) | |
228 | |
229 (provide 'regexp-opt) | |
230 | |
231 ;;; regexp-opt.el ends here |