aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRichard M. Stallman1994-11-19 10:00:14 +0000
committerRichard M. Stallman1994-11-19 10:00:14 +0000
commitb819a39025f5224ec3b554776ec20fb1b50f3043 (patch)
treed272ad17a365caff3b50dd26b0437f884ef131ce /src
parent547d70f5209877c3ef0a4aebc2949e76f7796f9f (diff)
downloademacs-b819a39025f5224ec3b554776ec20fb1b50f3043.tar.gz
emacs-b819a39025f5224ec3b554776ec20fb1b50f3043.zip
(struct regexp_cache): New field `posix'.
(compile_pattern_1): New arg `posix'. Controls the syntax spec for parsing; also record it in the cache. (compile_pattern): New arg `posix'. (looking_at_1, string_match_1, fast_string_match): Pass 0 for new arg. (search_buffer): New arg `posix'. Now static. (search_command): New arg `posix'. All callers changed. (Fposix_search_forward, Fposix_search_backward): New functions. (string_match_1, looking_at_1): New subroutines broken out. (Fstring_match, Flooking_at): Use them. (Fposix_string_match, Fposix_looking_at): New functions. (syms_of_search): defsubr new functions.
Diffstat (limited to 'src')
-rw-r--r--src/search.c189
1 files changed, 154 insertions, 35 deletions
diff --git a/src/search.c b/src/search.c
index a0050190931..622df821c70 100644
--- a/src/search.c
+++ b/src/search.c
@@ -38,6 +38,8 @@ struct regexp_cache {
38 Lisp_Object regexp; 38 Lisp_Object regexp;
39 struct re_pattern_buffer buf; 39 struct re_pattern_buffer buf;
40 char fastmap[0400]; 40 char fastmap[0400];
41 /* Nonzero means regexp was compiled to do full POSIX backtracking. */
42 char posix;
41}; 43};
42 44
43/* The instances of that struct. */ 45/* The instances of that struct. */
@@ -77,6 +79,8 @@ Lisp_Object Qinvalid_regexp;
77 79
78static void set_search_regs (); 80static void set_search_regs ();
79 81
82static int search_buffer ();
83
80static void 84static void
81matcher_overflow () 85matcher_overflow ()
82{ 86{
@@ -89,22 +93,37 @@ matcher_overflow ()
89#define CONST 93#define CONST
90#endif 94#endif
91 95
92/* Compile a regexp and signal a Lisp error if anything goes wrong. */ 96/* Compile a regexp and signal a Lisp error if anything goes wrong.
97 PATTERN is the pattern to compile.
98 CP is the place to put the result.
99 TRANSLATE is a translation table for ignoring case, or NULL for none.
100 REGP is the structure that says where to store the "register"
101 values that will result from matching this pattern.
102 If it is 0, we should compile the pattern not to record any
103 subexpression bounds.
104 POSIX is nonzero if we want full backtracking (POSIX style)
105 for this pattern. 0 means backtrack only enough to get a valid match. */
93 106
94static void 107static void
95compile_pattern_1 (cp, pattern, translate, regp) 108compile_pattern_1 (cp, pattern, translate, regp, posix)
96 struct regexp_cache *cp; 109 struct regexp_cache *cp;
97 Lisp_Object pattern; 110 Lisp_Object pattern;
98 char *translate; 111 char *translate;
99 struct re_registers *regp; 112 struct re_registers *regp;
113 int posix;
100{ 114{
101 CONST char *val; 115 CONST char *val;
116 reg_syntax_t old;
102 117
103 cp->regexp = Qnil; 118 cp->regexp = Qnil;
104 cp->buf.translate = translate; 119 cp->buf.translate = translate;
120 cp->posix = posix;
105 BLOCK_INPUT; 121 BLOCK_INPUT;
122 old = re_set_syntax (RE_SYNTAX_EMACS
123 | (posix ? 0 : RE_NO_POSIX_BACKTRACKING));
106 val = (CONST char *) re_compile_pattern ((char *) XSTRING (pattern)->data, 124 val = (CONST char *) re_compile_pattern ((char *) XSTRING (pattern)->data,
107 XSTRING (pattern)->size, &cp->buf); 125 XSTRING (pattern)->size, &cp->buf);
126 re_set_syntax (old);
108 UNBLOCK_INPUT; 127 UNBLOCK_INPUT;
109 if (val) 128 if (val)
110 Fsignal (Qinvalid_regexp, Fcons (build_string (val), Qnil)); 129 Fsignal (Qinvalid_regexp, Fcons (build_string (val), Qnil));
@@ -120,13 +139,22 @@ compile_pattern_1 (cp, pattern, translate, regp)
120} 139}
121 140
122/* Compile a regexp if necessary, but first check to see if there's one in 141/* Compile a regexp if necessary, but first check to see if there's one in
123 the cache. */ 142 the cache.
143 PATTERN is the pattern to compile.
144 TRANSLATE is a translation table for ignoring case, or NULL for none.
145 REGP is the structure that says where to store the "register"
146 values that will result from matching this pattern.
147 If it is 0, we should compile the pattern not to record any
148 subexpression bounds.
149 POSIX is nonzero if we want full backtracking (POSIX style)
150 for this pattern. 0 means backtrack only enough to get a valid match. */
124 151
125struct re_pattern_buffer * 152struct re_pattern_buffer *
126compile_pattern (pattern, regp, translate) 153compile_pattern (pattern, regp, translate, posix)
127 Lisp_Object pattern; 154 Lisp_Object pattern;
128 struct re_registers *regp; 155 struct re_registers *regp;
129 char *translate; 156 char *translate;
157 int posix;
130{ 158{
131 struct regexp_cache *cp, **cpp; 159 struct regexp_cache *cp, **cpp;
132 160
@@ -134,13 +162,14 @@ compile_pattern (pattern, regp, translate)
134 { 162 {
135 cp = *cpp; 163 cp = *cpp;
136 if (!NILP (Fstring_equal (cp->regexp, pattern)) 164 if (!NILP (Fstring_equal (cp->regexp, pattern))
137 && cp->buf.translate == translate) 165 && cp->buf.translate == translate
166 && cp->posix == posix)
138 break; 167 break;
139 168
140 /* If we're at the end of the cache, compile into the last cell. */ 169 /* If we're at the end of the cache, compile into the last cell. */
141 if (cp->next == 0) 170 if (cp->next == 0)
142 { 171 {
143 compile_pattern_1 (cp, pattern, translate, regp); 172 compile_pattern_1 (cp, pattern, translate, regp, posix);
144 break; 173 break;
145 } 174 }
146 } 175 }
@@ -166,13 +195,10 @@ signal_failure (arg)
166 return Qnil; 195 return Qnil;
167} 196}
168 197
169DEFUN ("looking-at", Flooking_at, Slooking_at, 1, 1, 0, 198static Lisp_Object
170 "Return t if text after point matches regular expression PAT.\n\ 199looking_at_1 (string, posix)
171This function modifies the match data that `match-beginning',\n\
172`match-end' and `match-data' access; save and restore the match\n\
173data if you want to preserve them.")
174 (string)
175 Lisp_Object string; 200 Lisp_Object string;
201 int posix;
176{ 202{
177 Lisp_Object val; 203 Lisp_Object val;
178 unsigned char *p1, *p2; 204 unsigned char *p1, *p2;
@@ -183,7 +209,8 @@ data if you want to preserve them.")
183 CHECK_STRING (string, 0); 209 CHECK_STRING (string, 0);
184 bufp = compile_pattern (string, &search_regs, 210 bufp = compile_pattern (string, &search_regs,
185 (!NILP (current_buffer->case_fold_search) 211 (!NILP (current_buffer->case_fold_search)
186 ? DOWNCASE_TABLE : 0)); 212 ? DOWNCASE_TABLE : 0),
213 posix);
187 214
188 immediate_quit = 1; 215 immediate_quit = 1;
189 QUIT; /* Do a pending quit right away, to avoid paradoxical behavior */ 216 QUIT; /* Do a pending quit right away, to avoid paradoxical behavior */
@@ -225,14 +252,33 @@ data if you want to preserve them.")
225 return val; 252 return val;
226} 253}
227 254
228DEFUN ("string-match", Fstring_match, Sstring_match, 2, 3, 0, 255DEFUN ("looking-at", Flooking_at, Slooking_at, 1, 1, 0,
229 "Return index of start of first match for REGEXP in STRING, or nil.\n\ 256 "Return t if text after point matches regular expression PAT.\n\
230If third arg START is non-nil, start search at that index in STRING.\n\ 257This function modifies the match data that `match-beginning',\n\
231For index of first char beyond the match, do (match-end 0).\n\ 258`match-end' and `match-data' access; save and restore the match\n\
232`match-end' and `match-beginning' also give indices of substrings\n\ 259data if you want to preserve them.")
233matched by parenthesis constructs in the pattern.") 260 (string)
234 (regexp, string, start) 261 Lisp_Object string;
262{
263 return looking_at_1 (string, 0);
264}
265
266DEFUN ("posix-looking-at", Fposix_looking_at, Sposix_looking_at, 1, 1, 0,
267 "Return t if text after point matches regular expression PAT.\n\
268Find the longest match, in accord with Posix regular expression rules.\n\
269This function modifies the match data that `match-beginning',\n\
270`match-end' and `match-data' access; save and restore the match\n\
271data if you want to preserve them.")
272 (string)
273 Lisp_Object string;
274{
275 return looking_at_1 (string, 1);
276}
277
278static Lisp_Object
279string_match_1 (regexp, string, start, posix)
235 Lisp_Object regexp, string, start; 280 Lisp_Object regexp, string, start;
281 int posix;
236{ 282{
237 int val; 283 int val;
238 int s; 284 int s;
@@ -257,7 +303,8 @@ matched by parenthesis constructs in the pattern.")
257 303
258 bufp = compile_pattern (regexp, &search_regs, 304 bufp = compile_pattern (regexp, &search_regs,
259 (!NILP (current_buffer->case_fold_search) 305 (!NILP (current_buffer->case_fold_search)
260 ? DOWNCASE_TABLE : 0)); 306 ? DOWNCASE_TABLE : 0),
307 0);
261 immediate_quit = 1; 308 immediate_quit = 1;
262 val = re_search (bufp, (char *) XSTRING (string)->data, 309 val = re_search (bufp, (char *) XSTRING (string)->data,
263 XSTRING (string)->size, s, XSTRING (string)->size - s, 310 XSTRING (string)->size, s, XSTRING (string)->size - s,
@@ -270,6 +317,31 @@ matched by parenthesis constructs in the pattern.")
270 return make_number (val); 317 return make_number (val);
271} 318}
272 319
320DEFUN ("string-match", Fstring_match, Sstring_match, 2, 3, 0,
321 "Return index of start of first match for REGEXP in STRING, or nil.\n\
322If third arg START is non-nil, start search at that index in STRING.\n\
323For index of first char beyond the match, do (match-end 0).\n\
324`match-end' and `match-beginning' also give indices of substrings\n\
325matched by parenthesis constructs in the pattern.")
326 (regexp, string, start)
327 Lisp_Object regexp, string, start;
328{
329 return string_match_1 (regexp, string, start, 0);
330}
331
332DEFUN ("posix-string-match", Fposix_string_match, Sposix_string_match, 2, 3, 0,
333 "Return index of start of first match for REGEXP in STRING, or nil.\n\
334Find the longest match, in accord with Posix regular expression rules.\n\
335If third arg START is non-nil, start search at that index in STRING.\n\
336For index of first char beyond the match, do (match-end 0).\n\
337`match-end' and `match-beginning' also give indices of substrings\n\
338matched by parenthesis constructs in the pattern.")
339 (regexp, string, start)
340 Lisp_Object regexp, string, start;
341{
342 return string_match_1 (regexp, string, start, 1);
343}
344
273/* Match REGEXP against STRING, searching all of STRING, 345/* Match REGEXP against STRING, searching all of STRING,
274 and return the index of the match, or negative on failure. 346 and return the index of the match, or negative on failure.
275 This does not clobber the match data. */ 347 This does not clobber the match data. */
@@ -281,7 +353,7 @@ fast_string_match (regexp, string)
281 int val; 353 int val;
282 struct re_pattern_buffer *bufp; 354 struct re_pattern_buffer *bufp;
283 355
284 bufp = compile_pattern (regexp, 0, 0); 356 bufp = compile_pattern (regexp, 0, 0, 0);
285 immediate_quit = 1; 357 immediate_quit = 1;
286 val = re_search (bufp, (char *) XSTRING (string)->data, 358 val = re_search (bufp, (char *) XSTRING (string)->data,
287 XSTRING (string)->size, 0, XSTRING (string)->size, 359 XSTRING (string)->size, 0, XSTRING (string)->size,
@@ -725,10 +797,11 @@ skip_chars (forwardp, syntaxp, string, lim)
725/* Subroutines of Lisp buffer search functions. */ 797/* Subroutines of Lisp buffer search functions. */
726 798
727static Lisp_Object 799static Lisp_Object
728search_command (string, bound, noerror, count, direction, RE) 800search_command (string, bound, noerror, count, direction, RE, posix)
729 Lisp_Object string, bound, noerror, count; 801 Lisp_Object string, bound, noerror, count;
730 int direction; 802 int direction;
731 int RE; 803 int RE;
804 int posix;
732{ 805{
733 register int np; 806 register int np;
734 int lim; 807 int lim;
@@ -759,7 +832,8 @@ search_command (string, bound, noerror, count, direction, RE)
759 (!NILP (current_buffer->case_fold_search) 832 (!NILP (current_buffer->case_fold_search)
760 ? XSTRING (current_buffer->case_canon_table)->data : 0), 833 ? XSTRING (current_buffer->case_canon_table)->data : 0),
761 (!NILP (current_buffer->case_fold_search) 834 (!NILP (current_buffer->case_fold_search)
762 ? XSTRING (current_buffer->case_eqv_table)->data : 0)); 835 ? XSTRING (current_buffer->case_eqv_table)->data : 0),
836 posix);
763 if (np <= 0) 837 if (np <= 0)
764 { 838 {
765 if (NILP (noerror)) 839 if (NILP (noerror))
@@ -818,7 +892,7 @@ trivial_regexp_p (regexp)
818 892
819/* Search for the n'th occurrence of STRING in the current buffer, 893/* Search for the n'th occurrence of STRING in the current buffer,
820 starting at position POS and stopping at position LIM, 894 starting at position POS and stopping at position LIM,
821 treating PAT as a literal string if RE is false or as 895 treating STRING as a literal string if RE is false or as
822 a regular expression if RE is true. 896 a regular expression if RE is true.
823 897
824 If N is positive, searching is forward and LIM must be greater than POS. 898 If N is positive, searching is forward and LIM must be greater than POS.
@@ -826,9 +900,13 @@ trivial_regexp_p (regexp)
826 900
827 Returns -x if only N-x occurrences found (x > 0), 901 Returns -x if only N-x occurrences found (x > 0),
828 or else the position at the beginning of the Nth occurrence 902 or else the position at the beginning of the Nth occurrence
829 (if searching backward) or the end (if searching forward). */ 903 (if searching backward) or the end (if searching forward).
904
905 POSIX is nonzero if we want full backtracking (POSIX style)
906 for this pattern. 0 means backtrack only enough to get a valid match. */
830 907
831search_buffer (string, pos, lim, n, RE, trt, inverse_trt) 908static int
909search_buffer (string, pos, lim, n, RE, trt, inverse_trt, posix)
832 Lisp_Object string; 910 Lisp_Object string;
833 int pos; 911 int pos;
834 int lim; 912 int lim;
@@ -836,6 +914,7 @@ search_buffer (string, pos, lim, n, RE, trt, inverse_trt)
836 int RE; 914 int RE;
837 register unsigned char *trt; 915 register unsigned char *trt;
838 register unsigned char *inverse_trt; 916 register unsigned char *inverse_trt;
917 int posix;
839{ 918{
840 int len = XSTRING (string)->size; 919 int len = XSTRING (string)->size;
841 unsigned char *base_pat = XSTRING (string)->data; 920 unsigned char *base_pat = XSTRING (string)->data;
@@ -864,7 +943,7 @@ search_buffer (string, pos, lim, n, RE, trt, inverse_trt)
864 { 943 {
865 struct re_pattern_buffer *bufp; 944 struct re_pattern_buffer *bufp;
866 945
867 bufp = compile_pattern (string, &search_regs, (char *) trt); 946 bufp = compile_pattern (string, &search_regs, (char *) trt, posix);
868 947
869 immediate_quit = 1; /* Quit immediately if user types ^G, 948 immediate_quit = 1; /* Quit immediately if user types ^G,
870 because letting this function finish 949 because letting this function finish
@@ -1296,7 +1375,7 @@ See also the functions `match-beginning', `match-end' and `replace-match'.")
1296 (string, bound, noerror, count) 1375 (string, bound, noerror, count)
1297 Lisp_Object string, bound, noerror, count; 1376 Lisp_Object string, bound, noerror, count;
1298{ 1377{
1299 return search_command (string, bound, noerror, count, -1, 0); 1378 return search_command (string, bound, noerror, count, -1, 0, 0);
1300} 1379}
1301 1380
1302DEFUN ("search-forward", Fsearch_forward, Ssearch_forward, 1, 4, "sSearch: ", 1381DEFUN ("search-forward", Fsearch_forward, Ssearch_forward, 1, 4, "sSearch: ",
@@ -1312,7 +1391,7 @@ See also the functions `match-beginning', `match-end' and `replace-match'.")
1312 (string, bound, noerror, count) 1391 (string, bound, noerror, count)
1313 Lisp_Object string, bound, noerror, count; 1392 Lisp_Object string, bound, noerror, count;
1314{ 1393{
1315 return search_command (string, bound, noerror, count, 1, 0); 1394 return search_command (string, bound, noerror, count, 1, 0, 0);
1316} 1395}
1317 1396
1318DEFUN ("word-search-backward", Fword_search_backward, Sword_search_backward, 1, 4, 1397DEFUN ("word-search-backward", Fword_search_backward, Sword_search_backward, 1, 4,
@@ -1327,7 +1406,7 @@ Optional fourth argument is repeat count--search for successive occurrences.")
1327 (string, bound, noerror, count) 1406 (string, bound, noerror, count)
1328 Lisp_Object string, bound, noerror, count; 1407 Lisp_Object string, bound, noerror, count;
1329{ 1408{
1330 return search_command (wordify (string), bound, noerror, count, -1, 1); 1409 return search_command (wordify (string), bound, noerror, count, -1, 1, 0);
1331} 1410}
1332 1411
1333DEFUN ("word-search-forward", Fword_search_forward, Sword_search_forward, 1, 4, 1412DEFUN ("word-search-forward", Fword_search_forward, Sword_search_forward, 1, 4,
@@ -1342,7 +1421,7 @@ Optional fourth argument is repeat count--search for successive occurrences.")
1342 (string, bound, noerror, count) 1421 (string, bound, noerror, count)
1343 Lisp_Object string, bound, noerror, count; 1422 Lisp_Object string, bound, noerror, count;
1344{ 1423{
1345 return search_command (wordify (string), bound, noerror, count, 1, 1); 1424 return search_command (wordify (string), bound, noerror, count, 1, 1, 0);
1346} 1425}
1347 1426
1348DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4, 1427DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4,
@@ -1360,7 +1439,7 @@ See also the functions `match-beginning', `match-end' and `replace-match'.")
1360 (regexp, bound, noerror, count) 1439 (regexp, bound, noerror, count)
1361 Lisp_Object regexp, bound, noerror, count; 1440 Lisp_Object regexp, bound, noerror, count;
1362{ 1441{
1363 return search_command (regexp, bound, noerror, count, -1, 1); 1442 return search_command (regexp, bound, noerror, count, -1, 1, 0);
1364} 1443}
1365 1444
1366DEFUN ("re-search-forward", Fre_search_forward, Sre_search_forward, 1, 4, 1445DEFUN ("re-search-forward", Fre_search_forward, Sre_search_forward, 1, 4,
@@ -1376,7 +1455,43 @@ See also the functions `match-beginning', `match-end' and `replace-match'.")
1376 (regexp, bound, noerror, count) 1455 (regexp, bound, noerror, count)
1377 Lisp_Object regexp, bound, noerror, count; 1456 Lisp_Object regexp, bound, noerror, count;
1378{ 1457{
1379 return search_command (regexp, bound, noerror, count, 1, 1); 1458 return search_command (regexp, bound, noerror, count, 1, 1, 0);
1459}
1460
1461DEFUN ("posix-search-backward", Fposix_search_backward, Sposix_search_backward, 1, 4,
1462 "sPosix search backward: ",
1463 "Search backward from point for match for regular expression REGEXP.\n\
1464Find the longest match in accord with Posix regular expression rules.\n\
1465Set point to the beginning of the match, and return point.\n\
1466The match found is the one starting last in the buffer\n\
1467and yet ending before the origin of the search.\n\
1468An optional second argument bounds the search; it is a buffer position.\n\
1469The match found must start at or after that position.\n\
1470Optional third argument, if t, means if fail just return nil (no error).\n\
1471 If not nil and not t, move to limit of search and return nil.\n\
1472Optional fourth argument is repeat count--search for successive occurrences.\n\
1473See also the functions `match-beginning', `match-end' and `replace-match'.")
1474 (regexp, bound, noerror, count)
1475 Lisp_Object regexp, bound, noerror, count;
1476{
1477 return search_command (regexp, bound, noerror, count, -1, 1, 1);
1478}
1479
1480DEFUN ("posix-search-forward", Fposix_search_forward, Sposix_search_forward, 1, 4,
1481 "sPosix search: ",
1482 "Search forward from point for regular expression REGEXP.\n\
1483Find the longest match in accord with Posix regular expression rules.\n\
1484Set point to the end of the occurrence found, and return point.\n\
1485An optional second argument bounds the search; it is a buffer position.\n\
1486The match found must not extend after that position.\n\
1487Optional third argument, if t, means if fail just return nil (no error).\n\
1488 If not nil and not t, move to limit of search and return nil.\n\
1489Optional fourth argument is repeat count--search for successive occurrences.\n\
1490See also the functions `match-beginning', `match-end' and `replace-match'.")
1491 (regexp, bound, noerror, count)
1492 Lisp_Object regexp, bound, noerror, count;
1493{
1494 return search_command (regexp, bound, noerror, count, 1, 1, 1);
1380} 1495}
1381 1496
1382DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 4, 0, 1497DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 4, 0,
@@ -1865,8 +1980,10 @@ syms_of_search ()
1865 last_thing_searched = Qnil; 1980 last_thing_searched = Qnil;
1866 staticpro (&last_thing_searched); 1981 staticpro (&last_thing_searched);
1867 1982
1868 defsubr (&Sstring_match);
1869 defsubr (&Slooking_at); 1983 defsubr (&Slooking_at);
1984 defsubr (&Sposix_looking_at);
1985 defsubr (&Sstring_match);
1986 defsubr (&Sposix_string_match);
1870 defsubr (&Sskip_chars_forward); 1987 defsubr (&Sskip_chars_forward);
1871 defsubr (&Sskip_chars_backward); 1988 defsubr (&Sskip_chars_backward);
1872 defsubr (&Sskip_syntax_forward); 1989 defsubr (&Sskip_syntax_forward);
@@ -1877,6 +1994,8 @@ syms_of_search ()
1877 defsubr (&Sword_search_backward); 1994 defsubr (&Sword_search_backward);
1878 defsubr (&Sre_search_forward); 1995 defsubr (&Sre_search_forward);
1879 defsubr (&Sre_search_backward); 1996 defsubr (&Sre_search_backward);
1997 defsubr (&Sposix_search_forward);
1998 defsubr (&Sposix_search_backward);
1880 defsubr (&Sreplace_match); 1999 defsubr (&Sreplace_match);
1881 defsubr (&Smatch_beginning); 2000 defsubr (&Smatch_beginning);
1882 defsubr (&Smatch_end); 2001 defsubr (&Smatch_end);