189
|
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
|
197
|
8 ;; Modified by Karl M. Hegbloom Sep. 1997 to support the new regexp syntax
|
|
9 ;; with shy groups. (benchmarks pending)
|
|
10
|
189
|
11 ;; This file is part of XEmacs.
|
|
12
|
|
13 ;; XEmacs is free software; you can redistribute it and/or modify
|
|
14 ;; it under the terms of the GNU General Public License as published by
|
|
15 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;; any later version.
|
|
17
|
|
18 ;; XEmacs is distributed in the hope that it will be useful,
|
|
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;; GNU General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;; Boston, MA 02111-1307, USA.
|
|
27
|
|
28 ;;; Commentary:
|
|
29
|
|
30 ;; The "opt" in "regexp-opt" stands for "optim\\(al\\|i\\(se\\|ze\\)\\)".
|
|
31 ;;
|
|
32 ;; This package generates a regexp from a given list of strings (which matches
|
|
33 ;; one of those strings) so that the regexp generated by:
|
|
34 ;;
|
|
35 ;; (regexp-opt strings)
|
|
36 ;;
|
|
37 ;; is equivalent to, but more efficient than, the regexp generated by:
|
|
38 ;;
|
|
39 ;; (mapconcat 'regexp-quote strings "\\|")
|
|
40 ;;
|
|
41 ;; For example:
|
|
42 ;;
|
|
43 ;; (let ((strings '("cond" "if" "when" "unless" "while"
|
|
44 ;; "let" "let*" "progn" "prog1" "prog2"
|
|
45 ;; "save-restriction" "save-excursion" "save-window-excursion"
|
|
46 ;; "save-current-buffer" "save-match-data"
|
|
47 ;; "catch" "throw" "unwind-protect" "condition-case")))
|
|
48 ;; (concat "(" (regexp-opt strings t) "\\>"))
|
197
|
49 ;;
|
|
50 ;; => "(\\(?: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\\)\\)\\>"
|
|
51 ;;
|
|
52 ;;
|
|
53 ;; (let ((strings '("cond" "if" "when" "unless" "while"
|
|
54 ;; "let" "let*" "progn" "prog1" "prog2"
|
|
55 ;; "save-restriction" "save-excursion" "save-window-excursion"
|
|
56 ;; "save-current-buffer" "save-match-data"
|
|
57 ;; "catch" "throw" "unwind-protect" "condition-case")))
|
|
58 ;; (concat "(" (regexp-opt strings t t) "\\>"))
|
|
59 ;; ^
|
189
|
60 ;; => "(\\(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\\)\\)\\>"
|
|
61 ;;
|
197
|
62 ;;
|
189
|
63 ;; Searching using the above example `regexp-opt' regexp takes approximately
|
|
64 ;; two-thirds of the time taken using the equivalent `mapconcat' regexp.
|
|
65
|
|
66 ;; Since this package was written to produce efficient regexps, not regexps
|
|
67 ;; efficiently, it is probably not a good idea to in-line too many calls in
|
|
68 ;; your code, unless you use the following trick with `eval-when-compile':
|
|
69 ;;
|
|
70 ;; (defvar definition-regexp
|
|
71 ;; (eval-when-compile
|
|
72 ;; (concat "^("
|
|
73 ;; (regexp-opt '("defun" "defsubst" "defmacro" "defalias"
|
|
74 ;; "defvar" "defconst") t)
|
|
75 ;; "\\>")))
|
|
76 ;;
|
|
77 ;; The `byte-compile' code will be as if you had defined the variable thus:
|
|
78 ;;
|
|
79 ;; (defvar definition-regexp
|
|
80 ;; "^(\\(def\\(alias\\|const\\|macro\\|subst\\|un\\|var\\)\\)\\>")
|
|
81 ;;
|
|
82 ;; Note that if you use this trick for all instances of `regexp-opt' and
|
|
83 ;; `regexp-opt-depth' in your code, regexp-opt.el would only have to be loaded
|
|
84 ;; at compile time. But note also that using this trick means that should
|
|
85 ;; regexp-opt.el be changed, perhaps to fix a bug or to add a feature to
|
|
86 ;; improve the efficiency of `regexp-opt' regexps, you would have to recompile
|
|
87 ;; your code for such changes to have effect in your code.
|
|
88
|
|
89 ;; Originally written for font-lock.el, from an idea from Stig's hl319.el, with
|
|
90 ;; thanks for ideas also to Michael Ernst, Bob Glickstein and Dan Nicolaescu.
|
|
91 ;; Please don't tell me that it doesn't produce optimal regexps; I know that
|
|
92 ;; already. For example, the above explanation for the meaning of "opt" would
|
|
93 ;; be more efficient as "optim\\(al\\|i[sz]e\\)", but this requires complex
|
|
94 ;; forward looking. But (ideas or) code to improve things (are) is welcome.
|
|
95
|
|
96 ;;; Code:
|
|
97
|
|
98 ;;;###autoload
|
197
|
99 (defun regexp-opt (strings &optional paren non-shy)
|
189
|
100 "Return a regexp to match a string in STRINGS.
|
|
101 Each string should be unique in STRINGS and should not contain any regexps,
|
197
|
102 quoted or not. If optional PAREN is non-nil, ensure that the returned
|
|
103 regexp is enclosed by at least one regexp match grouping construct. If
|
|
104 optional NON-SHY is non nil, the inner groupings will use \"\\\\( \\\\)\" grouping,
|
|
105 rather than the default \"\\\\(?: \\\\)\" 'shy', or non-match-capturing groups.
|
189
|
106 The returned regexp is typically more efficient than the equivalent regexp:
|
|
107
|
|
108 (let ((open-paren (if PAREN \"\\\\(\" \"\")) (close-paren (if PAREN \"\\\\)\" \"\")))
|
|
109 (concat open-paren (mapconcat 'regexp-quote STRINGS \"\\\\|\") close-paren))
|
|
110
|
|
111 but typically contains more regexp grouping constructs.
|
|
112 Use `regexp-opt-depth' to count them."
|
|
113 (save-match-data
|
|
114 ;; Recurse on the sorted list.
|
|
115 (let ((max-lisp-eval-depth (* 1024 1024))
|
|
116 (completion-ignore-case nil))
|
197
|
117 (regexp-opt-group (sort (copy-sequence strings) 'string-lessp) paren nil non-shy))))
|
189
|
118
|
|
119 ;;;###autoload
|
197
|
120 (defun regexp-opt-depth (regexp &optional count-shy-groups-too)
|
189
|
121 "Return the depth of REGEXP.
|
|
122 This means the number of regexp grouping constructs (parenthesised expressions)
|
197
|
123 in REGEXP, not counting the \"\\\\(?: \\\\)\" non-match-capturing groups unless
|
|
124 COUNT-SHY-GROUPS-TOO is non-nil.
|
|
125 See `regexp-opt'."
|
189
|
126 (save-match-data
|
|
127 ;; Hack to signal an error if REGEXP does not have balanced parentheses.
|
|
128 (string-match regexp "")
|
|
129 ;; Count the number of open parentheses in REGEXP.
|
197
|
130 (let ((max (1- (length regexp)))
|
|
131 (count 0) start)
|
189
|
132 (while (string-match "\\\\(" regexp start)
|
197
|
133 (setq start (match-end 0))
|
|
134 (when (or count-shy-groups-too
|
|
135 (not (string= (substring regexp start (min (+ start 2) max)) "?:")))
|
|
136 (setq count (1+ count))))
|
189
|
137 count)))
|
|
138
|
|
139 ;;; Workhorse functions.
|
|
140
|
|
141 (eval-when-compile
|
|
142 (require 'cl))
|
|
143
|
|
144 (unless (fboundp 'make-bool-vector)
|
|
145 (defalias 'make-bool-vector 'make-vector))
|
|
146
|
197
|
147 (defun regexp-opt-group (strings &optional paren lax non-shy)
|
189
|
148 ;;
|
|
149 ;; Return a regexp to match a string in STRINGS.
|
|
150 ;; If PAREN non-nil, output regexp parentheses around returned regexp.
|
|
151 ;; If LAX non-nil, don't output parentheses if it doesn't require them.
|
197
|
152 ;; If NON-SHY non-nil, don't use \\(?: \\) shy groups, use match capturing ones.
|
189
|
153 ;; Merges keywords to avoid backtracking in Emacs' regexp matcher.
|
|
154 ;;
|
|
155 ;; The basic idea is to find the shortest common prefix, remove it and
|
|
156 ;; recurse. If there is no prefix, we divide the list into two so that (at
|
|
157 ;; least) one half will have at least a one-character common prefix.
|
|
158 ;;
|
|
159 ;; Also we delay the addition of grouping parenthesis as long as possible
|
|
160 ;; until we're sure we need them, and try to remove one-character sequences
|
|
161 ;; so we can use character sets rather than grouping parenthesis.
|
|
162 ;;
|
197
|
163 (let* ((open-group (cond
|
|
164 ((and paren non-shy) "\\(")
|
|
165 (paren "\\(?:")
|
|
166 (t "")))
|
189
|
167 (close-group (if paren "\\)" ""))
|
|
168 (open-charset (if lax "" open-group))
|
|
169 (close-charset (if lax "" close-group)))
|
|
170 (cond
|
|
171 ;;
|
|
172 ;; If there is only one string, just return it.
|
|
173 ((= (length strings) 1)
|
|
174 (if (= (length (car strings)) 1)
|
|
175 (concat open-charset (regexp-quote (car strings)) close-charset)
|
|
176 (concat open-group (regexp-quote (car strings)) close-group)))
|
|
177 ;;
|
|
178 ;; If there is an empty string, remove it and recurse on the rest.
|
|
179 ((= (length (car strings)) 0)
|
|
180 (concat open-charset
|
197
|
181 (regexp-opt-group (cdr strings) t t non-shy) "?"
|
189
|
182 close-charset))
|
|
183 ;;
|
|
184 ;; If all are one-character strings, just return a character set.
|
|
185 ((= (length strings) (apply '+ (mapcar 'length strings)))
|
|
186 (concat open-charset
|
|
187 (regexp-opt-charset strings)
|
|
188 close-charset))
|
|
189 ;;
|
|
190 ;; We have a list of different length strings.
|
|
191 (t
|
|
192 (let ((prefix (try-completion "" (mapcar 'list strings)))
|
|
193 (letters (let ((completion-regexp-list '("^.$")))
|
|
194 (all-completions "" (mapcar 'list strings)))))
|
|
195 (cond
|
|
196 ;;
|
|
197 ;; If there is a common prefix, remove it and recurse on the suffixes.
|
|
198 ((> (length prefix) 0)
|
|
199 (let* ((length (length prefix))
|
|
200 (suffixes (mapcar (lambda (s) (substring s length)) strings)))
|
|
201 (concat open-group
|
197
|
202 (regexp-quote prefix) (regexp-opt-group suffixes t t non-shy)
|
189
|
203 close-group)))
|
|
204 ;;
|
|
205 ;; If there are several one-character strings, remove them and recurse
|
|
206 ;; on the rest (first so the final regexp finds the longest match).
|
|
207 ((> (length letters) 1)
|
|
208 (let ((rest (let ((completion-regexp-list '("^..+$")))
|
|
209 (all-completions "" (mapcar 'list strings)))))
|
|
210 (concat open-group
|
197
|
211 (regexp-opt-group rest nil nil non-shy) "\\|" (regexp-opt-charset letters)
|
189
|
212 close-group)))
|
|
213 ;;
|
|
214 ;; Otherwise, divide the list into those that start with a particular
|
|
215 ;; letter and those that do not, and recurse on them.
|
|
216 (t
|
|
217 (let* ((char (substring (car strings) 0 1))
|
|
218 (half1 (all-completions char (mapcar 'list strings)))
|
|
219 (half2 (nthcdr (length half1) strings)))
|
|
220 (concat open-group
|
197
|
221 (regexp-opt-group half1 nil nil non-shy) "\\|" (regexp-opt-group half2 nil nil non-shy)
|
189
|
222 close-group)))))))))
|
|
223
|
|
224 (defun regexp-opt-charset (chars)
|
|
225 ;;
|
|
226 ;; Return a regexp to match a character in CHARS.
|
|
227 ;;
|
|
228 ;; The basic idea is to find character ranges. Also we take care in the
|
|
229 ;; position of character set meta characters in the character set regexp.
|
|
230 ;;
|
|
231 (let* ((charwidth 256) ; Yeah, right.
|
|
232 ;; XEmacs: use bit-vectors instead of bool-vectors
|
|
233 (charmap (make-bit-vector charwidth 0))
|
|
234 (charset "")
|
|
235 (bracket "") (dash "") (caret ""))
|
|
236 ;;
|
|
237 ;; Make a character map but extract character set meta characters.
|
|
238 (dolist (char (mapcar 'string-to-char chars))
|
|
239 (case char
|
|
240 (?\]
|
|
241 (setq bracket "]"))
|
|
242 (?^
|
|
243 (setq caret "^"))
|
|
244 (?-
|
|
245 (setq dash "-"))
|
|
246 (otherwise
|
|
247 ;; XEmacs: 1
|
|
248 (aset charmap char 1))))
|
|
249 ;;
|
|
250 ;; Make a character set from the map using ranges where applicable.
|
|
251 (dotimes (char charwidth)
|
|
252 (let ((start char))
|
|
253 (while (and (< char charwidth)
|
|
254 ;; XEmacs: (not (zerop ...))
|
|
255 (not (zerop (aref charmap char))))
|
|
256 (incf char))
|
|
257 (cond ((> char (+ start 3))
|
|
258 (setq charset (format "%s%c-%c" charset start (1- char))))
|
|
259 ((> char start)
|
|
260 (setq charset (format "%s%c" charset (setq char start)))))))
|
|
261 ;;
|
|
262 ;; Make sure a caret is not first and a dash is first or last.
|
|
263 (if (and (string-equal charset "") (string-equal bracket ""))
|
|
264 (concat "[" dash caret "]")
|
|
265 (concat "[" bracket charset caret dash "]"))))
|
|
266
|
|
267 (provide 'regexp-opt)
|
|
268
|
|
269 ;;; regexp-opt.el ends here
|