Mercurial > hg > xemacs-beta
annotate src/search.c @ 4962:e813cf16c015
merge
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Mon, 01 Feb 2010 05:29:05 -0600 |
parents | 304aebb79cd3 e91e3e353805 |
children | 2ade80e8c640 |
rev | line source |
---|---|
428 | 1 /* String search routines for XEmacs. |
2 Copyright (C) 1985, 1986, 1987, 1992-1995 Free Software Foundation, Inc. | |
3 Copyright (C) 1995 Sun Microsystems, Inc. | |
793 | 4 Copyright (C) 2001, 2002 Ben Wing. |
428 | 5 |
6 This file is part of XEmacs. | |
7 | |
8 XEmacs is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 2, or (at your option) any | |
11 later version. | |
12 | |
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with XEmacs; see the file COPYING. If not, write to | |
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 Boston, MA 02111-1307, USA. */ | |
22 | |
23 /* Synched up with: FSF 19.29, except for region-cache stuff. */ | |
24 | |
25 /* Hacked on for Mule by Ben Wing, December 1994 and August 1995. */ | |
26 | |
826 | 27 /* This file has been Mule-ized. */ |
428 | 28 |
29 #include <config.h> | |
30 #include "lisp.h" | |
31 | |
32 #include "buffer.h" | |
33 #include "insdel.h" | |
34 #include "opaque.h" | |
35 #ifdef REGION_CACHE_NEEDS_WORK | |
36 #include "region-cache.h" | |
37 #endif | |
38 #include "syntax.h" | |
39 | |
40 #include <sys/types.h> | |
41 #include "regex.h" | |
446 | 42 #include "casetab.h" |
43 #include "chartab.h" | |
44 | |
45 #define TRANSLATE(table, pos) \ | |
867 | 46 (!NILP (table) ? TRT_TABLE_OF (table, (Ichar) pos) : pos) |
428 | 47 |
48 #define REGEXP_CACHE_SIZE 20 | |
49 | |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
50 #ifdef DEBUG_XEMACS |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
51 |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
52 /* Used in tests/automated/case-tests.el if available. */ |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
53 Fixnum debug_xemacs_searches; |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
54 |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
55 Lisp_Object Qsearch_algorithm_used, Qboyer_moore, Qsimple_search; |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
56 |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
57 #endif |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
58 |
428 | 59 /* If the regexp is non-nil, then the buffer contains the compiled form |
60 of that regexp, suitable for searching. */ | |
446 | 61 struct regexp_cache |
62 { | |
428 | 63 struct regexp_cache *next; |
64 Lisp_Object regexp; | |
65 struct re_pattern_buffer buf; | |
66 char fastmap[0400]; | |
67 /* Nonzero means regexp was compiled to do full POSIX backtracking. */ | |
68 char posix; | |
69 }; | |
70 | |
71 /* The instances of that struct. */ | |
72 static struct regexp_cache searchbufs[REGEXP_CACHE_SIZE]; | |
73 | |
74 /* The head of the linked list; points to the most recently used buffer. */ | |
75 static struct regexp_cache *searchbuf_head; | |
76 | |
77 | |
78 /* Every call to re_match, etc., must pass &search_regs as the regs | |
79 argument unless you can show it is unnecessary (i.e., if re_match | |
80 is certainly going to be called again before region-around-match | |
81 can be called). | |
82 | |
83 Since the registers are now dynamically allocated, we need to make | |
84 sure not to refer to the Nth register before checking that it has | |
85 been allocated by checking search_regs.num_regs. | |
86 | |
87 The regex code keeps track of whether it has allocated the search | |
88 buffer using bits in the re_pattern_buffer. This means that whenever | |
89 you compile a new pattern, it completely forgets whether it has | |
90 allocated any registers, and will allocate new registers the next | |
91 time you call a searching or matching function. Therefore, we need | |
92 to call re_set_registers after compiling a new pattern or after | |
93 setting the match registers, so that the regex functions will be | |
94 able to free or re-allocate it properly. */ | |
95 | |
96 /* Note: things get trickier under Mule because the values returned from | |
826 | 97 the regexp routines are in Bytebpos's but we need them to be in Charbpos's. |
428 | 98 We take the easy way out for the moment and just convert them immediately. |
99 We could be more clever by not converting them until necessary, but | |
100 that gets real ugly real fast since the buffer might have changed and | |
101 the positions might be out of sync or out of range. | |
102 */ | |
103 static struct re_registers search_regs; | |
104 | |
1468 | 105 /* Every function that sets the match data _must_ clear unused search |
106 registers on success. An unsuccessful search or match _must_ preserve | |
107 the search registers. The traditional documentation implied that | |
108 any match operation might trash the registers, but in fact failures | |
109 have always preserved the match data (in GNU Emacs as well). Some | |
110 plausible code depends on this behavior (cf. `w3-configuration-data' | |
111 in library "w3-cfg"). | |
112 | |
113 Ordinary string searchs use set_search_regs to set the whole-string | |
114 match. That function takes care of clearing the unused subexpression | |
1425 | 115 registers. |
116 */ | |
117 static void set_search_regs (struct buffer *buf, Charbpos beg, Charcount len); | |
1468 | 118 static void clear_search_regs (void); |
1425 | 119 |
428 | 120 /* The buffer in which the last search was performed, or |
121 Qt if the last search was done in a string; | |
122 Qnil if no searching has been done yet. */ | |
123 static Lisp_Object last_thing_searched; | |
124 | |
125 /* error condition signalled when regexp compile_pattern fails */ | |
126 | |
127 Lisp_Object Qinvalid_regexp; | |
128 | |
129 /* Regular expressions used in forward/backward-word */ | |
130 Lisp_Object Vforward_word_regexp, Vbackward_word_regexp; | |
131 | |
507 | 132 Fixnum warn_about_possibly_incompatible_back_references; |
502 | 133 |
428 | 134 /* range table for use with skip_chars. Only needed for Mule. */ |
135 Lisp_Object Vskip_chars_range_table; | |
136 | |
867 | 137 static Charbpos simple_search (struct buffer *buf, Ibyte *base_pat, |
826 | 138 Bytecount len, Bytebpos pos, Bytebpos lim, |
139 EMACS_INT n, Lisp_Object trt); | |
867 | 140 static Charbpos boyer_moore (struct buffer *buf, Ibyte *base_pat, |
826 | 141 Bytecount len, Bytebpos pos, Bytebpos lim, |
142 EMACS_INT n, Lisp_Object trt, | |
143 Lisp_Object inverse_trt, int charset_base); | |
665 | 144 static Charbpos search_buffer (struct buffer *buf, Lisp_Object str, |
826 | 145 Charbpos charbpos, Charbpos buflim, EMACS_INT n, |
146 int RE, Lisp_Object trt, | |
147 Lisp_Object inverse_trt, int posix); | |
771 | 148 |
2268 | 149 static DECLARE_DOESNT_RETURN (matcher_overflow (void)); |
150 | |
151 static DOESNT_RETURN | |
152 matcher_overflow () | |
428 | 153 { |
563 | 154 stack_overflow ("Stack overflow in regexp matcher", Qunbound); |
428 | 155 } |
156 | |
157 /* Compile a regexp and signal a Lisp error if anything goes wrong. | |
158 PATTERN is the pattern to compile. | |
159 CP is the place to put the result. | |
826 | 160 TRANSLATE is a translation table for ignoring case, or Qnil for none. |
428 | 161 REGP is the structure that says where to store the "register" |
162 values that will result from matching this pattern. | |
163 If it is 0, we should compile the pattern not to record any | |
164 subexpression bounds. | |
165 POSIX is nonzero if we want full backtracking (POSIX style) | |
166 for this pattern. 0 means backtrack only enough to get a valid match. */ | |
167 | |
168 static int | |
169 compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern, | |
2286 | 170 struct re_registers *UNUSED (regp), Lisp_Object translate, |
826 | 171 int posix, Error_Behavior errb) |
428 | 172 { |
442 | 173 const char *val; |
428 | 174 reg_syntax_t old; |
175 | |
176 cp->regexp = Qnil; | |
177 cp->buf.translate = translate; | |
178 cp->posix = posix; | |
179 old = re_set_syntax (RE_SYNTAX_EMACS | |
180 | (posix ? 0 : RE_NO_POSIX_BACKTRACKING)); | |
442 | 181 val = (const char *) |
428 | 182 re_compile_pattern ((char *) XSTRING_DATA (pattern), |
183 XSTRING_LENGTH (pattern), &cp->buf); | |
184 re_set_syntax (old); | |
185 if (val) | |
186 { | |
4953
304aebb79cd3
function renamings to track names of char typedefs
Ben Wing <ben@xemacs.org>
parents:
4952
diff
changeset
|
187 maybe_signal_error (Qinvalid_regexp, 0, build_cistring (val), |
428 | 188 Qsearch, errb); |
189 return 0; | |
190 } | |
191 | |
192 cp->regexp = Fcopy_sequence (pattern); | |
193 return 1; | |
194 } | |
195 | |
196 /* Compile a regexp if necessary, but first check to see if there's one in | |
197 the cache. | |
198 PATTERN is the pattern to compile. | |
826 | 199 TRANSLATE is a translation table for ignoring case, or Qnil for none. |
428 | 200 REGP is the structure that says where to store the "register" |
201 values that will result from matching this pattern. | |
202 If it is 0, we should compile the pattern not to record any | |
203 subexpression bounds. | |
204 POSIX is nonzero if we want full backtracking (POSIX style) | |
205 for this pattern. 0 means backtrack only enough to get a valid match. */ | |
206 | |
207 struct re_pattern_buffer * | |
208 compile_pattern (Lisp_Object pattern, struct re_registers *regp, | |
2286 | 209 Lisp_Object translate, Lisp_Object UNUSED (searchobj), |
210 struct buffer *UNUSED (searchbuf), int posix, | |
211 Error_Behavior errb) | |
428 | 212 { |
213 struct regexp_cache *cp, **cpp; | |
214 | |
215 for (cpp = &searchbuf_head; ; cpp = &cp->next) | |
216 { | |
217 cp = *cpp; | |
826 | 218 /* &&#### once we fix up the fastmap code in regex.c for 8-bit-fixed, |
219 we need to record and compare the buffer and format, since the | |
220 fastmap will reflect the state of the buffer -- and things get | |
221 more complicated if the buffer has changed formats or (esp.) has | |
222 kept the format but changed its interpretation! may need to have | |
223 the code that changes the interpretation go through and invalidate | |
224 cache entries for that buffer. */ | |
428 | 225 if (!NILP (Fstring_equal (cp->regexp, pattern)) |
446 | 226 && EQ (cp->buf.translate, translate) |
428 | 227 && cp->posix == posix) |
228 break; | |
229 | |
230 /* If we're at the end of the cache, compile into the last cell. */ | |
231 if (cp->next == 0) | |
232 { | |
826 | 233 if (!compile_pattern_1 (cp, pattern, regp, translate, |
234 posix, errb)) | |
428 | 235 return 0; |
236 break; | |
237 } | |
238 } | |
239 | |
240 /* When we get here, cp (aka *cpp) contains the compiled pattern, | |
241 either because we found it in the cache or because we just compiled it. | |
242 Move it to the front of the queue to mark it as most recently used. */ | |
243 *cpp = cp->next; | |
244 cp->next = searchbuf_head; | |
245 searchbuf_head = cp; | |
246 | |
247 /* Advise the searching functions about the space we have allocated | |
248 for register data. */ | |
249 if (regp) | |
250 re_set_registers (&cp->buf, regp, regp->num_regs, regp->start, regp->end); | |
251 | |
252 return &cp->buf; | |
253 } | |
254 | |
255 /* Error condition used for failing searches */ | |
256 Lisp_Object Qsearch_failed; | |
257 | |
2268 | 258 static DECLARE_DOESNT_RETURN (signal_failure (Lisp_Object)); |
259 | |
260 static DOESNT_RETURN | |
428 | 261 signal_failure (Lisp_Object arg) |
262 { | |
446 | 263 for (;;) |
264 Fsignal (Qsearch_failed, list1 (arg)); | |
428 | 265 } |
266 | |
826 | 267 /* Convert the search registers from Bytebpos's to Charbpos's. Needs to be |
428 | 268 done after each regexp match that uses the search regs. |
269 | |
270 We could get a potential speedup by not converting the search registers | |
271 until it's really necessary, e.g. when match-data or replace-match is | |
272 called. However, this complexifies the code a lot (e.g. the buffer | |
826 | 273 could have changed and the Bytebpos's stored might be invalid) and is |
428 | 274 probably not a great time-saver. */ |
275 | |
276 static void | |
277 fixup_search_regs_for_buffer (struct buffer *buf) | |
278 { | |
279 int i; | |
280 int num_regs = search_regs.num_regs; | |
281 | |
282 for (i = 0; i < num_regs; i++) | |
283 { | |
284 if (search_regs.start[i] >= 0) | |
826 | 285 search_regs.start[i] = bytebpos_to_charbpos (buf, |
286 search_regs.start[i]); | |
428 | 287 if (search_regs.end[i] >= 0) |
665 | 288 search_regs.end[i] = bytebpos_to_charbpos (buf, search_regs.end[i]); |
428 | 289 } |
290 } | |
291 | |
292 /* Similar but for strings. */ | |
293 static void | |
294 fixup_search_regs_for_string (Lisp_Object string) | |
295 { | |
296 int i; | |
297 int num_regs = search_regs.num_regs; | |
298 | |
299 /* #### bytecount_to_charcount() is not that efficient. This function | |
867 | 300 could be faster if it did its own conversion (using INC_IBYTEPTR() |
428 | 301 and such), because the register ends are likely to be somewhat ordered. |
302 (Even if not, you could sort them.) | |
303 | |
304 Think about this if this function is a time hog, which it's probably | |
305 not. */ | |
306 for (i = 0; i < num_regs; i++) | |
307 { | |
308 if (search_regs.start[i] > 0) | |
309 { | |
310 search_regs.start[i] = | |
793 | 311 string_index_byte_to_char (string, search_regs.start[i]); |
428 | 312 } |
313 if (search_regs.end[i] > 0) | |
314 { | |
315 search_regs.end[i] = | |
793 | 316 string_index_byte_to_char (string, search_regs.end[i]); |
428 | 317 } |
318 } | |
319 } | |
320 | |
321 | |
322 static Lisp_Object | |
323 looking_at_1 (Lisp_Object string, struct buffer *buf, int posix) | |
324 { | |
325 Lisp_Object val; | |
665 | 326 Bytebpos p1, p2; |
428 | 327 Bytecount s1, s2; |
328 REGISTER int i; | |
329 struct re_pattern_buffer *bufp; | |
826 | 330 struct syntax_cache scache_struct; |
331 struct syntax_cache *scache = &scache_struct; | |
332 | |
428 | 333 CHECK_STRING (string); |
334 bufp = compile_pattern (string, &search_regs, | |
335 (!NILP (buf->case_fold_search) | |
446 | 336 ? XCASE_TABLE_DOWNCASE (buf->case_table) : Qnil), |
826 | 337 wrap_buffer (buf), buf, posix, ERROR_ME); |
428 | 338 |
339 QUIT; | |
340 | |
341 /* Get pointers and sizes of the two strings | |
342 that make up the visible portion of the buffer. */ | |
343 | |
826 | 344 p1 = BYTE_BUF_BEGV (buf); |
345 p2 = BYTE_BUF_CEILING_OF (buf, p1); | |
428 | 346 s1 = p2 - p1; |
826 | 347 s2 = BYTE_BUF_ZV (buf) - p2; |
348 | |
349 /* By making the regex object, regex buffer, and syntax cache arguments | |
350 to re_{search,match}{,_2}, we've removed the need to do nasty things | |
351 to deal with regex reentrancy. (See stack trace in signal.c for proof | |
352 that this can happen.) | |
353 | |
354 #### there is still a potential problem with the regex cache -- | |
355 the compiled regex could be overwritten. we'd need 20-fold | |
356 reentrancy, though. Fix this. */ | |
357 | |
358 i = re_match_2 (bufp, (char *) BYTE_BUF_BYTE_ADDRESS (buf, p1), | |
359 s1, (char *) BYTE_BUF_BYTE_ADDRESS (buf, p2), s2, | |
360 BYTE_BUF_PT (buf) - BYTE_BUF_BEGV (buf), &search_regs, | |
361 BYTE_BUF_ZV (buf) - BYTE_BUF_BEGV (buf), wrap_buffer (buf), | |
362 buf, scache); | |
428 | 363 |
364 if (i == -2) | |
365 matcher_overflow (); | |
366 | |
367 val = (0 <= i ? Qt : Qnil); | |
368 if (NILP (val)) | |
826 | 369 return Qnil; |
428 | 370 { |
371 int num_regs = search_regs.num_regs; | |
372 for (i = 0; i < num_regs; i++) | |
373 if (search_regs.start[i] >= 0) | |
374 { | |
826 | 375 search_regs.start[i] += BYTE_BUF_BEGV (buf); |
376 search_regs.end[i] += BYTE_BUF_BEGV (buf); | |
428 | 377 } |
378 } | |
793 | 379 last_thing_searched = wrap_buffer (buf); |
428 | 380 fixup_search_regs_for_buffer (buf); |
826 | 381 return val; |
428 | 382 } |
383 | |
384 DEFUN ("looking-at", Flooking_at, 1, 2, 0, /* | |
385 Return t if text after point matches regular expression REGEXP. | |
1468 | 386 When the match is successful, this function modifies the match data |
387 that `match-beginning', `match-end' and `match-data' access; save the | |
388 match data with `match-data' and restore it with `store-match-data' if | |
389 you want to preserve them. If the match fails, the match data from the | |
390 previous success match is preserved. | |
428 | 391 |
392 Optional argument BUFFER defaults to the current buffer. | |
393 */ | |
394 (regexp, buffer)) | |
395 { | |
396 return looking_at_1 (regexp, decode_buffer (buffer, 0), 0); | |
397 } | |
398 | |
399 DEFUN ("posix-looking-at", Fposix_looking_at, 1, 2, 0, /* | |
400 Return t if text after point matches regular expression REGEXP. | |
401 Find the longest match, in accord with Posix regular expression rules. | |
1468 | 402 When the match is successful, this function modifies the match data |
403 that `match-beginning', `match-end' and `match-data' access; save the | |
404 match data with `match-data' and restore it with `store-match-data' if | |
405 you want to preserve them. If the match fails, the match data from the | |
406 previous success match is preserved. | |
428 | 407 |
408 Optional argument BUFFER defaults to the current buffer. | |
409 */ | |
410 (regexp, buffer)) | |
411 { | |
826 | 412 return looking_at_1 (regexp, decode_buffer (buffer, 0), 1); |
428 | 413 } |
414 | |
415 static Lisp_Object | |
416 string_match_1 (Lisp_Object regexp, Lisp_Object string, Lisp_Object start, | |
2286 | 417 struct buffer *buf, int UNUSED (posix)) |
428 | 418 { |
419 Bytecount val; | |
420 Charcount s; | |
421 struct re_pattern_buffer *bufp; | |
422 | |
853 | 423 /* Some FSF junk with running_asynch_code, to preserve the match |
424 data. Not necessary because we don't call process filters | |
425 asynchronously (i.e. from within QUIT). */ | |
428 | 426 |
427 CHECK_STRING (regexp); | |
428 CHECK_STRING (string); | |
429 | |
430 if (NILP (start)) | |
431 s = 0; | |
432 else | |
433 { | |
826 | 434 Charcount len = string_char_length (string); |
428 | 435 |
436 CHECK_INT (start); | |
437 s = XINT (start); | |
438 if (s < 0 && -s <= len) | |
439 s = len + s; | |
440 else if (0 > s || s > len) | |
441 args_out_of_range (string, start); | |
442 } | |
443 | |
444 | |
445 bufp = compile_pattern (regexp, &search_regs, | |
446 (!NILP (buf->case_fold_search) | |
446 | 447 ? XCASE_TABLE_DOWNCASE (buf->case_table) : Qnil), |
826 | 448 string, buf, 0, ERROR_ME); |
428 | 449 QUIT; |
450 { | |
793 | 451 Bytecount bis = string_index_char_to_byte (string, s); |
826 | 452 struct syntax_cache scache_struct; |
453 struct syntax_cache *scache = &scache_struct; | |
454 | |
455 /* By making the regex object, regex buffer, and syntax cache arguments | |
456 to re_{search,match}{,_2}, we've removed the need to do nasty things | |
457 to deal with regex reentrancy. (See stack trace in signal.c for proof | |
458 that this can happen.) | |
459 | |
460 #### there is still a potential problem with the regex cache -- | |
461 the compiled regex could be overwritten. we'd need 20-fold | |
462 reentrancy, though. Fix this. */ | |
463 | |
428 | 464 val = re_search (bufp, (char *) XSTRING_DATA (string), |
465 XSTRING_LENGTH (string), bis, | |
466 XSTRING_LENGTH (string) - bis, | |
826 | 467 &search_regs, string, buf, scache); |
428 | 468 } |
469 if (val == -2) | |
470 matcher_overflow (); | |
826 | 471 if (val < 0) return Qnil; |
428 | 472 last_thing_searched = Qt; |
473 fixup_search_regs_for_string (string); | |
826 | 474 return make_int (string_index_byte_to_char (string, val)); |
428 | 475 } |
476 | |
477 DEFUN ("string-match", Fstring_match, 2, 4, 0, /* | |
478 Return index of start of first match for REGEXP in STRING, or nil. | |
479 If third arg START is non-nil, start search at that index in STRING. | |
480 For index of first char beyond the match, do (match-end 0). | |
481 `match-end' and `match-beginning' also give indices of substrings | |
482 matched by parenthesis constructs in the pattern. | |
483 | |
826 | 484 Optional arg BUFFER controls how case folding and syntax and category |
485 lookup is done (according to the value of `case-fold-search' in that buffer | |
486 and that buffer's case tables, syntax tables, and category table). If nil | |
487 or unspecified, it defaults *NOT* to the current buffer but instead: | |
488 | |
489 -- the value of `case-fold-search' in the current buffer is still respected | |
490 because of idioms like | |
491 | |
492 (let ((case-fold-search nil)) | |
493 (string-match "^foo.*bar" string)) | |
494 | |
495 but the case, syntax, and category tables come from the standard tables, | |
1468 | 496 which are accessed through functions `default-{case,syntax,category}-table' |
497 and serve as the parents of the tables in particular buffer. | |
498 | |
499 When the match is successful, this function modifies the match data | |
500 that `match-beginning', `match-end' and `match-data' access; save the | |
501 match data with `match-data' and restore it with `store-match-data' if | |
502 you want to preserve them. If the match fails, the match data from the | |
503 previous success match is preserved. | |
428 | 504 */ |
505 (regexp, string, start, buffer)) | |
506 { | |
826 | 507 /* &&#### implement new interp for buffer arg; check code to see if it |
508 makes more sense than prev */ | |
428 | 509 return string_match_1 (regexp, string, start, decode_buffer (buffer, 0), 0); |
510 } | |
511 | |
512 DEFUN ("posix-string-match", Fposix_string_match, 2, 4, 0, /* | |
513 Return index of start of first match for REGEXP in STRING, or nil. | |
514 Find the longest match, in accord with Posix regular expression rules. | |
515 If third arg START is non-nil, start search at that index in STRING. | |
516 For index of first char beyond the match, do (match-end 0). | |
517 `match-end' and `match-beginning' also give indices of substrings | |
518 matched by parenthesis constructs in the pattern. | |
519 | |
520 Optional arg BUFFER controls how case folding is done (according to | |
521 the value of `case-fold-search' in that buffer and that buffer's case | |
522 tables) and defaults to the current buffer. | |
1468 | 523 |
524 When the match is successful, this function modifies the match data | |
525 that `match-beginning', `match-end' and `match-data' access; save the | |
526 match data with `match-data' and restore it with `store-match-data' if | |
527 you want to preserve them. If the match fails, the match data from the | |
528 previous success match is preserved. | |
428 | 529 */ |
530 (regexp, string, start, buffer)) | |
531 { | |
532 return string_match_1 (regexp, string, start, decode_buffer (buffer, 0), 1); | |
533 } | |
534 | |
535 /* Match REGEXP against STRING, searching all of STRING, | |
536 and return the index of the match, or negative on failure. | |
537 This does not clobber the match data. */ | |
538 | |
539 Bytecount | |
1347 | 540 fast_string_match (Lisp_Object regexp, const Ibyte *nonreloc, |
428 | 541 Lisp_Object reloc, Bytecount offset, |
542 Bytecount length, int case_fold_search, | |
578 | 543 Error_Behavior errb, int no_quit) |
428 | 544 { |
545 Bytecount val; | |
867 | 546 Ibyte *newnonreloc = (Ibyte *) nonreloc; |
428 | 547 struct re_pattern_buffer *bufp; |
826 | 548 struct syntax_cache scache_struct; |
549 struct syntax_cache *scache = &scache_struct; | |
428 | 550 |
551 bufp = compile_pattern (regexp, 0, | |
552 (case_fold_search | |
771 | 553 ? XCASE_TABLE_DOWNCASE (Vstandard_case_table) |
446 | 554 : Qnil), |
826 | 555 reloc, 0, 0, errb); |
428 | 556 if (!bufp) |
557 return -1; /* will only do this when errb != ERROR_ME */ | |
558 if (!no_quit) | |
559 QUIT; | |
560 else | |
561 no_quit_in_re_search = 1; | |
562 | |
563 fixup_internal_substring (nonreloc, reloc, offset, &length); | |
564 | |
771 | 565 /* Don't need to protect against GC inside of re_search() due to QUIT; |
566 QUIT is GC-inhibited. */ | |
428 | 567 if (!NILP (reloc)) |
771 | 568 newnonreloc = XSTRING_DATA (reloc); |
569 | |
826 | 570 /* By making the regex object, regex buffer, and syntax cache arguments |
571 to re_{search,match}{,_2}, we've removed the need to do nasty things | |
572 to deal with regex reentrancy. (See stack trace in signal.c for proof | |
573 that this can happen.) | |
574 | |
575 #### there is still a potential problem with the regex cache -- | |
576 the compiled regex could be overwritten. we'd need 20-fold | |
577 reentrancy, though. Fix this. */ | |
578 | |
428 | 579 val = re_search (bufp, (char *) newnonreloc + offset, length, 0, |
826 | 580 length, 0, reloc, 0, scache); |
428 | 581 |
582 no_quit_in_re_search = 0; | |
583 return val; | |
584 } | |
585 | |
586 Bytecount | |
587 fast_lisp_string_match (Lisp_Object regex, Lisp_Object string) | |
588 { | |
589 return fast_string_match (regex, 0, string, 0, -1, 0, ERROR_ME, 0); | |
590 } | |
591 | |
592 | |
593 #ifdef REGION_CACHE_NEEDS_WORK | |
594 /* The newline cache: remembering which sections of text have no newlines. */ | |
595 | |
596 /* If the user has requested newline caching, make sure it's on. | |
597 Otherwise, make sure it's off. | |
598 This is our cheezy way of associating an action with the change of | |
599 state of a buffer-local variable. */ | |
600 static void | |
601 newline_cache_on_off (struct buffer *buf) | |
602 { | |
603 if (NILP (buf->cache_long_line_scans)) | |
604 { | |
605 /* It should be off. */ | |
606 if (buf->newline_cache) | |
607 { | |
608 free_region_cache (buf->newline_cache); | |
609 buf->newline_cache = 0; | |
610 } | |
611 } | |
612 else | |
613 { | |
614 /* It should be on. */ | |
615 if (buf->newline_cache == 0) | |
616 buf->newline_cache = new_region_cache (); | |
617 } | |
618 } | |
619 #endif | |
620 | |
621 /* Search in BUF for COUNT instances of the character TARGET between | |
622 START and END. | |
623 | |
624 If COUNT is positive, search forwards; END must be >= START. | |
625 If COUNT is negative, search backwards for the -COUNTth instance; | |
626 END must be <= START. | |
627 If COUNT is zero, do anything you please; run rogue, for all I care. | |
628 | |
629 If END is zero, use BEGV or ZV instead, as appropriate for the | |
630 direction indicated by COUNT. | |
631 | |
632 If we find COUNT instances, set *SHORTAGE to zero, and return the | |
633 position after the COUNTth match. Note that for reverse motion | |
634 this is not the same as the usual convention for Emacs motion commands. | |
635 | |
636 If we don't find COUNT instances before reaching END, set *SHORTAGE | |
637 to the number of TARGETs left unfound, and return END. | |
638 | |
639 If ALLOW_QUIT is non-zero, call QUIT periodically. */ | |
640 | |
665 | 641 static Bytebpos |
867 | 642 byte_scan_buffer (struct buffer *buf, Ichar target, Bytebpos st, Bytebpos en, |
872 | 643 EMACS_INT count, EMACS_INT *shortage, int allow_quit) |
428 | 644 { |
665 | 645 Bytebpos lim = en > 0 ? en : |
826 | 646 ((count > 0) ? BYTE_BUF_ZV (buf) : BYTE_BUF_BEGV (buf)); |
428 | 647 |
648 /* #### newline cache stuff in this function not yet ported */ | |
649 assert (count != 0); | |
650 | |
651 if (shortage) | |
652 *shortage = 0; | |
653 | |
654 if (count > 0) | |
655 { | |
656 #ifdef MULE | |
826 | 657 Internal_Format fmt = buf->text->format; |
658 /* Check for char that's unrepresentable in the buffer -- it | |
659 certainly can't be there. */ | |
867 | 660 if (!ichar_fits_in_format (target, fmt, wrap_buffer (buf))) |
428 | 661 { |
826 | 662 *shortage = count; |
663 return lim; | |
664 } | |
665 /* Due to the Mule representation of characters in a buffer, we can | |
666 simply search for characters in the range 0 - 127 directly; for | |
667 8-bit-fixed, we can do this for all characters. In other cases, | |
668 we do it the "hard" way. Note that this way works for all | |
669 characters and all formats, but the other way is faster. */ | |
670 else if (! (fmt == FORMAT_8_BIT_FIXED || | |
867 | 671 (fmt == FORMAT_DEFAULT && ichar_ascii_p (target)))) |
826 | 672 { |
867 | 673 Raw_Ichar raw = ichar_to_raw (target, fmt, wrap_buffer (buf)); |
428 | 674 while (st < lim && count > 0) |
675 { | |
826 | 676 if (BYTE_BUF_FETCH_CHAR_RAW (buf, st) == raw) |
428 | 677 count--; |
665 | 678 INC_BYTEBPOS (buf, st); |
428 | 679 } |
680 } | |
681 else | |
682 #endif | |
683 { | |
867 | 684 Raw_Ichar raw = ichar_to_raw (target, fmt, wrap_buffer (buf)); |
428 | 685 while (st < lim && count > 0) |
686 { | |
665 | 687 Bytebpos ceil; |
867 | 688 Ibyte *bufptr; |
428 | 689 |
826 | 690 ceil = BYTE_BUF_CEILING_OF (buf, st); |
428 | 691 ceil = min (lim, ceil); |
867 | 692 bufptr = (Ibyte *) memchr (BYTE_BUF_BYTE_ADDRESS (buf, st), |
826 | 693 raw, ceil - st); |
428 | 694 if (bufptr) |
695 { | |
696 count--; | |
826 | 697 st = BYTE_BUF_PTR_BYTE_POS (buf, bufptr) + 1; |
428 | 698 } |
699 else | |
700 st = ceil; | |
701 } | |
702 } | |
703 | |
704 if (shortage) | |
705 *shortage = count; | |
706 if (allow_quit) | |
707 QUIT; | |
708 return st; | |
709 } | |
710 else | |
711 { | |
712 #ifdef MULE | |
826 | 713 Internal_Format fmt = buf->text->format; |
714 /* Check for char that's unrepresentable in the buffer -- it | |
715 certainly can't be there. */ | |
867 | 716 if (!ichar_fits_in_format (target, fmt, wrap_buffer (buf))) |
428 | 717 { |
826 | 718 *shortage = -count; |
719 return lim; | |
720 } | |
721 else if (! (fmt == FORMAT_8_BIT_FIXED || | |
867 | 722 (fmt == FORMAT_DEFAULT && ichar_ascii_p (target)))) |
826 | 723 { |
867 | 724 Raw_Ichar raw = ichar_to_raw (target, fmt, wrap_buffer (buf)); |
428 | 725 while (st > lim && count < 0) |
726 { | |
665 | 727 DEC_BYTEBPOS (buf, st); |
826 | 728 if (BYTE_BUF_FETCH_CHAR_RAW (buf, st) == raw) |
428 | 729 count++; |
730 } | |
731 } | |
732 else | |
733 #endif | |
734 { | |
867 | 735 Raw_Ichar raw = ichar_to_raw (target, fmt, wrap_buffer (buf)); |
428 | 736 while (st > lim && count < 0) |
737 { | |
665 | 738 Bytebpos floor; |
867 | 739 Ibyte *bufptr; |
740 Ibyte *floorptr; | |
428 | 741 |
826 | 742 floor = BYTE_BUF_FLOOR_OF (buf, st); |
428 | 743 floor = max (lim, floor); |
744 /* No memrchr() ... */ | |
826 | 745 bufptr = BYTE_BUF_BYTE_ADDRESS_BEFORE (buf, st); |
746 floorptr = BYTE_BUF_BYTE_ADDRESS (buf, floor); | |
428 | 747 while (bufptr >= floorptr) |
748 { | |
749 st--; | |
750 /* At this point, both ST and BUFPTR refer to the same | |
751 character. When the loop terminates, ST will | |
752 always point to the last character we tried. */ | |
867 | 753 if (*bufptr == (Ibyte) raw) |
428 | 754 { |
755 count++; | |
756 break; | |
757 } | |
758 bufptr--; | |
759 } | |
760 } | |
761 } | |
762 | |
763 if (shortage) | |
764 *shortage = -count; | |
765 if (allow_quit) | |
766 QUIT; | |
767 if (count) | |
768 return st; | |
769 else | |
770 { | |
771 /* We found the character we were looking for; we have to return | |
772 the position *after* it due to the strange way that the return | |
773 value is defined. */ | |
665 | 774 INC_BYTEBPOS (buf, st); |
428 | 775 return st; |
776 } | |
777 } | |
778 } | |
779 | |
665 | 780 Charbpos |
867 | 781 scan_buffer (struct buffer *buf, Ichar target, Charbpos start, Charbpos end, |
428 | 782 EMACS_INT count, EMACS_INT *shortage, int allow_quit) |
783 { | |
826 | 784 Bytebpos byte_retval; |
785 Bytebpos byte_start, byte_end; | |
786 | |
787 byte_start = charbpos_to_bytebpos (buf, start); | |
428 | 788 if (end) |
826 | 789 byte_end = charbpos_to_bytebpos (buf, end); |
428 | 790 else |
826 | 791 byte_end = 0; |
792 byte_retval = byte_scan_buffer (buf, target, byte_start, byte_end, count, | |
428 | 793 shortage, allow_quit); |
826 | 794 return bytebpos_to_charbpos (buf, byte_retval); |
428 | 795 } |
796 | |
665 | 797 Bytebpos |
826 | 798 byte_find_next_newline_no_quit (struct buffer *buf, Bytebpos from, int count) |
428 | 799 { |
826 | 800 return byte_scan_buffer (buf, '\n', from, 0, count, 0, 0); |
428 | 801 } |
802 | |
665 | 803 Charbpos |
804 find_next_newline_no_quit (struct buffer *buf, Charbpos from, int count) | |
428 | 805 { |
806 return scan_buffer (buf, '\n', from, 0, count, 0, 0); | |
807 } | |
808 | |
665 | 809 Charbpos |
810 find_next_newline (struct buffer *buf, Charbpos from, int count) | |
428 | 811 { |
812 return scan_buffer (buf, '\n', from, 0, count, 0, 1); | |
813 } | |
814 | |
826 | 815 Bytecount |
867 | 816 byte_find_next_ichar_in_string (Lisp_Object str, Ichar target, Bytecount st, |
428 | 817 EMACS_INT count) |
818 { | |
793 | 819 Bytebpos lim = XSTRING_LENGTH (str) -1; |
867 | 820 Ibyte *s = XSTRING_DATA (str); |
428 | 821 |
822 assert (count >= 0); | |
823 | |
824 #ifdef MULE | |
825 /* Due to the Mule representation of characters in a buffer, | |
826 we can simply search for characters in the range 0 - 127 | |
827 directly. For other characters, we do it the "hard" way. | |
828 Note that this way works for all characters but the other | |
829 way is faster. */ | |
830 if (target >= 0200) | |
831 { | |
832 while (st < lim && count > 0) | |
833 { | |
867 | 834 if (string_ichar (str, st) == target) |
428 | 835 count--; |
826 | 836 INC_BYTECOUNT (s, st); |
428 | 837 } |
838 } | |
839 else | |
840 #endif | |
841 { | |
842 while (st < lim && count > 0) | |
843 { | |
867 | 844 Ibyte *bufptr = (Ibyte *) memchr (itext_n_addr (s, st), |
428 | 845 (int) target, lim - st); |
846 if (bufptr) | |
847 { | |
848 count--; | |
826 | 849 st = (Bytebpos) (bufptr - s) + 1; |
428 | 850 } |
851 else | |
852 st = lim; | |
853 } | |
854 } | |
855 return st; | |
856 } | |
857 | |
858 /* Like find_next_newline, but returns position before the newline, | |
859 not after, and only search up to TO. This isn't just | |
860 find_next_newline (...)-1, because you might hit TO. */ | |
665 | 861 Charbpos |
826 | 862 find_before_next_newline (struct buffer *buf, Charbpos from, Charbpos to, |
863 int count) | |
428 | 864 { |
865 EMACS_INT shortage; | |
665 | 866 Charbpos pos = scan_buffer (buf, '\n', from, to, count, &shortage, 1); |
428 | 867 |
868 if (shortage == 0) | |
869 pos--; | |
870 | |
871 return pos; | |
872 } | |
873 | |
872 | 874 /* This function synched with FSF 21.1 */ |
428 | 875 static Lisp_Object |
876 skip_chars (struct buffer *buf, int forwardp, int syntaxp, | |
877 Lisp_Object string, Lisp_Object lim) | |
878 { | |
867 | 879 REGISTER Ibyte *p, *pend; |
880 REGISTER Ichar c; | |
428 | 881 /* We store the first 256 chars in an array here and the rest in |
882 a range table. */ | |
883 unsigned char fastmap[0400]; | |
884 int negate = 0; | |
885 REGISTER int i; | |
665 | 886 Charbpos limit; |
826 | 887 struct syntax_cache *scache; |
888 | |
428 | 889 if (NILP (lim)) |
890 limit = forwardp ? BUF_ZV (buf) : BUF_BEGV (buf); | |
891 else | |
892 { | |
893 CHECK_INT_COERCE_MARKER (lim); | |
894 limit = XINT (lim); | |
895 | |
896 /* In any case, don't allow scan outside bounds of buffer. */ | |
897 if (limit > BUF_ZV (buf)) limit = BUF_ZV (buf); | |
898 if (limit < BUF_BEGV (buf)) limit = BUF_BEGV (buf); | |
899 } | |
900 | |
901 CHECK_STRING (string); | |
902 p = XSTRING_DATA (string); | |
903 pend = p + XSTRING_LENGTH (string); | |
904 memset (fastmap, 0, sizeof (fastmap)); | |
905 | |
906 Fclear_range_table (Vskip_chars_range_table); | |
907 | |
908 if (p != pend && *p == '^') | |
909 { | |
910 negate = 1; | |
911 p++; | |
912 } | |
913 | |
914 /* Find the characters specified and set their elements of fastmap. | |
915 If syntaxp, each character counts as itself. | |
916 Otherwise, handle backslashes and ranges specially */ | |
917 | |
918 while (p != pend) | |
919 { | |
867 | 920 c = itext_ichar (p); |
921 INC_IBYTEPTR (p); | |
428 | 922 if (syntaxp) |
923 { | |
924 if (c < 0400 && syntax_spec_code[c] < (unsigned char) Smax) | |
925 fastmap[c] = 1; | |
926 else | |
831 | 927 invalid_argument ("Invalid syntax designator", make_char (c)); |
428 | 928 } |
929 else | |
930 { | |
931 if (c == '\\') | |
932 { | |
933 if (p == pend) break; | |
867 | 934 c = itext_ichar (p); |
935 INC_IBYTEPTR (p); | |
428 | 936 } |
937 if (p != pend && *p == '-') | |
938 { | |
867 | 939 Ichar cend; |
428 | 940 |
872 | 941 /* Skip over the dash. */ |
428 | 942 p++; |
943 if (p == pend) break; | |
867 | 944 cend = itext_ichar (p); |
428 | 945 while (c <= cend && c < 0400) |
946 { | |
947 fastmap[c] = 1; | |
948 c++; | |
949 } | |
950 if (c <= cend) | |
951 Fput_range_table (make_int (c), make_int (cend), Qt, | |
952 Vskip_chars_range_table); | |
867 | 953 INC_IBYTEPTR (p); |
428 | 954 } |
955 else | |
956 { | |
957 if (c < 0400) | |
958 fastmap[c] = 1; | |
959 else | |
960 Fput_range_table (make_int (c), make_int (c), Qt, | |
961 Vskip_chars_range_table); | |
962 } | |
963 } | |
964 } | |
965 | |
872 | 966 /* #### Not in FSF 21.1 */ |
428 | 967 if (syntaxp && fastmap['-'] != 0) |
968 fastmap[' '] = 1; | |
969 | |
970 /* If ^ was the first character, complement the fastmap. | |
971 We don't complement the range table, however; we just use negate | |
972 in the comparisons below. */ | |
973 | |
974 if (negate) | |
647 | 975 for (i = 0; i < (int) (sizeof (fastmap)); i++) |
428 | 976 fastmap[i] ^= 1; |
977 | |
978 { | |
665 | 979 Charbpos start_point = BUF_PT (buf); |
872 | 980 Charbpos pos = start_point; |
981 Charbpos pos_byte = BYTE_BUF_PT (buf); | |
428 | 982 |
983 if (syntaxp) | |
984 { | |
872 | 985 scache = setup_buffer_syntax_cache (buf, pos, forwardp ? 1 : -1); |
428 | 986 /* All syntax designators are normal chars so nothing strange |
987 to worry about */ | |
988 if (forwardp) | |
989 { | |
872 | 990 if (pos < limit) |
991 while (fastmap[(unsigned char) | |
992 syntax_code_spec | |
993 [(int) SYNTAX_FROM_CACHE | |
994 (scache, BYTE_BUF_FETCH_CHAR (buf, pos_byte))]]) | |
995 { | |
996 pos++; | |
997 INC_BYTEBPOS (buf, pos_byte); | |
879 | 998 if (pos >= limit) |
872 | 999 break; |
1000 UPDATE_SYNTAX_CACHE_FORWARD (scache, pos); | |
1001 } | |
428 | 1002 } |
1003 else | |
1004 { | |
872 | 1005 while (pos > limit) |
460 | 1006 { |
872 | 1007 Charbpos savepos = pos_byte; |
1008 pos--; | |
1009 DEC_BYTEBPOS (buf, pos_byte); | |
1010 UPDATE_SYNTAX_CACHE_BACKWARD (scache, pos); | |
1011 if (!fastmap[(unsigned char) | |
1012 syntax_code_spec | |
1013 [(int) SYNTAX_FROM_CACHE | |
1014 (scache, BYTE_BUF_FETCH_CHAR (buf, pos_byte))]]) | |
1015 { | |
1016 pos++; | |
1017 pos_byte = savepos; | |
1018 break; | |
1019 } | |
460 | 1020 } |
428 | 1021 } |
1022 } | |
1023 else | |
1024 { | |
1025 if (forwardp) | |
1026 { | |
872 | 1027 while (pos < limit) |
428 | 1028 { |
872 | 1029 Ichar ch = BYTE_BUF_FETCH_CHAR (buf, pos_byte); |
428 | 1030 if ((ch < 0400) ? fastmap[ch] : |
1031 (NILP (Fget_range_table (make_int (ch), | |
1032 Vskip_chars_range_table, | |
1033 Qnil)) | |
1034 == negate)) | |
872 | 1035 { |
1036 pos++; | |
1037 INC_BYTEBPOS (buf, pos_byte); | |
1038 } | |
428 | 1039 else |
1040 break; | |
1041 } | |
1042 } | |
1043 else | |
1044 { | |
872 | 1045 while (pos > limit) |
428 | 1046 { |
872 | 1047 Charbpos prev_pos_byte = pos_byte; |
1048 Ichar ch; | |
1049 | |
1050 DEC_BYTEBPOS (buf, prev_pos_byte); | |
1051 ch = BYTE_BUF_FETCH_CHAR (buf, prev_pos_byte); | |
428 | 1052 if ((ch < 0400) ? fastmap[ch] : |
1053 (NILP (Fget_range_table (make_int (ch), | |
1054 Vskip_chars_range_table, | |
1055 Qnil)) | |
1056 == negate)) | |
872 | 1057 { |
1058 pos--; | |
1059 pos_byte = prev_pos_byte; | |
1060 } | |
428 | 1061 else |
1062 break; | |
1063 } | |
1064 } | |
1065 } | |
1066 QUIT; | |
872 | 1067 BOTH_BUF_SET_PT (buf, pos, pos_byte); |
428 | 1068 return make_int (BUF_PT (buf) - start_point); |
1069 } | |
1070 } | |
1071 | |
1072 DEFUN ("skip-chars-forward", Fskip_chars_forward, 1, 3, 0, /* | |
444 | 1073 Move point forward, stopping before a char not in STRING, or at pos LIMIT. |
428 | 1074 STRING is like the inside of a `[...]' in a regular expression |
1075 except that `]' is never special and `\\' quotes `^', `-' or `\\'. | |
1076 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter. | |
1077 With arg "^a-zA-Z", skips nonletters stopping before first letter. | |
1078 Returns the distance traveled, either zero or positive. | |
1079 | |
1080 Optional argument BUFFER defaults to the current buffer. | |
1081 */ | |
444 | 1082 (string, limit, buffer)) |
428 | 1083 { |
444 | 1084 return skip_chars (decode_buffer (buffer, 0), 1, 0, string, limit); |
428 | 1085 } |
1086 | |
1087 DEFUN ("skip-chars-backward", Fskip_chars_backward, 1, 3, 0, /* | |
444 | 1088 Move point backward, stopping after a char not in STRING, or at pos LIMIT. |
428 | 1089 See `skip-chars-forward' for details. |
1090 Returns the distance traveled, either zero or negative. | |
1091 | |
1092 Optional argument BUFFER defaults to the current buffer. | |
1093 */ | |
444 | 1094 (string, limit, buffer)) |
428 | 1095 { |
444 | 1096 return skip_chars (decode_buffer (buffer, 0), 0, 0, string, limit); |
428 | 1097 } |
1098 | |
1099 | |
1100 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, 1, 3, 0, /* | |
1101 Move point forward across chars in specified syntax classes. | |
1102 SYNTAX is a string of syntax code characters. | |
444 | 1103 Stop before a char whose syntax is not in SYNTAX, or at position LIMIT. |
428 | 1104 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX. |
1105 This function returns the distance traveled, either zero or positive. | |
1106 | |
1107 Optional argument BUFFER defaults to the current buffer. | |
1108 */ | |
444 | 1109 (syntax, limit, buffer)) |
428 | 1110 { |
444 | 1111 return skip_chars (decode_buffer (buffer, 0), 1, 1, syntax, limit); |
428 | 1112 } |
1113 | |
1114 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, 1, 3, 0, /* | |
1115 Move point backward across chars in specified syntax classes. | |
1116 SYNTAX is a string of syntax code characters. | |
444 | 1117 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIMIT. |
428 | 1118 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX. |
1119 This function returns the distance traveled, either zero or negative. | |
1120 | |
1121 Optional argument BUFFER defaults to the current buffer. | |
1122 */ | |
444 | 1123 (syntax, limit, buffer)) |
428 | 1124 { |
444 | 1125 return skip_chars (decode_buffer (buffer, 0), 0, 1, syntax, limit); |
428 | 1126 } |
1127 | |
1128 | |
1129 /* Subroutines of Lisp buffer search functions. */ | |
1130 | |
1131 static Lisp_Object | |
444 | 1132 search_command (Lisp_Object string, Lisp_Object limit, Lisp_Object noerror, |
428 | 1133 Lisp_Object count, Lisp_Object buffer, int direction, |
1134 int RE, int posix) | |
1135 { | |
665 | 1136 REGISTER Charbpos np; |
1137 Charbpos lim; | |
428 | 1138 EMACS_INT n = direction; |
1139 struct buffer *buf; | |
1140 | |
1141 if (!NILP (count)) | |
1142 { | |
1143 CHECK_INT (count); | |
1144 n *= XINT (count); | |
1145 } | |
1146 | |
1147 buf = decode_buffer (buffer, 0); | |
1148 CHECK_STRING (string); | |
444 | 1149 if (NILP (limit)) |
428 | 1150 lim = n > 0 ? BUF_ZV (buf) : BUF_BEGV (buf); |
1151 else | |
1152 { | |
444 | 1153 CHECK_INT_COERCE_MARKER (limit); |
1154 lim = XINT (limit); | |
428 | 1155 if (n > 0 ? lim < BUF_PT (buf) : lim > BUF_PT (buf)) |
563 | 1156 invalid_argument ("Invalid search limit (wrong side of point)", |
1157 Qunbound); | |
428 | 1158 if (lim > BUF_ZV (buf)) |
1159 lim = BUF_ZV (buf); | |
1160 if (lim < BUF_BEGV (buf)) | |
1161 lim = BUF_BEGV (buf); | |
1162 } | |
1163 | |
1164 np = search_buffer (buf, string, BUF_PT (buf), lim, n, RE, | |
1165 (!NILP (buf->case_fold_search) | |
446 | 1166 ? XCASE_TABLE_CANON (buf->case_table) |
1167 : Qnil), | |
428 | 1168 (!NILP (buf->case_fold_search) |
446 | 1169 ? XCASE_TABLE_EQV (buf->case_table) |
1170 : Qnil), posix); | |
428 | 1171 |
1172 if (np <= 0) | |
1173 { | |
444 | 1174 if (NILP (noerror)) |
2268 | 1175 { |
1176 signal_failure (string); | |
1177 RETURN_NOT_REACHED (Qnil); | |
1178 } | |
444 | 1179 if (!EQ (noerror, Qt)) |
428 | 1180 { |
1181 if (lim < BUF_BEGV (buf) || lim > BUF_ZV (buf)) | |
2500 | 1182 ABORT (); |
428 | 1183 BUF_SET_PT (buf, lim); |
1184 return Qnil; | |
1185 #if 0 /* This would be clean, but maybe programs depend on | |
1186 a value of nil here. */ | |
1187 np = lim; | |
1188 #endif | |
1189 } | |
1190 else | |
1191 return Qnil; | |
1192 } | |
1193 | |
1194 if (np < BUF_BEGV (buf) || np > BUF_ZV (buf)) | |
2500 | 1195 ABORT (); |
428 | 1196 |
1197 BUF_SET_PT (buf, np); | |
1198 | |
1199 return make_int (np); | |
1200 } | |
1201 | |
1202 static int | |
1203 trivial_regexp_p (Lisp_Object regexp) | |
1204 { | |
1205 Bytecount len = XSTRING_LENGTH (regexp); | |
867 | 1206 Ibyte *s = XSTRING_DATA (regexp); |
428 | 1207 while (--len >= 0) |
1208 { | |
1209 switch (*s++) | |
1210 { | |
1724 | 1211 /* #### howcum ']' doesn't appear here, but ... */ |
428 | 1212 case '.': case '*': case '+': case '?': case '[': case '^': case '$': |
1213 return 0; | |
1214 case '\\': | |
1215 if (--len < 0) | |
1216 return 0; | |
1217 switch (*s++) | |
1218 { | |
1724 | 1219 /* ... ')' does appear here? ('<' and '>' can appear singly.) */ |
1220 /* #### are there other constructs to check? */ | |
428 | 1221 case '|': case '(': case ')': case '`': case '\'': case 'b': |
1222 case 'B': case '<': case '>': case 'w': case 'W': case 's': | |
1724 | 1223 case 'S': case '=': case '{': case '}': |
428 | 1224 #ifdef MULE |
1225 /* 97/2/25 jhod Added for category matches */ | |
1226 case 'c': case 'C': | |
1227 #endif /* MULE */ | |
1228 case '1': case '2': case '3': case '4': case '5': | |
1229 case '6': case '7': case '8': case '9': | |
1230 return 0; | |
1231 } | |
1232 } | |
1233 } | |
1234 return 1; | |
1235 } | |
1236 | |
1237 /* Search for the n'th occurrence of STRING in BUF, | |
665 | 1238 starting at position CHARBPOS and stopping at position BUFLIM, |
428 | 1239 treating PAT as a literal string if RE is false or as |
1240 a regular expression if RE is true. | |
1241 | |
1242 If N is positive, searching is forward and BUFLIM must be greater | |
665 | 1243 than CHARBPOS. |
428 | 1244 If N is negative, searching is backward and BUFLIM must be less |
665 | 1245 than CHARBPOS. |
428 | 1246 |
1247 Returns -x if only N-x occurrences found (x > 0), | |
1248 or else the position at the beginning of the Nth occurrence | |
1249 (if searching backward) or the end (if searching forward). | |
1250 | |
1251 POSIX is nonzero if we want full backtracking (POSIX style) | |
1252 for this pattern. 0 means backtrack only enough to get a valid match. */ | |
665 | 1253 static Charbpos |
1254 search_buffer (struct buffer *buf, Lisp_Object string, Charbpos charbpos, | |
1255 Charbpos buflim, EMACS_INT n, int RE, Lisp_Object trt, | |
446 | 1256 Lisp_Object inverse_trt, int posix) |
428 | 1257 { |
1258 Bytecount len = XSTRING_LENGTH (string); | |
867 | 1259 Ibyte *base_pat = XSTRING_DATA (string); |
428 | 1260 REGISTER EMACS_INT i, j; |
665 | 1261 Bytebpos p1, p2; |
428 | 1262 Bytecount s1, s2; |
665 | 1263 Bytebpos pos, lim; |
428 | 1264 |
853 | 1265 /* Some FSF junk with running_asynch_code, to preserve the match |
1266 data. Not necessary because we don't call process filters | |
1267 asynchronously (i.e. from within QUIT). */ | |
428 | 1268 |
1425 | 1269 /* Searching 0 times means noop---don't move, don't touch registers. */ |
1270 if (n == 0) | |
1271 return charbpos; | |
1272 | |
428 | 1273 /* Null string is found at starting position. */ |
1274 if (len == 0) | |
1275 { | |
665 | 1276 set_search_regs (buf, charbpos, 0); |
1277 return charbpos; | |
428 | 1278 } |
1279 | |
665 | 1280 pos = charbpos_to_bytebpos (buf, charbpos); |
1281 lim = charbpos_to_bytebpos (buf, buflim); | |
428 | 1282 if (RE && !trivial_regexp_p (string)) |
1283 { | |
1284 struct re_pattern_buffer *bufp; | |
826 | 1285 |
1286 bufp = compile_pattern (string, &search_regs, trt, | |
1287 wrap_buffer (buf), buf, posix, ERROR_ME); | |
428 | 1288 |
1289 /* Get pointers and sizes of the two strings | |
1290 that make up the visible portion of the buffer. */ | |
1291 | |
826 | 1292 p1 = BYTE_BUF_BEGV (buf); |
1293 p2 = BYTE_BUF_CEILING_OF (buf, p1); | |
428 | 1294 s1 = p2 - p1; |
826 | 1295 s2 = BYTE_BUF_ZV (buf) - p2; |
1296 | |
1297 while (n != 0) | |
428 | 1298 { |
1299 Bytecount val; | |
826 | 1300 struct syntax_cache scache_struct; |
1301 struct syntax_cache *scache = &scache_struct; | |
1302 | |
428 | 1303 QUIT; |
826 | 1304 /* By making the regex object, regex buffer, and syntax cache |
1305 arguments to re_{search,match}{,_2}, we've removed the need to | |
1306 do nasty things to deal with regex reentrancy. (See stack | |
1307 trace in signal.c for proof that this can happen.) | |
1308 | |
1309 #### there is still a potential problem with the regex cache -- | |
1310 the compiled regex could be overwritten. we'd need 20-fold | |
1311 reentrancy, though. Fix this. */ | |
1312 | |
428 | 1313 val = re_search_2 (bufp, |
826 | 1314 (char *) BYTE_BUF_BYTE_ADDRESS (buf, p1), s1, |
1315 (char *) BYTE_BUF_BYTE_ADDRESS (buf, p2), s2, | |
1316 pos - BYTE_BUF_BEGV (buf), lim - pos, &search_regs, | |
1317 n > 0 ? lim - BYTE_BUF_BEGV (buf) : | |
1318 pos - BYTE_BUF_BEGV (buf), wrap_buffer (buf), | |
1319 buf, scache); | |
428 | 1320 |
1321 if (val == -2) | |
1322 { | |
1323 matcher_overflow (); | |
1324 } | |
1325 if (val >= 0) | |
1326 { | |
1327 int num_regs = search_regs.num_regs; | |
826 | 1328 j = BYTE_BUF_BEGV (buf); |
428 | 1329 for (i = 0; i < num_regs; i++) |
1330 if (search_regs.start[i] >= 0) | |
1331 { | |
1332 search_regs.start[i] += j; | |
1333 search_regs.end[i] += j; | |
1334 } | |
793 | 1335 last_thing_searched = wrap_buffer (buf); |
428 | 1336 /* Set pos to the new position. */ |
826 | 1337 pos = n > 0 ? search_regs.end[0] : search_regs.start[0]; |
428 | 1338 fixup_search_regs_for_buffer (buf); |
665 | 1339 /* And charbpos too. */ |
826 | 1340 charbpos = n > 0 ? search_regs.end[0] : search_regs.start[0]; |
428 | 1341 } |
1342 else | |
826 | 1343 return (n > 0 ? 0 - n : n); |
1344 if (n > 0) n--; else n++; | |
428 | 1345 } |
665 | 1346 return charbpos; |
428 | 1347 } |
1348 else /* non-RE case */ | |
1349 { | |
446 | 1350 int charset_base = -1; |
1351 int boyer_moore_ok = 1; | |
2367 | 1352 Ibyte *patbuf = alloca_ibytes (len * MAX_ICHAR_LEN); |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1353 Ibyte *pat = patbuf; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1354 |
446 | 1355 #ifdef MULE |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1356 int entirely_one_byte_p = buf->text->entirely_one_byte_p; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1357 int nothing_greater_than_0xff = |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1358 buf->text->num_8_bit_fixed_chars == BUF_Z(buf) - BUF_BEG (buf); |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1359 |
446 | 1360 while (len > 0) |
1361 { | |
867 | 1362 Ibyte tmp_str[MAX_ICHAR_LEN]; |
1363 Ichar c, translated, inverse; | |
446 | 1364 Bytecount orig_bytelen, new_bytelen, inv_bytelen; |
1365 | |
1366 /* If we got here and the RE flag is set, it's because | |
1367 we're dealing with a regexp known to be trivial, so the | |
1368 backslash just quotes the next character. */ | |
1369 if (RE && *base_pat == '\\') | |
1370 { | |
1371 len--; | |
1372 base_pat++; | |
1373 } | |
867 | 1374 c = itext_ichar (base_pat); |
446 | 1375 translated = TRANSLATE (trt, c); |
1376 inverse = TRANSLATE (inverse_trt, c); | |
1377 | |
867 | 1378 orig_bytelen = itext_ichar_len (base_pat); |
1379 inv_bytelen = set_itext_ichar (tmp_str, inverse); | |
1380 new_bytelen = set_itext_ichar (tmp_str, translated); | |
446 | 1381 |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1382 if (boyer_moore_ok |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1383 /* Only do the Boyer-Moore check for characters needing |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1384 translation. */ |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1385 && (translated != c || inverse != c)) |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1386 { |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1387 Ichar starting_c = c; |
4421
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1388 int charset_base_code, checked = 0; |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1389 |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1390 do |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1391 { |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1392 c = TRANSLATE (inverse_trt, c); |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1393 |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1394 /* If a character cannot occur in the buffer, ignore |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1395 it. */ |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1396 if (c > 0x7F && entirely_one_byte_p) |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1397 continue; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1398 |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1399 if (c > 0xFF && nothing_greater_than_0xff) |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1400 continue; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1401 |
4421
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1402 checked = 1; |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1403 |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1404 if (-1 == charset_base) /* No charset yet specified. */ |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1405 { |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1406 /* Keep track of which charset and character set row |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1407 contains the characters that need translation. |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1408 |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1409 Zero out the bits corresponding to the last |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1410 byte. */ |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1411 charset_base = c & ~ICHAR_FIELD3_MASK; |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1412 } |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1413 else |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1414 { |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1415 charset_base_code = c & ~ICHAR_FIELD3_MASK; |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1416 |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1417 if (charset_base_code != charset_base) |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1418 { |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1419 /* If two different rows, or two different |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1420 charsets, appear, needing non-ASCII |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1421 translation, then we cannot use boyer_moore |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1422 search. See the comment at the head of |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1423 boyer_moore(). */ |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1424 boyer_moore_ok = 0; |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1425 break; |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1426 } |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1427 } |
4901
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1428 |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1429 if (ichar_len (c) > 2) |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1430 { |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1431 /* Case-equivalence plus repeated octets throws off |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1432 the construction of the stride table; avoid this. |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1433 |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1434 It should be possible to correct boyer_moore to |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1435 behave correctly even in this case--it doesn't have |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1436 problems with repeated octets when case conversion |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1437 is not involved--but this is not a critical |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1438 issue. */ |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1439 Ibyte encoded[MAX_ICHAR_LEN]; |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1440 Bytecount len = set_itext_ichar (encoded, c); |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1441 int i, j; |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1442 for (i = 0; i < len && boyer_moore_ok; ++i) |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1443 { |
4904
e91e3e353805
Don't compare the same octet with itself if checking for boyer_moore_ok
Aidan Kehoe <kehoea@parhasard.net>
parents:
4901
diff
changeset
|
1444 for (j = i + 1; j < len && boyer_moore_ok; ++j) |
4901
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1445 { |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1446 if (encoded[i] == encoded[j]) |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1447 { |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1448 boyer_moore_ok = 0; |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1449 } |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1450 } |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1451 } |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1452 |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1453 if (0 == boyer_moore_ok) |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1454 { |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1455 break; |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1456 } |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1457 } |
7504864a986c
Don't use Boyer-Moore if repeated octets & case-insensitive search.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4897
diff
changeset
|
1458 |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1459 } while (c != starting_c); |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1460 |
4421
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1461 if (!checked) |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1462 { |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1463 #ifdef DEBUG_XEMACS |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1464 if (debug_xemacs_searches) |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1465 { |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1466 Lisp_Symbol *sym = XSYMBOL (Qsearch_algorithm_used); |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1467 sym->value = Qnil; |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1468 } |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1469 #endif |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1470 /* The "continue" clauses were used above, for every |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1471 translation of the character. As such, this character |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1472 is not to be found in the buffer and neither is the |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1473 string as a whole. Return immediately; also avoid |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1474 triggering the assertion a few lines down. */ |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1475 return n > 0 ? -n : n; |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1476 } |
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1477 |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1478 if (boyer_moore_ok && charset_base != -1 && |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1479 charset_base != (translated & ~ICHAR_FIELD3_MASK)) |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1480 { |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1481 /* In the rare event that the CANON entry for this |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1482 character is not in the desired set, choose one |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1483 that is, from the equivalence set. It doesn't much |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1484 matter which. */ |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1485 Ichar starting_ch = translated; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1486 do |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1487 { |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1488 translated = TRANSLATE (inverse_trt, translated); |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1489 |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1490 if (charset_base == (translated & ~ICHAR_FIELD3_MASK)) |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1491 break; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1492 |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1493 } while (starting_ch != translated); |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1494 |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1495 assert (starting_ch != translated); |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1496 |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1497 new_bytelen = set_itext_ichar (tmp_str, translated); |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1498 } |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1499 } |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1500 |
446 | 1501 memcpy (pat, tmp_str, new_bytelen); |
1502 pat += new_bytelen; | |
1503 base_pat += orig_bytelen; | |
1504 len -= orig_bytelen; | |
1505 } | |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1506 |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1507 if (-1 == charset_base) |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1508 { |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1509 charset_base = 'a' & ~ICHAR_FIELD3_MASK; /* Default to ASCII. */ |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1510 } |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1511 |
446 | 1512 #else /* not MULE */ |
1513 while (--len >= 0) | |
1514 { | |
1515 /* If we got here and the RE flag is set, it's because | |
1516 we're dealing with a regexp known to be trivial, so the | |
1517 backslash just quotes the next character. */ | |
1518 if (RE && *base_pat == '\\') | |
1519 { | |
1520 len--; | |
1521 base_pat++; | |
1522 } | |
1523 *pat++ = TRANSLATE (trt, *base_pat++); | |
1524 } | |
1525 #endif /* MULE */ | |
1526 len = pat - patbuf; | |
1527 pat = base_pat = patbuf; | |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1528 |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1529 #ifdef DEBUG_XEMACS |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1530 if (debug_xemacs_searches) |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1531 { |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1532 Lisp_Symbol *sym = XSYMBOL (Qsearch_algorithm_used); |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1533 sym->value = boyer_moore_ok ? Qboyer_moore : Qsimple_search; |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1534 } |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1535 #endif |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1536 |
446 | 1537 if (boyer_moore_ok) |
1538 return boyer_moore (buf, base_pat, len, pos, lim, n, | |
1539 trt, inverse_trt, charset_base); | |
1540 else | |
1541 return simple_search (buf, base_pat, len, pos, lim, n, trt); | |
1542 } | |
1543 } | |
1544 | |
826 | 1545 /* Do a simple string search N times for the string PAT, whose length is |
1546 LEN/LEN_BYTE, from buffer position POS until LIM. TRT is the | |
1547 translation table. | |
446 | 1548 |
1549 Return the character position where the match is found. | |
1550 Otherwise, if M matches remained to be found, return -M. | |
1551 | |
1552 This kind of search works regardless of what is in PAT and | |
1553 regardless of what is in TRT. It is used in cases where | |
1554 boyer_moore cannot work. */ | |
1555 | |
665 | 1556 static Charbpos |
867 | 1557 simple_search (struct buffer *buf, Ibyte *base_pat, Bytecount len, |
826 | 1558 Bytebpos pos, Bytebpos lim, EMACS_INT n, Lisp_Object trt) |
446 | 1559 { |
1560 int forward = n > 0; | |
1561 Bytecount buf_len = 0; /* Shut up compiler. */ | |
1562 | |
826 | 1563 if (lim > pos) |
446 | 1564 while (n > 0) |
428 | 1565 { |
446 | 1566 while (1) |
428 | 1567 { |
826 | 1568 Bytecount this_len = len; |
1569 Bytebpos this_pos = pos; | |
867 | 1570 Ibyte *p = base_pat; |
826 | 1571 if (pos >= lim) |
446 | 1572 goto stop; |
1573 | |
1574 while (this_len > 0) | |
1575 { | |
867 | 1576 Ichar pat_ch, buf_ch; |
446 | 1577 Bytecount pat_len; |
1578 | |
867 | 1579 pat_ch = itext_ichar (p); |
826 | 1580 buf_ch = BYTE_BUF_FETCH_CHAR (buf, this_pos); |
446 | 1581 |
1582 buf_ch = TRANSLATE (trt, buf_ch); | |
1583 | |
1584 if (buf_ch != pat_ch) | |
1585 break; | |
1586 | |
867 | 1587 pat_len = itext_ichar_len (p); |
446 | 1588 p += pat_len; |
1589 this_len -= pat_len; | |
826 | 1590 INC_BYTEBPOS (buf, this_pos); |
446 | 1591 } |
1592 if (this_len == 0) | |
428 | 1593 { |
826 | 1594 buf_len = this_pos - pos; |
1595 pos = this_pos; | |
446 | 1596 break; |
428 | 1597 } |
826 | 1598 INC_BYTEBPOS (buf, pos); |
428 | 1599 } |
446 | 1600 n--; |
1601 } | |
1602 else | |
4322
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1603 { |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1604 /* If lim < len, then there are too few buffer positions to hold the |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1605 pattern between the beginning of the buffer and lim. Adjust to |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1606 ensure pattern fits. If we don't do this, we can assert in the |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1607 DEC_BYTEBPOS below. */ |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1608 if (lim < len) |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1609 lim = len; |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1610 while (n < 0) |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1611 { |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1612 while (1) |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1613 { |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1614 Bytecount this_len = len; |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1615 Bytebpos this_pos = pos; |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1616 Ibyte *p; |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1617 if (pos <= lim) |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1618 goto stop; |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1619 p = base_pat + len; |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1620 |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1621 while (this_len > 0) |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1622 { |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1623 Ichar pat_ch, buf_ch; |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1624 |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1625 DEC_IBYTEPTR (p); |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1626 DEC_BYTEBPOS (buf, this_pos); |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1627 pat_ch = itext_ichar (p); |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1628 buf_ch = BYTE_BUF_FETCH_CHAR (buf, this_pos); |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1629 |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1630 buf_ch = TRANSLATE (trt, buf_ch); |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1631 |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1632 if (buf_ch != pat_ch) |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1633 break; |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1634 |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1635 this_len -= itext_ichar_len (p); |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1636 } |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1637 if (this_len == 0) |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1638 { |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1639 buf_len = pos - this_pos; |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1640 pos = this_pos; |
446 | 1641 break; |
4322
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1642 } |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1643 DEC_BYTEBPOS (buf, pos); |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1644 } |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1645 n++; |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1646 } |
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1647 } |
446 | 1648 stop: |
1649 if (n == 0) | |
1650 { | |
665 | 1651 Charbpos beg, end, retval; |
446 | 1652 if (forward) |
1653 { | |
826 | 1654 beg = bytebpos_to_charbpos (buf, pos - buf_len); |
1655 retval = end = bytebpos_to_charbpos (buf, pos); | |
446 | 1656 } |
1657 else | |
428 | 1658 { |
826 | 1659 retval = beg = bytebpos_to_charbpos (buf, pos); |
1660 end = bytebpos_to_charbpos (buf, pos + buf_len); | |
428 | 1661 } |
446 | 1662 set_search_regs (buf, beg, end - beg); |
1663 | |
1664 return retval; | |
1665 } | |
1666 else if (n > 0) | |
1667 return -n; | |
1668 else | |
1669 return n; | |
1670 } | |
1671 | |
1672 /* Do Boyer-Moore search N times for the string PAT, | |
1673 whose length is LEN/LEN_BYTE, | |
1674 from buffer position POS/POS_BYTE until LIM/LIM_BYTE. | |
1675 DIRECTION says which direction we search in. | |
1676 TRT and INVERSE_TRT are translation tables. | |
1677 | |
1678 This kind of search works if all the characters in PAT that have | |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1679 (non-ASCII) translation are the same aside from the last byte. This |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1680 makes it possible to translate just the last byte of a character, and do |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1681 so after just a simple test of the context. |
446 | 1682 |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1683 If that criterion is not satisfied, do not call this function. You will |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1684 get an assertion failure. */ |
446 | 1685 |
665 | 1686 static Charbpos |
867 | 1687 boyer_moore (struct buffer *buf, Ibyte *base_pat, Bytecount len, |
665 | 1688 Bytebpos pos, Bytebpos lim, EMACS_INT n, Lisp_Object trt, |
2333 | 1689 Lisp_Object inverse_trt, int USED_IF_MULE (charset_base)) |
446 | 1690 { |
1691 /* #### Someone really really really needs to comment the workings | |
1692 of this junk somewhat better. | |
1693 | |
1694 BTW "BM" stands for Boyer-Moore, which is one of the standard | |
1695 string-searching algorithms. It's the best string-searching | |
1696 algorithm out there, provided that: | |
1697 | |
1698 a) You're not fazed by algorithm complexity. (Rabin-Karp, which | |
1699 uses hashing, is much much easier to code but not as fast.) | |
1700 b) You can freely move backwards in the string that you're | |
1701 searching through. | |
1702 | |
1703 As the comment below tries to explain (but garbles in typical | |
1704 programmer-ese), the idea is that you don't have to do a | |
1705 string match at every successive position in the text. For | |
1706 example, let's say the pattern is "a very long string". We | |
1707 compare the last character in the string (`g') with the | |
1708 corresponding character in the text. If it mismatches, and | |
1709 it is, say, `z', then we can skip forward by the entire | |
1710 length of the pattern because `z' does not occur anywhere | |
1711 in the pattern. If the mismatching character does occur | |
1712 in the pattern, we can usually still skip forward by more | |
1713 than one: e.g. if it is `l', then we can skip forward | |
1714 by the length of the substring "ong string" -- i.e. the | |
1715 largest end section of the pattern that does not contain | |
1716 the mismatched character. So what we do is compute, for | |
1717 each possible character, the distance we can skip forward | |
1718 (the "stride") and use it in the string matching. This | |
1719 is what the BM_tab holds. */ | |
1720 REGISTER EMACS_INT *BM_tab; | |
1721 EMACS_INT *BM_tab_base; | |
1722 REGISTER Bytecount dirlen; | |
1723 EMACS_INT infinity; | |
665 | 1724 Bytebpos limit; |
446 | 1725 Bytecount stride_for_teases = 0; |
1726 REGISTER EMACS_INT i, j; | |
867 | 1727 Ibyte *pat, *pat_end; |
1728 REGISTER Ibyte *cursor, *p_limit, *ptr2; | |
1729 Ibyte simple_translate[0400]; | |
446 | 1730 REGISTER int direction = ((n > 0) ? 1 : -1); |
1731 #ifdef MULE | |
867 | 1732 Ibyte translate_prev_byte = 0; |
1733 Ibyte translate_anteprev_byte = 0; | |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1734 /* These need to be rethought in the event that the internal format |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1735 changes, or in the event that num_8_bit_fixed_chars disappears |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1736 (entirely_one_byte_p can be trivially worked out by checking is the |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1737 byte count equal to the char count.) */ |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1738 int buffer_entirely_one_byte_p = buf->text->entirely_one_byte_p; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1739 int buffer_nothing_greater_than_0xff = |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1740 buf->text->num_8_bit_fixed_chars == BUF_Z(buf) - BUF_BEG (buf); |
446 | 1741 #endif |
1742 #ifdef C_ALLOCA | |
1743 EMACS_INT BM_tab_space[0400]; | |
1744 BM_tab = &BM_tab_space[0]; | |
1745 #else | |
1746 BM_tab = alloca_array (EMACS_INT, 256); | |
1747 #endif | |
1748 | |
1749 /* The general approach is that we are going to maintain that we | |
1750 know the first (closest to the present position, in whatever | |
1751 direction we're searching) character that could possibly be | |
1752 the last (furthest from present position) character of a | |
1753 valid match. We advance the state of our knowledge by | |
1754 looking at that character and seeing whether it indeed | |
1755 matches the last character of the pattern. If it does, we | |
1756 take a closer look. If it does not, we move our pointer (to | |
1757 putative last characters) as far as is logically possible. | |
1758 This amount of movement, which I call a stride, will be the | |
1759 length of the pattern if the actual character appears nowhere | |
1760 in the pattern, otherwise it will be the distance from the | |
1761 last occurrence of that character to the end of the pattern. | |
1762 As a coding trick, an enormous stride is coded into the table | |
1763 for characters that match the last character. This allows | |
1764 use of only a single test, a test for having gone past the | |
1765 end of the permissible match region, to test for both | |
1766 possible matches (when the stride goes past the end | |
1767 immediately) and failure to match (where you get nudged past | |
1768 the end one stride at a time). | |
1769 | |
1770 Here we make a "mickey mouse" BM table. The stride of the | |
1771 search is determined only by the last character of the | |
1772 putative match. If that character does not match, we will | |
1773 stride the proper distance to propose a match that | |
1774 superimposes it on the last instance of a character that | |
1775 matches it (per trt), or misses it entirely if there is | |
1776 none. */ | |
1777 | |
1778 dirlen = len * direction; | |
1779 infinity = dirlen - (lim + pos + len + len) * direction; | |
1780 /* Record position after the end of the pattern. */ | |
1781 pat_end = base_pat + len; | |
1782 if (direction < 0) | |
1783 base_pat = pat_end - 1; | |
1784 BM_tab_base = BM_tab; | |
1785 BM_tab += 0400; | |
1786 j = dirlen; /* to get it in a register */ | |
1787 /* A character that does not appear in the pattern induces a | |
1788 stride equal to the pattern length. */ | |
1789 while (BM_tab_base != BM_tab) | |
1790 { | |
1791 *--BM_tab = j; | |
1792 *--BM_tab = j; | |
1793 *--BM_tab = j; | |
1794 *--BM_tab = j; | |
1795 } | |
1796 /* We use this for translation, instead of TRT itself. We | |
1797 fill this in to handle the characters that actually occur | |
1798 in the pattern. Others don't matter anyway! */ | |
1799 xzero (simple_translate); | |
1800 for (i = 0; i < 0400; i++) | |
867 | 1801 simple_translate[i] = (Ibyte) i; |
446 | 1802 i = 0; |
1425 | 1803 |
446 | 1804 while (i != infinity) |
1805 { | |
867 | 1806 Ibyte *ptr = base_pat + i; |
446 | 1807 i += direction; |
1808 if (i == dirlen) | |
1809 i = infinity; | |
1810 if (!NILP (trt)) | |
428 | 1811 { |
446 | 1812 #ifdef MULE |
4897
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1813 Ichar ch = -1, untranslated; |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1814 Ibyte byte; |
446 | 1815 int this_translated = 1; |
1816 | |
1817 /* Is *PTR the last byte of a character? */ | |
867 | 1818 if (pat_end - ptr == 1 || ibyte_first_byte_p (ptr[1])) |
428 | 1819 { |
867 | 1820 Ibyte *charstart = ptr; |
1821 while (!ibyte_first_byte_p (*charstart)) | |
446 | 1822 charstart--; |
867 | 1823 untranslated = itext_ichar (charstart); |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1824 |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1825 ch = TRANSLATE (trt, untranslated); |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1826 if (!ibyte_first_byte_p (*ptr)) |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1827 { |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1828 translate_prev_byte = ptr[-1]; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1829 if (!ibyte_first_byte_p (translate_prev_byte)) |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1830 translate_anteprev_byte = ptr[-2]; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1831 } |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1832 |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1833 if (ch != untranslated && /* Was translation done? */ |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1834 charset_base != (ch & ~ICHAR_FIELD3_MASK)) |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1835 { |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1836 /* In the very rare event that the CANON entry for this |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1837 character is not in the desired set, choose one that |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1838 is, from the equivalence set. It doesn't much matter |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1839 which, since we're building our own cheesy equivalence |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1840 table instead of using that belonging to the case |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1841 table directly. |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1842 |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1843 We can get here if search_buffer has worked out that |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1844 the buffer is entirely single width. */ |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1845 Ichar starting_ch = ch; |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1846 int count = 0; |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1847 do |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1848 { |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1849 ch = TRANSLATE (inverse_trt, ch); |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1850 if (charset_base == (ch & ~ICHAR_FIELD3_MASK)) |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1851 break; |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1852 ++count; |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1853 } while (starting_ch != ch); |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1854 |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1855 /* If starting_ch is equal to ch (and count is not one, |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1856 which means no translation is necessary), the case |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1857 table is corrupt. (Any mapping in the canon table |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1858 should be reflected in the equivalence table, and we |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1859 know from the canon table that untranslated maps to |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1860 starting_ch and that untranslated has the correct value |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1861 for charset_base.) */ |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1862 assert (1 == count || starting_ch != ch); |
446 | 1863 } |
4897
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1864 { |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1865 Ibyte tmp[MAX_ICHAR_LEN]; |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1866 Bytecount chlen; |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1867 |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1868 chlen = set_itext_ichar (tmp, ch); |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1869 byte = tmp[chlen - 1]; |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1870 } |
428 | 1871 } |
1872 else | |
1873 { | |
4897
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1874 byte = *ptr; |
446 | 1875 this_translated = 0; |
4897
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1876 ch = -1; |
446 | 1877 } |
4897
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1878 |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1879 /* BYTE = last byte of character CH when represented as text */ |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1880 j = byte; |
446 | 1881 |
1882 if (i == infinity) | |
1883 stride_for_teases = BM_tab[j]; | |
1884 BM_tab[j] = dirlen - i; | |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1885 /* A translation table is accompanied by its inverse -- see |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1886 comment in casetab.c. */ |
446 | 1887 if (this_translated) |
1888 { | |
867 | 1889 Ichar starting_ch = ch; |
446 | 1890 EMACS_INT starting_j = j; |
4897
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1891 |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1892 text_checking_assert (valid_ichar_p (ch)); |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1893 do |
446 | 1894 { |
1895 ch = TRANSLATE (inverse_trt, ch); | |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1896 |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1897 if (ch > 0x7F && buffer_entirely_one_byte_p) |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1898 continue; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1899 |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1900 if (ch > 0xFF && buffer_nothing_greater_than_0xff) |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1901 continue; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1902 |
4897
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1903 |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1904 /* Retrieve last byte of character CH when represented as |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1905 text */ |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1906 { |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1907 Ibyte tmp[MAX_ICHAR_LEN]; |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1908 Bytecount chlen; |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1909 |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1910 chlen = set_itext_ichar (tmp, ch); |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1911 j = tmp[chlen - 1]; |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1912 } |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1913 |
4407
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1914 /* For all the characters that map into CH, set up |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1915 simple_translate to map the last byte into |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1916 STARTING_J. */ |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1917 simple_translate[j] = (Ibyte) starting_j; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1918 BM_tab[j] = dirlen - i; |
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1919 |
4897
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1920 } |
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1921 while (ch != starting_ch); |
446 | 1922 } |
4897
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1923 #else /* not MULE */ |
446 | 1924 EMACS_INT k; |
1925 j = *ptr; | |
1926 k = (j = TRANSLATE (trt, j)); | |
1927 if (i == infinity) | |
1928 stride_for_teases = BM_tab[j]; | |
1929 BM_tab[j] = dirlen - i; | |
1930 /* A translation table is accompanied by its inverse -- | |
826 | 1931 see comment in casetab.c. */ |
446 | 1932 while ((j = TRANSLATE (inverse_trt, j)) != k) |
1933 { | |
867 | 1934 simple_translate[j] = (Ibyte) k; |
428 | 1935 BM_tab[j] = dirlen - i; |
1936 } | |
4897
91a023144e72
fix longstanding search bug involving searching for Control-1 chars
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
1937 #endif /* (not) MULE */ |
446 | 1938 } |
1939 else | |
1940 { | |
1941 j = *ptr; | |
1942 | |
1943 if (i == infinity) | |
1944 stride_for_teases = BM_tab[j]; | |
1945 BM_tab[j] = dirlen - i; | |
428 | 1946 } |
446 | 1947 /* stride_for_teases tells how much to stride if we get a |
1948 match on the far character but are subsequently | |
1949 disappointed, by recording what the stride would have been | |
1950 for that character if the last character had been | |
1951 different. */ | |
1952 } | |
1953 infinity = dirlen - infinity; | |
1954 pos += dirlen - ((direction > 0) ? direction : 0); | |
1955 /* loop invariant - pos points at where last char (first char if | |
1956 reverse) of pattern would align in a possible match. */ | |
1957 while (n != 0) | |
1958 { | |
665 | 1959 Bytebpos tail_end; |
867 | 1960 Ibyte *tail_end_ptr; |
446 | 1961 /* It's been reported that some (broken) compiler thinks |
1962 that Boolean expressions in an arithmetic context are | |
1963 unsigned. Using an explicit ?1:0 prevents this. */ | |
1964 if ((lim - pos - ((direction > 0) ? 1 : 0)) * direction < 0) | |
1965 return n * (0 - direction); | |
1966 /* First we do the part we can by pointers (maybe | |
1967 nothing) */ | |
1968 QUIT; | |
1969 pat = base_pat; | |
1970 limit = pos - dirlen + direction; | |
1971 /* XEmacs change: definitions of CEILING_OF and FLOOR_OF | |
1972 have changed. See buffer.h. */ | |
1973 limit = ((direction > 0) | |
826 | 1974 ? BYTE_BUF_CEILING_OF (buf, limit) - 1 |
1975 : BYTE_BUF_FLOOR_OF (buf, limit + 1)); | |
446 | 1976 /* LIMIT is now the last (not beyond-last!) value POS can |
1977 take on without hitting edge of buffer or the gap. */ | |
1978 limit = ((direction > 0) | |
1979 ? min (lim - 1, min (limit, pos + 20000)) | |
1980 : max (lim, max (limit, pos - 20000))); | |
826 | 1981 tail_end = BYTE_BUF_CEILING_OF (buf, pos); |
1982 tail_end_ptr = BYTE_BUF_BYTE_ADDRESS (buf, tail_end); | |
446 | 1983 |
1984 if ((limit - pos) * direction > 20) | |
428 | 1985 { |
826 | 1986 /* We have to be careful because the code can generate addresses |
1987 that don't point to the beginning of characters. */ | |
1988 p_limit = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, limit); | |
1989 ptr2 = (cursor = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos)); | |
446 | 1990 /* In this loop, pos + cursor - ptr2 is the surrogate |
1991 for pos */ | |
1992 while (1) /* use one cursor setting as long as i can */ | |
1993 { | |
1994 if (direction > 0) /* worth duplicating */ | |
1995 { | |
1996 /* Use signed comparison if appropriate to make | |
1997 cursor+infinity sure to be > p_limit. | |
1998 Assuming that the buffer lies in a range of | |
1999 addresses that are all "positive" (as ints) | |
2000 or all "negative", either kind of comparison | |
2001 will work as long as we don't step by | |
2002 infinity. So pick the kind that works when | |
2003 we do step by infinity. */ | |
2004 if ((EMACS_INT) (p_limit + infinity) > | |
2005 (EMACS_INT) p_limit) | |
2006 while ((EMACS_INT) cursor <= | |
2007 (EMACS_INT) p_limit) | |
2008 cursor += BM_tab[*cursor]; | |
2009 else | |
2010 while ((EMACS_UINT) cursor <= | |
2011 (EMACS_UINT) p_limit) | |
2012 cursor += BM_tab[*cursor]; | |
2013 } | |
2014 else | |
2015 { | |
2016 if ((EMACS_INT) (p_limit + infinity) < | |
2017 (EMACS_INT) p_limit) | |
2018 while ((EMACS_INT) cursor >= | |
2019 (EMACS_INT) p_limit) | |
2020 cursor += BM_tab[*cursor]; | |
2021 else | |
2022 while ((EMACS_UINT) cursor >= | |
2023 (EMACS_UINT) p_limit) | |
2024 cursor += BM_tab[*cursor]; | |
2025 } | |
2026 /* If you are here, cursor is beyond the end of the | |
2027 searched region. This can happen if you match on | |
2028 the far character of the pattern, because the | |
2029 "stride" of that character is infinity, a number | |
2030 able to throw you well beyond the end of the | |
2031 search. It can also happen if you fail to match | |
2032 within the permitted region and would otherwise | |
2033 try a character beyond that region */ | |
2034 if ((cursor - p_limit) * direction <= len) | |
2035 break; /* a small overrun is genuine */ | |
2036 cursor -= infinity; /* large overrun = hit */ | |
2037 i = dirlen - direction; | |
2038 if (!NILP (trt)) | |
2039 { | |
2040 while ((i -= direction) + direction != 0) | |
2041 { | |
2042 #ifdef MULE | |
867 | 2043 Ichar ch; |
446 | 2044 cursor -= direction; |
2045 /* Translate only the last byte of a character. */ | |
2046 if ((cursor == tail_end_ptr | |
867 | 2047 || ibyte_first_byte_p (cursor[1])) |
2048 && (ibyte_first_byte_p (cursor[0]) | |
446 | 2049 || (translate_prev_byte == cursor[-1] |
867 | 2050 && (ibyte_first_byte_p (translate_prev_byte) |
446 | 2051 || translate_anteprev_byte == cursor[-2])))) |
2052 ch = simple_translate[*cursor]; | |
2053 else | |
2054 ch = *cursor; | |
2055 if (pat[i] != ch) | |
2056 break; | |
2057 #else | |
2058 if (pat[i] != TRANSLATE (trt, *(cursor -= direction))) | |
2059 break; | |
2060 #endif | |
2061 } | |
2062 } | |
2063 else | |
2064 { | |
2065 while ((i -= direction) + direction != 0) | |
2066 if (pat[i] != *(cursor -= direction)) | |
2067 break; | |
2068 } | |
2069 cursor += dirlen - i - direction; /* fix cursor */ | |
2070 if (i + direction == 0) | |
2071 { | |
2072 cursor -= direction; | |
2073 | |
2074 { | |
665 | 2075 Bytebpos bytstart = (pos + cursor - ptr2 + |
446 | 2076 ((direction > 0) |
2077 ? 1 - len : 0)); | |
665 | 2078 Charbpos bufstart = bytebpos_to_charbpos (buf, bytstart); |
2079 Charbpos bufend = bytebpos_to_charbpos (buf, bytstart + len); | |
446 | 2080 |
2081 set_search_regs (buf, bufstart, bufend - bufstart); | |
2082 } | |
2083 | |
2084 if ((n -= direction) != 0) | |
2085 cursor += dirlen; /* to resume search */ | |
2086 else | |
2087 return ((direction > 0) | |
2088 ? search_regs.end[0] : search_regs.start[0]); | |
2089 } | |
2090 else | |
2091 cursor += stride_for_teases; /* <sigh> we lose - */ | |
2092 } | |
2093 pos += cursor - ptr2; | |
2094 } | |
2095 else | |
2096 /* Now we'll pick up a clump that has to be done the hard | |
2097 way because it covers a discontinuity */ | |
2098 { | |
428 | 2099 /* XEmacs change: definitions of CEILING_OF and FLOOR_OF |
2100 have changed. See buffer.h. */ | |
2101 limit = ((direction > 0) | |
826 | 2102 ? BYTE_BUF_CEILING_OF (buf, pos - dirlen + 1) - 1 |
2103 : BYTE_BUF_FLOOR_OF (buf, pos - dirlen)); | |
428 | 2104 limit = ((direction > 0) |
446 | 2105 ? min (limit + len, lim - 1) |
2106 : max (limit - len, lim)); | |
2107 /* LIMIT is now the last value POS can have | |
2108 and still be valid for a possible match. */ | |
2109 while (1) | |
428 | 2110 { |
446 | 2111 /* This loop can be coded for space rather than |
2112 speed because it will usually run only once. | |
2113 (the reach is at most len + 21, and typically | |
2114 does not exceed len) */ | |
2115 while ((limit - pos) * direction >= 0) | |
826 | 2116 /* *not* BYTE_BUF_FETCH_CHAR. We are working here |
446 | 2117 with bytes, not characters. */ |
826 | 2118 pos += BM_tab[*BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos)]; |
446 | 2119 /* now run the same tests to distinguish going off |
2120 the end, a match or a phony match. */ | |
2121 if ((pos - limit) * direction <= len) | |
2122 break; /* ran off the end */ | |
2123 /* Found what might be a match. | |
2124 Set POS back to last (first if reverse) char pos. */ | |
2125 pos -= infinity; | |
2126 i = dirlen - direction; | |
2127 while ((i -= direction) + direction != 0) | |
428 | 2128 { |
446 | 2129 #ifdef MULE |
867 | 2130 Ichar ch; |
2131 Ibyte *ptr; | |
446 | 2132 #endif |
2133 pos -= direction; | |
2134 #ifdef MULE | |
826 | 2135 ptr = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos); |
446 | 2136 if ((ptr == tail_end_ptr |
867 | 2137 || ibyte_first_byte_p (ptr[1])) |
2138 && (ibyte_first_byte_p (ptr[0]) | |
446 | 2139 || (translate_prev_byte == ptr[-1] |
867 | 2140 && (ibyte_first_byte_p (translate_prev_byte) |
446 | 2141 || translate_anteprev_byte == ptr[-2])))) |
2142 ch = simple_translate[*ptr]; | |
428 | 2143 else |
446 | 2144 ch = *ptr; |
2145 if (pat[i] != ch) | |
2146 break; | |
2147 | |
2148 #else | |
826 | 2149 if (pat[i] != |
2150 TRANSLATE (trt, | |
2151 *BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos))) | |
446 | 2152 break; |
2153 #endif | |
428 | 2154 } |
446 | 2155 /* Above loop has moved POS part or all the way back |
2156 to the first char pos (last char pos if reverse). | |
2157 Set it once again at the last (first if reverse) | |
2158 char. */ | |
2159 pos += dirlen - i- direction; | |
2160 if (i + direction == 0) | |
428 | 2161 { |
446 | 2162 pos -= direction; |
2163 | |
2164 { | |
665 | 2165 Bytebpos bytstart = (pos + |
446 | 2166 ((direction > 0) |
2167 ? 1 - len : 0)); | |
665 | 2168 Charbpos bufstart = bytebpos_to_charbpos (buf, bytstart); |
2169 Charbpos bufend = bytebpos_to_charbpos (buf, bytstart + len); | |
446 | 2170 |
2171 set_search_regs (buf, bufstart, bufend - bufstart); | |
2172 } | |
2173 | |
2174 if ((n -= direction) != 0) | |
2175 pos += dirlen; /* to resume search */ | |
428 | 2176 else |
446 | 2177 return ((direction > 0) |
2178 ? search_regs.end[0] : search_regs.start[0]); | |
428 | 2179 } |
446 | 2180 else |
2181 pos += stride_for_teases; | |
2182 } | |
428 | 2183 } |
446 | 2184 /* We have done one clump. Can we continue? */ |
2185 if ((lim - pos) * direction < 0) | |
2186 return (0 - n) * direction; | |
428 | 2187 } |
665 | 2188 return bytebpos_to_charbpos (buf, pos); |
428 | 2189 } |
2190 | |
1024 | 2191 /* Record the whole-match data (beginning BEG and end BEG + LEN) and the |
2192 buffer for a match just found. */ | |
428 | 2193 |
2194 static void | |
665 | 2195 set_search_regs (struct buffer *buf, Charbpos beg, Charcount len) |
428 | 2196 { |
2197 /* Make sure we have registers in which to store | |
2198 the match position. */ | |
2199 if (search_regs.num_regs == 0) | |
2200 { | |
2201 search_regs.start = xnew (regoff_t); | |
2202 search_regs.end = xnew (regoff_t); | |
2203 search_regs.num_regs = 1; | |
2204 } | |
2205 | |
1468 | 2206 clear_search_regs (); |
428 | 2207 search_regs.start[0] = beg; |
2208 search_regs.end[0] = beg + len; | |
793 | 2209 last_thing_searched = wrap_buffer (buf); |
428 | 2210 } |
2211 | |
1468 | 2212 /* Clear search registers so match data will be null. */ |
1024 | 2213 |
2214 static void | |
1468 | 2215 clear_search_regs (void) |
1024 | 2216 { |
2217 /* This function has been Mule-ized. */ | |
2218 int i; | |
2219 | |
1468 | 2220 for (i = 0; i < search_regs.num_regs; i++) |
2221 search_regs.start[i] = search_regs.end[i] = -1; | |
1024 | 2222 } |
2223 | |
428 | 2224 |
2225 /* Given a string of words separated by word delimiters, | |
442 | 2226 compute a regexp that matches those exact words |
2227 separated by arbitrary punctuation. */ | |
428 | 2228 |
2229 static Lisp_Object | |
2230 wordify (Lisp_Object buffer, Lisp_Object string) | |
2231 { | |
2232 Charcount i, len; | |
2233 EMACS_INT punct_count = 0, word_count = 0; | |
2234 struct buffer *buf = decode_buffer (buffer, 0); | |
826 | 2235 Lisp_Object syntax_table = buf->mirror_syntax_table; |
428 | 2236 |
2237 CHECK_STRING (string); | |
826 | 2238 len = string_char_length (string); |
428 | 2239 |
2240 for (i = 0; i < len; i++) | |
867 | 2241 if (!WORD_SYNTAX_P (syntax_table, string_ichar (string, i))) |
428 | 2242 { |
2243 punct_count++; | |
2244 if (i > 0 && WORD_SYNTAX_P (syntax_table, | |
867 | 2245 string_ichar (string, i - 1))) |
428 | 2246 word_count++; |
2247 } | |
867 | 2248 if (WORD_SYNTAX_P (syntax_table, string_ichar (string, len - 1))) |
428 | 2249 word_count++; |
4952
19a72041c5ed
Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
2250 if (!word_count) return build_ascstring (""); |
428 | 2251 |
2252 { | |
2253 /* The following value is an upper bound on the amount of storage we | |
2254 need. In non-Mule, it is exact. */ | |
867 | 2255 Ibyte *storage = |
2367 | 2256 alloca_ibytes (XSTRING_LENGTH (string) - punct_count + |
428 | 2257 5 * (word_count - 1) + 4); |
867 | 2258 Ibyte *o = storage; |
428 | 2259 |
2260 *o++ = '\\'; | |
2261 *o++ = 'b'; | |
2262 | |
2263 for (i = 0; i < len; i++) | |
2264 { | |
867 | 2265 Ichar ch = string_ichar (string, i); |
428 | 2266 |
2267 if (WORD_SYNTAX_P (syntax_table, ch)) | |
867 | 2268 o += set_itext_ichar (o, ch); |
428 | 2269 else if (i > 0 |
2270 && WORD_SYNTAX_P (syntax_table, | |
867 | 2271 string_ichar (string, i - 1)) |
428 | 2272 && --word_count) |
2273 { | |
2274 *o++ = '\\'; | |
2275 *o++ = 'W'; | |
2276 *o++ = '\\'; | |
2277 *o++ = 'W'; | |
2278 *o++ = '*'; | |
2279 } | |
2280 } | |
2281 | |
2282 *o++ = '\\'; | |
2283 *o++ = 'b'; | |
2284 | |
2285 return make_string (storage, o - storage); | |
2286 } | |
2287 } | |
2288 | |
2289 DEFUN ("search-backward", Fsearch_backward, 1, 5, "sSearch backward: ", /* | |
2290 Search backward from point for STRING. | |
2291 Set point to the beginning of the occurrence found, and return point. | |
444 | 2292 |
2293 Optional second argument LIMIT bounds the search; it is a buffer | |
2294 position. The match found must not extend before that position. | |
2295 The value nil is equivalent to (point-min). | |
2296 | |
2297 Optional third argument NOERROR, if t, means just return nil (no | |
2298 error) if the search fails. If neither nil nor t, set point to LIMIT | |
2299 and return nil. | |
2300 | |
2301 Optional fourth argument COUNT is a repeat count--search for | |
2302 successive occurrences. | |
2303 | |
428 | 2304 Optional fifth argument BUFFER specifies the buffer to search in and |
444 | 2305 defaults to the current buffer. |
2306 | |
1468 | 2307 When the match is successful, this function modifies the match data |
2308 that `match-beginning', `match-end' and `match-data' access; save the | |
2309 match data with `match-data' and restore it with `store-match-data' if | |
2310 you want to preserve them. If the match fails, the match data from the | |
2311 previous success match is preserved. | |
2312 | |
2313 See also the function `replace-match'. | |
428 | 2314 */ |
444 | 2315 (string, limit, noerror, count, buffer)) |
428 | 2316 { |
444 | 2317 return search_command (string, limit, noerror, count, buffer, -1, 0, 0); |
428 | 2318 } |
2319 | |
2320 DEFUN ("search-forward", Fsearch_forward, 1, 5, "sSearch: ", /* | |
2321 Search forward from point for STRING. | |
2322 Set point to the end of the occurrence found, and return point. | |
444 | 2323 |
2324 Optional second argument LIMIT bounds the search; it is a buffer | |
2325 position. The match found must not extend after that position. The | |
2326 value nil is equivalent to (point-max). | |
2327 | |
2328 Optional third argument NOERROR, if t, means just return nil (no | |
2329 error) if the search fails. If neither nil nor t, set point to LIMIT | |
2330 and return nil. | |
2331 | |
2332 Optional fourth argument COUNT is a repeat count--search for | |
2333 successive occurrences. | |
2334 | |
428 | 2335 Optional fifth argument BUFFER specifies the buffer to search in and |
444 | 2336 defaults to the current buffer. |
2337 | |
1468 | 2338 When the match is successful, this function modifies the match data |
2339 that `match-beginning', `match-end' and `match-data' access; save the | |
2340 match data with `match-data' and restore it with `store-match-data' if | |
2341 you want to preserve them. If the match fails, the match data from the | |
2342 previous success match is preserved. | |
2343 | |
2344 See also the function `replace-match'. | |
428 | 2345 */ |
444 | 2346 (string, limit, noerror, count, buffer)) |
428 | 2347 { |
444 | 2348 return search_command (string, limit, noerror, count, buffer, 1, 0, 0); |
428 | 2349 } |
2350 | |
2351 DEFUN ("word-search-backward", Fword_search_backward, 1, 5, | |
2352 "sWord search backward: ", /* | |
2353 Search backward from point for STRING, ignoring differences in punctuation. | |
2354 Set point to the beginning of the occurrence found, and return point. | |
444 | 2355 |
2356 Optional second argument LIMIT bounds the search; it is a buffer | |
2357 position. The match found must not extend before that position. | |
2358 The value nil is equivalent to (point-min). | |
2359 | |
2360 Optional third argument NOERROR, if t, means just return nil (no | |
2361 error) if the search fails. If neither nil nor t, set point to LIMIT | |
2362 and return nil. | |
2363 | |
2364 Optional fourth argument COUNT is a repeat count--search for | |
2365 successive occurrences. | |
2366 | |
428 | 2367 Optional fifth argument BUFFER specifies the buffer to search in and |
444 | 2368 defaults to the current buffer. |
2369 | |
1468 | 2370 When the match is successful, this function modifies the match data |
2371 that `match-beginning', `match-end' and `match-data' access; save the | |
2372 match data with `match-data' and restore it with `store-match-data' if | |
2373 you want to preserve them. If the match fails, the match data from the | |
2374 previous success match is preserved. | |
2375 | |
2376 See also the function `replace-match'. | |
428 | 2377 */ |
444 | 2378 (string, limit, noerror, count, buffer)) |
428 | 2379 { |
444 | 2380 return search_command (wordify (buffer, string), limit, noerror, count, |
428 | 2381 buffer, -1, 1, 0); |
2382 } | |
2383 | |
2384 DEFUN ("word-search-forward", Fword_search_forward, 1, 5, "sWord search: ", /* | |
2385 Search forward from point for STRING, ignoring differences in punctuation. | |
2386 Set point to the end of the occurrence found, and return point. | |
444 | 2387 |
2388 Optional second argument LIMIT bounds the search; it is a buffer | |
2389 position. The match found must not extend after that position. The | |
2390 value nil is equivalent to (point-max). | |
2391 | |
2392 Optional third argument NOERROR, if t, means just return nil (no | |
2393 error) if the search fails. If neither nil nor t, set point to LIMIT | |
2394 and return nil. | |
2395 | |
2396 Optional fourth argument COUNT is a repeat count--search for | |
2397 successive occurrences. | |
2398 | |
428 | 2399 Optional fifth argument BUFFER specifies the buffer to search in and |
444 | 2400 defaults to the current buffer. |
2401 | |
1468 | 2402 When the match is successful, this function modifies the match data |
2403 that `match-beginning', `match-end' and `match-data' access; save the | |
2404 match data with `match-data' and restore it with `store-match-data' if | |
2405 you want to preserve them. If the match fails, the match data from the | |
2406 previous success match is preserved. | |
2407 | |
2408 See also the function `replace-match'. | |
428 | 2409 */ |
444 | 2410 (string, limit, noerror, count, buffer)) |
428 | 2411 { |
444 | 2412 return search_command (wordify (buffer, string), limit, noerror, count, |
428 | 2413 buffer, 1, 1, 0); |
2414 } | |
2415 | |
2416 DEFUN ("re-search-backward", Fre_search_backward, 1, 5, | |
2417 "sRE search backward: ", /* | |
2418 Search backward from point for match for regular expression REGEXP. | |
2419 Set point to the beginning of the match, and return point. | |
2420 The match found is the one starting last in the buffer | |
2421 and yet ending before the origin of the search. | |
444 | 2422 |
2423 Optional second argument LIMIT bounds the search; it is a buffer | |
2424 position. The match found must not extend before that position. | |
2425 The value nil is equivalent to (point-min). | |
2426 | |
2427 Optional third argument NOERROR, if t, means just return nil (no | |
2428 error) if the search fails. If neither nil nor t, set point to LIMIT | |
2429 and return nil. | |
2430 | |
2431 Optional fourth argument COUNT is a repeat count--search for | |
2432 successive occurrences. | |
2433 | |
428 | 2434 Optional fifth argument BUFFER specifies the buffer to search in and |
444 | 2435 defaults to the current buffer. |
2436 | |
1468 | 2437 When the match is successful, this function modifies the match data |
2438 that `match-beginning', `match-end' and `match-data' access; save the | |
2439 match data with `match-data' and restore it with `store-match-data' if | |
2440 you want to preserve them. If the match fails, the match data from the | |
2441 previous success match is preserved. | |
2442 | |
2443 See also the function `replace-match'. | |
428 | 2444 */ |
444 | 2445 (regexp, limit, noerror, count, buffer)) |
428 | 2446 { |
444 | 2447 return search_command (regexp, limit, noerror, count, buffer, -1, 1, 0); |
428 | 2448 } |
2449 | |
2450 DEFUN ("re-search-forward", Fre_search_forward, 1, 5, "sRE search: ", /* | |
2451 Search forward from point for regular expression REGEXP. | |
2452 Set point to the end of the occurrence found, and return point. | |
444 | 2453 |
2454 Optional second argument LIMIT bounds the search; it is a buffer | |
2455 position. The match found must not extend after that position. The | |
2456 value nil is equivalent to (point-max). | |
2457 | |
2458 Optional third argument NOERROR, if t, means just return nil (no | |
2459 error) if the search fails. If neither nil nor t, set point to LIMIT | |
2460 and return nil. | |
2461 | |
2462 Optional fourth argument COUNT is a repeat count--search for | |
2463 successive occurrences. | |
2464 | |
428 | 2465 Optional fifth argument BUFFER specifies the buffer to search in and |
444 | 2466 defaults to the current buffer. |
2467 | |
1468 | 2468 When the match is successful, this function modifies the match data |
2469 that `match-beginning', `match-end' and `match-data' access; save the | |
2470 match data with `match-data' and restore it with `store-match-data' if | |
2471 you want to preserve them. If the match fails, the match data from the | |
2472 previous success match is preserved. | |
2473 | |
2474 See also the function `replace-match'. | |
428 | 2475 */ |
444 | 2476 (regexp, limit, noerror, count, buffer)) |
428 | 2477 { |
444 | 2478 return search_command (regexp, limit, noerror, count, buffer, 1, 1, 0); |
428 | 2479 } |
2480 | |
2481 DEFUN ("posix-search-backward", Fposix_search_backward, 1, 5, | |
2482 "sPosix search backward: ", /* | |
2483 Search backward from point for match for regular expression REGEXP. | |
2484 Find the longest match in accord with Posix regular expression rules. | |
2485 Set point to the beginning of the match, and return point. | |
2486 The match found is the one starting last in the buffer | |
2487 and yet ending before the origin of the search. | |
444 | 2488 |
2489 Optional second argument LIMIT bounds the search; it is a buffer | |
2490 position. The match found must not extend before that position. | |
2491 The value nil is equivalent to (point-min). | |
2492 | |
2493 Optional third argument NOERROR, if t, means just return nil (no | |
2494 error) if the search fails. If neither nil nor t, set point to LIMIT | |
2495 and return nil. | |
2496 | |
2497 Optional fourth argument COUNT is a repeat count--search for | |
2498 successive occurrences. | |
2499 | |
428 | 2500 Optional fifth argument BUFFER specifies the buffer to search in and |
444 | 2501 defaults to the current buffer. |
2502 | |
1468 | 2503 When the match is successful, this function modifies the match data |
2504 that `match-beginning', `match-end' and `match-data' access; save the | |
2505 match data with `match-data' and restore it with `store-match-data' if | |
2506 you want to preserve them. If the match fails, the match data from the | |
2507 previous success match is preserved. | |
2508 | |
2509 See also the function `replace-match'. | |
428 | 2510 */ |
444 | 2511 (regexp, limit, noerror, count, buffer)) |
428 | 2512 { |
444 | 2513 return search_command (regexp, limit, noerror, count, buffer, -1, 1, 1); |
428 | 2514 } |
2515 | |
2516 DEFUN ("posix-search-forward", Fposix_search_forward, 1, 5, "sPosix search: ", /* | |
2517 Search forward from point for regular expression REGEXP. | |
2518 Find the longest match in accord with Posix regular expression rules. | |
2519 Set point to the end of the occurrence found, and return point. | |
444 | 2520 |
2521 Optional second argument LIMIT bounds the search; it is a buffer | |
2522 position. The match found must not extend after that position. The | |
2523 value nil is equivalent to (point-max). | |
2524 | |
2525 Optional third argument NOERROR, if t, means just return nil (no | |
2526 error) if the search fails. If neither nil nor t, set point to LIMIT | |
2527 and return nil. | |
2528 | |
2529 Optional fourth argument COUNT is a repeat count--search for | |
2530 successive occurrences. | |
2531 | |
428 | 2532 Optional fifth argument BUFFER specifies the buffer to search in and |
444 | 2533 defaults to the current buffer. |
2534 | |
1468 | 2535 When the match is successful, this function modifies the match data |
2536 that `match-beginning', `match-end' and `match-data' access; save the | |
2537 match data with `match-data' and restore it with `store-match-data' if | |
2538 you want to preserve them. If the match fails, the match data from the | |
2539 previous success match is preserved. | |
2540 | |
2541 See also the function `replace-match'. | |
428 | 2542 */ |
444 | 2543 (regexp, limit, noerror, count, buffer)) |
428 | 2544 { |
444 | 2545 return search_command (regexp, limit, noerror, count, buffer, 1, 1, 1); |
428 | 2546 } |
2547 | |
2548 | |
2549 static Lisp_Object | |
2550 free_created_dynarrs (Lisp_Object cons) | |
2551 { | |
2552 Dynarr_free (get_opaque_ptr (XCAR (cons))); | |
2553 Dynarr_free (get_opaque_ptr (XCDR (cons))); | |
2554 free_opaque_ptr (XCAR (cons)); | |
2555 free_opaque_ptr (XCDR (cons)); | |
853 | 2556 free_cons (cons); |
428 | 2557 return Qnil; |
2558 } | |
2559 | |
2560 DEFUN ("replace-match", Freplace_match, 1, 5, 0, /* | |
444 | 2561 Replace text matched by last search with REPLACEMENT. |
4199 | 2562 Leaves point at end of replacement text. |
2563 Optional boolean FIXEDCASE inhibits matching case of REPLACEMENT to source. | |
2564 Optional boolean LITERAL inhibits interpretation of escape sequences. | |
2565 Optional STRING provides the source text to replace. | |
2566 Optional STRBUFFER may be a buffer, providing match context, or an integer | |
2567 specifying the subexpression to replace. | |
2568 | |
2569 If FIXEDCASE is non-nil, do not alter case of replacement text. | |
428 | 2570 Otherwise maybe capitalize the whole text, or maybe just word initials, |
2571 based on the replaced text. | |
4199 | 2572 If the replaced text has only capital letters and has at least one |
2573 multiletter word, convert REPLACEMENT to all caps. | |
428 | 2574 If the replaced text has at least one word starting with a capital letter, |
444 | 2575 then capitalize each word in REPLACEMENT. |
428 | 2576 |
4199 | 2577 If LITERAL is non-nil, insert REPLACEMENT literally. |
428 | 2578 Otherwise treat `\\' as special: |
444 | 2579 `\\&' in REPLACEMENT means substitute original matched text. |
428 | 2580 `\\N' means substitute what matched the Nth `\\(...\\)'. |
2581 If Nth parens didn't match, substitute nothing. | |
2582 `\\\\' means insert one `\\'. | |
2583 `\\u' means upcase the next character. | |
2584 `\\l' means downcase the next character. | |
2585 `\\U' means begin upcasing all following characters. | |
2586 `\\L' means begin downcasing all following characters. | |
2587 `\\E' means terminate the effect of any `\\U' or `\\L'. | |
2588 Case changes made with `\\u', `\\l', `\\U', and `\\L' override | |
2589 all other case changes that may be made in the replaced text. | |
4199 | 2590 |
2591 If non-nil, STRING is the source string, and a new string with the specified | |
2592 replacements is created and returned. Otherwise the current buffer is the | |
2593 source text. | |
2594 | |
2595 If non-nil, STRBUFFER may be an integer, interpreted as the index of the | |
2596 subexpression to replace in the source text, or a buffer to provide the | |
2597 syntax table and case table. If nil, then the \"subexpression\" is 0, i.e., | |
2598 the whole match, and the current buffer provides the syntax and case tables. | |
2599 If STRING is nil, STRBUFFER must be nil or an integer. | |
2600 | |
2601 Specifying a subexpression is only useful after a regular expression match, | |
2602 since a fixed string search has no non-trivial subexpressions. | |
2603 | |
2604 It is not possible to specify both a buffer and a subexpression. If that is | |
2605 desired, the idiom `(with-current-buffer BUFFER (replace-match ... INTEGER))' | |
2606 may be appropriate. | |
2607 | |
2608 If STRING is nil but the last thing matched (or searched) was a string, or | |
2609 STRING is a string but the last thing matched was a buffer, an | |
2610 `invalid-argument' error will be signaled. (XEmacs does not check that the | |
2611 last thing searched is the source string, but it is not useful to use a | |
2612 different string as source.) | |
2613 | |
2614 If no match (including searches) has been successful or the requested | |
1468 | 2615 subexpression was not matched, an `args-out-of-range' error will be |
2616 signaled. (If no match has ever been conducted in this instance of | |
2617 XEmacs, an `invalid-operation' error will be signaled. This is very | |
2618 rare.) | |
428 | 2619 */ |
444 | 2620 (replacement, fixedcase, literal, string, strbuffer)) |
428 | 2621 { |
2622 /* This function can GC */ | |
2623 enum { nochange, all_caps, cap_initial } case_action; | |
665 | 2624 Charbpos pos, last; |
428 | 2625 int some_multiletter_word; |
2626 int some_lowercase; | |
2627 int some_uppercase; | |
2628 int some_nonuppercase_initial; | |
867 | 2629 Ichar c, prevc; |
428 | 2630 Charcount inslen; |
2631 struct buffer *buf; | |
826 | 2632 Lisp_Object syntax_table; |
428 | 2633 int mc_count; |
2634 Lisp_Object buffer; | |
2635 int_dynarr *ul_action_dynarr = 0; | |
2636 int_dynarr *ul_pos_dynarr = 0; | |
502 | 2637 int sub = 0; |
428 | 2638 int speccount; |
2639 | |
444 | 2640 CHECK_STRING (replacement); |
428 | 2641 |
4199 | 2642 /* Because GNU decided to be incompatible here, we support the following |
2643 baroque and bogus API for the STRING and STRBUFFER arguments: | |
2644 types interpretations | |
2645 STRING STRBUFFER STRING STRBUFFER | |
2646 nil nil none 0 = index of subexpression to replace | |
2647 nil integer none index of subexpression to replace | |
2648 nil other ***** error ***** | |
2649 string nil source current buffer provides syntax table | |
2650 subexpression = 0 (whole match) | |
2651 string buffer source buffer providing syntax table | |
2652 subexpression = 0 (whole match) | |
2653 string integer source current buffer provides syntax table | |
2654 subexpression = STRBUFFER | |
2655 string other ***** error ***** | |
2656 */ | |
2657 | |
2658 /* Do STRBUFFER first; if STRING is nil, we'll overwrite BUF and BUFFER. */ | |
2659 | |
2660 /* If the match data were abstracted into a special "match data" type | |
2661 instead of the typical half-assed "let the implementation be visible" | |
2662 form it's in, we could extend it to include the last string matched | |
2663 and the buffer used for that matching. But of course we can't change | |
2664 it as it is. | |
2665 */ | |
2666 if (NILP (strbuffer) || BUFFERP (strbuffer)) | |
2667 { | |
2668 buf = decode_buffer (strbuffer, 0); | |
2669 } | |
2670 else if (!NILP (strbuffer)) | |
2671 { | |
2672 CHECK_INT (strbuffer); | |
2673 sub = XINT (strbuffer); | |
2674 if (sub < 0 || sub >= (int) search_regs.num_regs) | |
2675 invalid_argument ("match data register invalid", strbuffer); | |
2676 if (search_regs.start[sub] < 0) | |
2677 invalid_argument ("match data register not set", strbuffer); | |
2678 buf = current_buffer; | |
2679 } | |
2680 else | |
2681 invalid_argument ("STRBUFFER must be nil, a buffer, or an integer", | |
2682 strbuffer); | |
2683 buffer = wrap_buffer (buf); | |
2684 | |
428 | 2685 if (! NILP (string)) |
2686 { | |
2687 CHECK_STRING (string); | |
2688 if (!EQ (last_thing_searched, Qt)) | |
4199 | 2689 invalid_argument ("last thing matched was not a string", Qunbound); |
428 | 2690 } |
2691 else | |
2692 { | |
2693 if (!BUFFERP (last_thing_searched)) | |
4199 | 2694 invalid_argument ("last thing matched was not a buffer", Qunbound); |
428 | 2695 buffer = last_thing_searched; |
2696 buf = XBUFFER (buffer); | |
2697 } | |
2698 | |
826 | 2699 syntax_table = buf->mirror_syntax_table; |
428 | 2700 |
2701 case_action = nochange; /* We tried an initialization */ | |
2702 /* but some C compilers blew it */ | |
2703 | |
2704 if (search_regs.num_regs == 0) | |
826 | 2705 signal_error (Qinvalid_operation, |
2706 "replace-match called before any match found", Qunbound); | |
428 | 2707 |
2708 if (NILP (string)) | |
2709 { | |
469 | 2710 if (search_regs.start[sub] < BUF_BEGV (buf) |
2711 || search_regs.start[sub] > search_regs.end[sub] | |
2712 || search_regs.end[sub] > BUF_ZV (buf)) | |
2713 args_out_of_range (make_int (search_regs.start[sub]), | |
2714 make_int (search_regs.end[sub])); | |
428 | 2715 } |
2716 else | |
2717 { | |
2718 if (search_regs.start[0] < 0 | |
2719 || search_regs.start[0] > search_regs.end[0] | |
826 | 2720 || search_regs.end[0] > string_char_length (string)) |
428 | 2721 args_out_of_range (make_int (search_regs.start[0]), |
2722 make_int (search_regs.end[0])); | |
2723 } | |
2724 | |
2725 if (NILP (fixedcase)) | |
2726 { | |
2727 /* Decide how to casify by examining the matched text. */ | |
2728 | |
707 | 2729 last = search_regs.end[sub]; |
428 | 2730 prevc = '\n'; |
2731 case_action = all_caps; | |
2732 | |
2733 /* some_multiletter_word is set nonzero if any original word | |
2734 is more than one letter long. */ | |
2735 some_multiletter_word = 0; | |
2736 some_lowercase = 0; | |
2737 some_nonuppercase_initial = 0; | |
2738 some_uppercase = 0; | |
2739 | |
707 | 2740 for (pos = search_regs.start[sub]; pos < last; pos++) |
428 | 2741 { |
2742 if (NILP (string)) | |
2743 c = BUF_FETCH_CHAR (buf, pos); | |
2744 else | |
867 | 2745 c = string_ichar (string, pos); |
428 | 2746 |
2747 if (LOWERCASEP (buf, c)) | |
2748 { | |
2749 /* Cannot be all caps if any original char is lower case */ | |
2750 | |
2751 some_lowercase = 1; | |
2752 if (!WORD_SYNTAX_P (syntax_table, prevc)) | |
2753 some_nonuppercase_initial = 1; | |
2754 else | |
2755 some_multiletter_word = 1; | |
2756 } | |
2757 else if (!NOCASEP (buf, c)) | |
2758 { | |
2759 some_uppercase = 1; | |
2760 if (!WORD_SYNTAX_P (syntax_table, prevc)) | |
2761 ; | |
2762 else | |
2763 some_multiletter_word = 1; | |
2764 } | |
2765 else | |
2766 { | |
2767 /* If the initial is a caseless word constituent, | |
2768 treat that like a lowercase initial. */ | |
2769 if (!WORD_SYNTAX_P (syntax_table, prevc)) | |
2770 some_nonuppercase_initial = 1; | |
2771 } | |
2772 | |
2773 prevc = c; | |
2774 } | |
2775 | |
2776 /* Convert to all caps if the old text is all caps | |
2777 and has at least one multiletter word. */ | |
2778 if (! some_lowercase && some_multiletter_word) | |
2779 case_action = all_caps; | |
2780 /* Capitalize each word, if the old text has all capitalized words. */ | |
2781 else if (!some_nonuppercase_initial && some_multiletter_word) | |
2782 case_action = cap_initial; | |
2783 else if (!some_nonuppercase_initial && some_uppercase) | |
2784 /* Should x -> yz, operating on X, give Yz or YZ? | |
2785 We'll assume the latter. */ | |
2786 case_action = all_caps; | |
2787 else | |
2788 case_action = nochange; | |
2789 } | |
2790 | |
2791 /* Do replacement in a string. */ | |
2792 if (!NILP (string)) | |
2793 { | |
2794 Lisp_Object before, after; | |
2795 | |
2796 speccount = specpdl_depth (); | |
4199 | 2797 before = Fsubstring (string, Qzero, make_int (search_regs.start[sub])); |
2798 after = Fsubstring (string, make_int (search_regs.end[sub]), Qnil); | |
428 | 2799 |
444 | 2800 /* Do case substitution into REPLACEMENT if desired. */ |
428 | 2801 if (NILP (literal)) |
2802 { | |
826 | 2803 Charcount stlen = string_char_length (replacement); |
428 | 2804 Charcount strpos; |
2805 /* XEmacs change: rewrote this loop somewhat to make it | |
2806 cleaner. Also added \U, \E, etc. */ | |
2807 Charcount literal_start = 0; | |
2808 /* We build up the substituted string in ACCUM. */ | |
2809 Lisp_Object accum; | |
2810 | |
2811 accum = Qnil; | |
2812 | |
2813 /* OK, the basic idea here is that we scan through the | |
2814 replacement string until we find a backslash, which | |
2815 represents a substring of the original string to be | |
2816 substituted. We then append onto ACCUM the literal | |
2817 text before the backslash (LASTPOS marks the | |
2818 beginning of this) followed by the substring of the | |
2819 original string that needs to be inserted. */ | |
2820 for (strpos = 0; strpos < stlen; strpos++) | |
2821 { | |
2822 /* If LITERAL_END is set, we've encountered a backslash | |
2823 (the end of literal text to be inserted). */ | |
2824 Charcount literal_end = -1; | |
2825 /* If SUBSTART is set, we need to also insert the | |
2826 text from SUBSTART to SUBEND in the original string. */ | |
2827 Charcount substart = -1; | |
2828 Charcount subend = -1; | |
2829 | |
867 | 2830 c = string_ichar (replacement, strpos); |
428 | 2831 if (c == '\\' && strpos < stlen - 1) |
2832 { | |
867 | 2833 c = string_ichar (replacement, ++strpos); |
428 | 2834 if (c == '&') |
2835 { | |
2836 literal_end = strpos - 1; | |
2837 substart = search_regs.start[0]; | |
2838 subend = search_regs.end[0]; | |
2839 } | |
4199 | 2840 /* #### This logic is totally broken, |
2841 since we can have backrefs like "\99", right? */ | |
428 | 2842 else if (c >= '1' && c <= '9' && |
2843 c <= search_regs.num_regs + '0') | |
2844 { | |
2845 if (search_regs.start[c - '0'] >= 0) | |
2846 { | |
2847 literal_end = strpos - 1; | |
2848 substart = search_regs.start[c - '0']; | |
2849 subend = search_regs.end[c - '0']; | |
2850 } | |
2851 } | |
2852 else if (c == 'U' || c == 'u' || c == 'L' || c == 'l' || | |
2853 c == 'E') | |
2854 { | |
2855 /* Keep track of all case changes requested, but don't | |
2856 make them now. Do them later so we override | |
2857 everything else. */ | |
2858 if (!ul_pos_dynarr) | |
2859 { | |
2860 ul_pos_dynarr = Dynarr_new (int); | |
2861 ul_action_dynarr = Dynarr_new (int); | |
2862 record_unwind_protect | |
2863 (free_created_dynarrs, | |
2864 noseeum_cons | |
2865 (make_opaque_ptr (ul_pos_dynarr), | |
2866 make_opaque_ptr (ul_action_dynarr))); | |
2867 } | |
2868 literal_end = strpos - 1; | |
2869 Dynarr_add (ul_pos_dynarr, | |
2870 (!NILP (accum) | |
826 | 2871 ? string_char_length (accum) |
428 | 2872 : 0) + (literal_end - literal_start)); |
2873 Dynarr_add (ul_action_dynarr, c); | |
2874 } | |
2875 else if (c == '\\') | |
2876 /* So we get just one backslash. */ | |
2877 literal_end = strpos; | |
2878 } | |
2879 if (literal_end >= 0) | |
2880 { | |
2881 Lisp_Object literal_text = Qnil; | |
2882 Lisp_Object substring = Qnil; | |
2883 if (literal_end != literal_start) | |
444 | 2884 literal_text = Fsubstring (replacement, |
428 | 2885 make_int (literal_start), |
2886 make_int (literal_end)); | |
2887 if (substart >= 0 && subend != substart) | |
2888 substring = Fsubstring (string, | |
2889 make_int (substart), | |
2890 make_int (subend)); | |
2891 if (!NILP (literal_text) || !NILP (substring)) | |
2892 accum = concat3 (accum, literal_text, substring); | |
2893 literal_start = strpos + 1; | |
2894 } | |
2895 } | |
2896 | |
2897 if (strpos != literal_start) | |
2898 /* some literal text at end to be inserted */ | |
444 | 2899 replacement = concat2 (accum, Fsubstring (replacement, |
2900 make_int (literal_start), | |
2901 make_int (strpos))); | |
428 | 2902 else |
444 | 2903 replacement = accum; |
428 | 2904 } |
2905 | |
444 | 2906 /* replacement can be nil. */ |
2907 if (NILP (replacement)) | |
4952
19a72041c5ed
Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents:
4421
diff
changeset
|
2908 replacement = build_ascstring (""); |
444 | 2909 |
428 | 2910 if (case_action == all_caps) |
444 | 2911 replacement = Fupcase (replacement, buffer); |
428 | 2912 else if (case_action == cap_initial) |
444 | 2913 replacement = Fupcase_initials (replacement, buffer); |
428 | 2914 |
2915 /* Now finally, we need to process the \U's, \E's, etc. */ | |
2916 if (ul_pos_dynarr) | |
2917 { | |
2918 int i = 0; | |
2919 int cur_action = 'E'; | |
826 | 2920 Charcount stlen = string_char_length (replacement); |
428 | 2921 Charcount strpos; |
2922 | |
2923 for (strpos = 0; strpos < stlen; strpos++) | |
2924 { | |
867 | 2925 Ichar curchar = string_ichar (replacement, strpos); |
2926 Ichar newchar = -1; | |
428 | 2927 if (i < Dynarr_length (ul_pos_dynarr) && |
2928 strpos == Dynarr_at (ul_pos_dynarr, i)) | |
2929 { | |
2930 int new_action = Dynarr_at (ul_action_dynarr, i); | |
2931 i++; | |
2932 if (new_action == 'u') | |
2933 newchar = UPCASE (buf, curchar); | |
2934 else if (new_action == 'l') | |
2935 newchar = DOWNCASE (buf, curchar); | |
2936 else | |
2937 cur_action = new_action; | |
2938 } | |
2939 if (newchar == -1) | |
2940 { | |
2941 if (cur_action == 'U') | |
2942 newchar = UPCASE (buf, curchar); | |
2943 else if (cur_action == 'L') | |
2944 newchar = DOWNCASE (buf, curchar); | |
2945 else | |
2946 newchar = curchar; | |
2947 } | |
2948 if (newchar != curchar) | |
793 | 2949 set_string_char (replacement, strpos, newchar); |
428 | 2950 } |
2951 } | |
2952 | |
2953 /* frees the Dynarrs if necessary. */ | |
771 | 2954 unbind_to (speccount); |
444 | 2955 return concat3 (before, replacement, after); |
428 | 2956 } |
2957 | |
707 | 2958 mc_count = begin_multiple_change (buf, search_regs.start[sub], |
2959 search_regs.end[sub]); | |
428 | 2960 |
2961 /* begin_multiple_change() records an unwind-protect, so we need to | |
2962 record this value now. */ | |
2963 speccount = specpdl_depth (); | |
2964 | |
2965 /* We insert the replacement text before the old text, and then | |
2966 delete the original text. This means that markers at the | |
2967 beginning or end of the original will float to the corresponding | |
2968 position in the replacement. */ | |
707 | 2969 BUF_SET_PT (buf, search_regs.start[sub]); |
428 | 2970 if (!NILP (literal)) |
444 | 2971 Finsert (1, &replacement); |
428 | 2972 else |
2973 { | |
826 | 2974 Charcount stlen = string_char_length (replacement); |
428 | 2975 Charcount strpos; |
2976 struct gcpro gcpro1; | |
444 | 2977 GCPRO1 (replacement); |
428 | 2978 for (strpos = 0; strpos < stlen; strpos++) |
2979 { | |
707 | 2980 /* on the first iteration assert(offset==0), |
2981 exactly complementing BUF_SET_PT() above. | |
2982 During the loop, it keeps track of the amount inserted. | |
2983 */ | |
2984 Charcount offset = BUF_PT (buf) - search_regs.start[sub]; | |
428 | 2985 |
867 | 2986 c = string_ichar (replacement, strpos); |
428 | 2987 if (c == '\\' && strpos < stlen - 1) |
2988 { | |
707 | 2989 /* XXX FIXME: replacing just a substring non-literally |
2990 using backslash refs to the match looks dangerous. But | |
2991 <15366.18513.698042.156573@ns.caldera.de> from Torsten Duwe | |
2992 <duwe@caldera.de> claims Finsert_buffer_substring already | |
2993 handles this correctly. | |
2994 */ | |
867 | 2995 c = string_ichar (replacement, ++strpos); |
428 | 2996 if (c == '&') |
2997 Finsert_buffer_substring | |
2998 (buffer, | |
2999 make_int (search_regs.start[0] + offset), | |
3000 make_int (search_regs.end[0] + offset)); | |
4199 | 3001 /* #### This logic is totally broken, |
3002 since we can have backrefs like "\99", right? */ | |
428 | 3003 else if (c >= '1' && c <= '9' && |
3004 c <= search_regs.num_regs + '0') | |
3005 { | |
3006 if (search_regs.start[c - '0'] >= 1) | |
3007 Finsert_buffer_substring | |
3008 (buffer, | |
3009 make_int (search_regs.start[c - '0'] + offset), | |
3010 make_int (search_regs.end[c - '0'] + offset)); | |
3011 } | |
3012 else if (c == 'U' || c == 'u' || c == 'L' || c == 'l' || | |
3013 c == 'E') | |
3014 { | |
3015 /* Keep track of all case changes requested, but don't | |
3016 make them now. Do them later so we override | |
3017 everything else. */ | |
3018 if (!ul_pos_dynarr) | |
3019 { | |
3020 ul_pos_dynarr = Dynarr_new (int); | |
3021 ul_action_dynarr = Dynarr_new (int); | |
3022 record_unwind_protect | |
3023 (free_created_dynarrs, | |
3024 Fcons (make_opaque_ptr (ul_pos_dynarr), | |
3025 make_opaque_ptr (ul_action_dynarr))); | |
3026 } | |
3027 Dynarr_add (ul_pos_dynarr, BUF_PT (buf)); | |
3028 Dynarr_add (ul_action_dynarr, c); | |
3029 } | |
3030 else | |
3031 buffer_insert_emacs_char (buf, c); | |
3032 } | |
3033 else | |
3034 buffer_insert_emacs_char (buf, c); | |
3035 } | |
3036 UNGCPRO; | |
3037 } | |
3038 | |
707 | 3039 inslen = BUF_PT (buf) - (search_regs.start[sub]); |
3040 buffer_delete_range (buf, search_regs.start[sub] + inslen, | |
3041 search_regs.end[sub] + inslen, 0); | |
428 | 3042 |
3043 if (case_action == all_caps) | |
3044 Fupcase_region (make_int (BUF_PT (buf) - inslen), | |
3045 make_int (BUF_PT (buf)), buffer); | |
3046 else if (case_action == cap_initial) | |
3047 Fupcase_initials_region (make_int (BUF_PT (buf) - inslen), | |
3048 make_int (BUF_PT (buf)), buffer); | |
3049 | |
3050 /* Now go through and make all the case changes that were requested | |
3051 in the replacement string. */ | |
3052 if (ul_pos_dynarr) | |
3053 { | |
665 | 3054 Charbpos eend = BUF_PT (buf); |
428 | 3055 int i = 0; |
3056 int cur_action = 'E'; | |
3057 | |
3058 for (pos = BUF_PT (buf) - inslen; pos < eend; pos++) | |
3059 { | |
867 | 3060 Ichar curchar = BUF_FETCH_CHAR (buf, pos); |
3061 Ichar newchar = -1; | |
428 | 3062 if (i < Dynarr_length (ul_pos_dynarr) && |
3063 pos == Dynarr_at (ul_pos_dynarr, i)) | |
3064 { | |
3065 int new_action = Dynarr_at (ul_action_dynarr, i); | |
3066 i++; | |
3067 if (new_action == 'u') | |
3068 newchar = UPCASE (buf, curchar); | |
3069 else if (new_action == 'l') | |
3070 newchar = DOWNCASE (buf, curchar); | |
3071 else | |
3072 cur_action = new_action; | |
3073 } | |
3074 if (newchar == -1) | |
3075 { | |
3076 if (cur_action == 'U') | |
3077 newchar = UPCASE (buf, curchar); | |
3078 else if (cur_action == 'L') | |
3079 newchar = DOWNCASE (buf, curchar); | |
3080 else | |
3081 newchar = curchar; | |
3082 } | |
3083 if (newchar != curchar) | |
3084 buffer_replace_char (buf, pos, newchar, 0, 0); | |
3085 } | |
3086 } | |
3087 | |
3088 /* frees the Dynarrs if necessary. */ | |
771 | 3089 unbind_to (speccount); |
428 | 3090 end_multiple_change (buf, mc_count); |
3091 | |
3092 return Qnil; | |
3093 } | |
3094 | |
3095 static Lisp_Object | |
3096 match_limit (Lisp_Object num, int beginningp) | |
3097 { | |
3098 int n; | |
3099 | |
3100 CHECK_INT (num); | |
3101 n = XINT (num); | |
3102 if (n < 0 || n >= search_regs.num_regs) | |
3103 args_out_of_range (num, make_int (search_regs.num_regs)); | |
3104 if (search_regs.num_regs == 0 || | |
3105 search_regs.start[n] < 0) | |
3106 return Qnil; | |
3107 return make_int (beginningp ? search_regs.start[n] : search_regs.end[n]); | |
3108 } | |
3109 | |
3110 DEFUN ("match-beginning", Fmatch_beginning, 1, 1, 0, /* | |
3111 Return position of start of text matched by last regexp search. | |
3112 NUM, specifies which parenthesized expression in the last regexp. | |
3113 Value is nil if NUMth pair didn't match, or there were less than NUM pairs. | |
3114 Zero means the entire text matched by the whole regexp or whole string. | |
3115 */ | |
3116 (num)) | |
3117 { | |
3118 return match_limit (num, 1); | |
3119 } | |
3120 | |
3121 DEFUN ("match-end", Fmatch_end, 1, 1, 0, /* | |
3122 Return position of end of text matched by last regexp search. | |
3123 NUM specifies which parenthesized expression in the last regexp. | |
3124 Value is nil if NUMth pair didn't match, or there were less than NUM pairs. | |
3125 Zero means the entire text matched by the whole regexp or whole string. | |
3126 */ | |
3127 (num)) | |
3128 { | |
3129 return match_limit (num, 0); | |
3130 } | |
3131 | |
3132 DEFUN ("match-data", Fmatch_data, 0, 2, 0, /* | |
3133 Return a list containing all info on what the last regexp search matched. | |
3134 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'. | |
3135 All the elements are markers or nil (nil if the Nth pair didn't match) | |
3136 if the last match was on a buffer; integers or nil if a string was matched. | |
3137 Use `store-match-data' to reinstate the data in this list. | |
3138 | |
3139 If INTEGERS (the optional first argument) is non-nil, always use integers | |
3140 \(rather than markers) to represent buffer positions. | |
3141 If REUSE is a list, reuse it as part of the value. If REUSE is long enough | |
3142 to hold all the values, and if INTEGERS is non-nil, no consing is done. | |
3143 */ | |
3144 (integers, reuse)) | |
3145 { | |
3146 Lisp_Object tail, prev; | |
3147 Lisp_Object *data; | |
3148 int i; | |
3149 Charcount len; | |
3150 | |
3151 if (NILP (last_thing_searched)) | |
563 | 3152 /*error ("match-data called before any match found", Qunbound);*/ |
428 | 3153 return Qnil; |
3154 | |
3155 data = alloca_array (Lisp_Object, 2 * search_regs.num_regs); | |
3156 | |
3157 len = -1; | |
3158 for (i = 0; i < search_regs.num_regs; i++) | |
3159 { | |
665 | 3160 Charbpos start = search_regs.start[i]; |
428 | 3161 if (start >= 0) |
3162 { | |
3163 if (EQ (last_thing_searched, Qt) | |
3164 || !NILP (integers)) | |
3165 { | |
3166 data[2 * i] = make_int (start); | |
3167 data[2 * i + 1] = make_int (search_regs.end[i]); | |
3168 } | |
3169 else if (BUFFERP (last_thing_searched)) | |
3170 { | |
3171 data[2 * i] = Fmake_marker (); | |
3172 Fset_marker (data[2 * i], | |
3173 make_int (start), | |
3174 last_thing_searched); | |
3175 data[2 * i + 1] = Fmake_marker (); | |
3176 Fset_marker (data[2 * i + 1], | |
3177 make_int (search_regs.end[i]), | |
3178 last_thing_searched); | |
3179 } | |
3180 else | |
3181 /* last_thing_searched must always be Qt, a buffer, or Qnil. */ | |
2500 | 3182 ABORT (); |
428 | 3183 |
3184 len = i; | |
3185 } | |
3186 else | |
3187 data[2 * i] = data [2 * i + 1] = Qnil; | |
3188 } | |
3189 if (!CONSP (reuse)) | |
3190 return Flist (2 * len + 2, data); | |
3191 | |
3192 /* If REUSE is a list, store as many value elements as will fit | |
3193 into the elements of REUSE. */ | |
3194 for (prev = Qnil, i = 0, tail = reuse; CONSP (tail); i++, tail = XCDR (tail)) | |
3195 { | |
3196 if (i < 2 * len + 2) | |
3197 XCAR (tail) = data[i]; | |
3198 else | |
3199 XCAR (tail) = Qnil; | |
3200 prev = tail; | |
3201 } | |
3202 | |
3203 /* If we couldn't fit all value elements into REUSE, | |
3204 cons up the rest of them and add them to the end of REUSE. */ | |
3205 if (i < 2 * len + 2) | |
3206 XCDR (prev) = Flist (2 * len + 2 - i, data + i); | |
3207 | |
3208 return reuse; | |
3209 } | |
3210 | |
3211 | |
3212 DEFUN ("store-match-data", Fstore_match_data, 1, 1, 0, /* | |
3213 Set internal data on last search match from elements of LIST. | |
1468 | 3214 LIST should have been created by calling `match-data' previously, |
3215 or be nil, to clear the internal match data. | |
428 | 3216 */ |
3217 (list)) | |
3218 { | |
3219 REGISTER int i; | |
3220 REGISTER Lisp_Object marker; | |
3221 int num_regs; | |
3222 int length; | |
3223 | |
853 | 3224 /* Some FSF junk with running_asynch_code, to preserve the match |
3225 data. Not necessary because we don't call process filters | |
3226 asynchronously (i.e. from within QUIT). */ | |
428 | 3227 |
3228 CONCHECK_LIST (list); | |
3229 | |
3230 /* Unless we find a marker with a buffer in LIST, assume that this | |
3231 match data came from a string. */ | |
3232 last_thing_searched = Qt; | |
3233 | |
3234 /* Allocate registers if they don't already exist. */ | |
3235 length = XINT (Flength (list)) / 2; | |
3236 num_regs = search_regs.num_regs; | |
3237 | |
3238 if (length > num_regs) | |
3239 { | |
3240 if (search_regs.num_regs == 0) | |
3241 { | |
3242 search_regs.start = xnew_array (regoff_t, length); | |
3243 search_regs.end = xnew_array (regoff_t, length); | |
3244 } | |
3245 else | |
3246 { | |
3247 XREALLOC_ARRAY (search_regs.start, regoff_t, length); | |
3248 XREALLOC_ARRAY (search_regs.end, regoff_t, length); | |
3249 } | |
3250 | |
3251 search_regs.num_regs = length; | |
3252 } | |
3253 | |
3254 for (i = 0; i < num_regs; i++) | |
3255 { | |
3256 marker = Fcar (list); | |
3257 if (NILP (marker)) | |
3258 { | |
3259 search_regs.start[i] = -1; | |
3260 list = Fcdr (list); | |
3261 } | |
3262 else | |
3263 { | |
3264 if (MARKERP (marker)) | |
3265 { | |
3266 if (XMARKER (marker)->buffer == 0) | |
3267 marker = Qzero; | |
3268 else | |
793 | 3269 last_thing_searched = wrap_buffer (XMARKER (marker)->buffer); |
428 | 3270 } |
3271 | |
3272 CHECK_INT_COERCE_MARKER (marker); | |
3273 search_regs.start[i] = XINT (marker); | |
3274 list = Fcdr (list); | |
3275 | |
3276 marker = Fcar (list); | |
3277 if (MARKERP (marker) && XMARKER (marker)->buffer == 0) | |
3278 marker = Qzero; | |
3279 | |
3280 CHECK_INT_COERCE_MARKER (marker); | |
3281 search_regs.end[i] = XINT (marker); | |
3282 } | |
3283 list = Fcdr (list); | |
3284 } | |
3285 | |
3286 return Qnil; | |
3287 } | |
3288 | |
3289 /* Quote a string to inactivate reg-expr chars */ | |
3290 | |
3291 DEFUN ("regexp-quote", Fregexp_quote, 1, 1, 0, /* | |
3292 Return a regexp string which matches exactly STRING and nothing else. | |
3293 */ | |
444 | 3294 (string)) |
428 | 3295 { |
867 | 3296 REGISTER Ibyte *in, *out, *end; |
3297 REGISTER Ibyte *temp; | |
428 | 3298 |
444 | 3299 CHECK_STRING (string); |
428 | 3300 |
2367 | 3301 temp = alloca_ibytes (XSTRING_LENGTH (string) * 2); |
428 | 3302 |
3303 /* Now copy the data into the new string, inserting escapes. */ | |
3304 | |
444 | 3305 in = XSTRING_DATA (string); |
3306 end = in + XSTRING_LENGTH (string); | |
428 | 3307 out = temp; |
3308 | |
3309 while (in < end) | |
3310 { | |
867 | 3311 Ichar c = itext_ichar (in); |
428 | 3312 |
3313 if (c == '[' || c == ']' | |
3314 || c == '*' || c == '.' || c == '\\' | |
3315 || c == '?' || c == '+' | |
3316 || c == '^' || c == '$') | |
3317 *out++ = '\\'; | |
867 | 3318 out += set_itext_ichar (out, c); |
3319 INC_IBYTEPTR (in); | |
428 | 3320 } |
3321 | |
3322 return make_string (temp, out - temp); | |
3323 } | |
3324 | |
3325 DEFUN ("set-word-regexp", Fset_word_regexp, 1, 1, 0, /* | |
3326 Set the regexp to be used to match a word in regular-expression searching. | |
3327 #### Not yet implemented. Currently does nothing. | |
3328 #### Do not use this yet. Its calling interface is likely to change. | |
3329 */ | |
2286 | 3330 (UNUSED (regexp))) |
428 | 3331 { |
3332 return Qnil; | |
3333 } | |
3334 | |
3335 | |
3336 /************************************************************************/ | |
3337 /* initialization */ | |
3338 /************************************************************************/ | |
3339 | |
3340 void | |
3341 syms_of_search (void) | |
3342 { | |
3343 | |
442 | 3344 DEFERROR_STANDARD (Qsearch_failed, Qinvalid_operation); |
3345 DEFERROR_STANDARD (Qinvalid_regexp, Qsyntax_error); | |
563 | 3346 Fput (Qinvalid_regexp, Qerror_lacks_explanatory_string, Qt); |
428 | 3347 |
3348 DEFSUBR (Flooking_at); | |
3349 DEFSUBR (Fposix_looking_at); | |
3350 DEFSUBR (Fstring_match); | |
3351 DEFSUBR (Fposix_string_match); | |
3352 DEFSUBR (Fskip_chars_forward); | |
3353 DEFSUBR (Fskip_chars_backward); | |
3354 DEFSUBR (Fskip_syntax_forward); | |
3355 DEFSUBR (Fskip_syntax_backward); | |
3356 DEFSUBR (Fsearch_forward); | |
3357 DEFSUBR (Fsearch_backward); | |
3358 DEFSUBR (Fword_search_forward); | |
3359 DEFSUBR (Fword_search_backward); | |
3360 DEFSUBR (Fre_search_forward); | |
3361 DEFSUBR (Fre_search_backward); | |
3362 DEFSUBR (Fposix_search_forward); | |
3363 DEFSUBR (Fposix_search_backward); | |
3364 DEFSUBR (Freplace_match); | |
3365 DEFSUBR (Fmatch_beginning); | |
3366 DEFSUBR (Fmatch_end); | |
3367 DEFSUBR (Fmatch_data); | |
3368 DEFSUBR (Fstore_match_data); | |
3369 DEFSUBR (Fregexp_quote); | |
3370 DEFSUBR (Fset_word_regexp); | |
3371 } | |
3372 | |
3373 void | |
3374 reinit_vars_of_search (void) | |
3375 { | |
3376 int i; | |
3377 | |
3378 last_thing_searched = Qnil; | |
3379 staticpro_nodump (&last_thing_searched); | |
3380 | |
3381 for (i = 0; i < REGEXP_CACHE_SIZE; ++i) | |
3382 { | |
3383 searchbufs[i].buf.allocated = 100; | |
3384 searchbufs[i].buf.buffer = (unsigned char *) xmalloc (100); | |
3385 searchbufs[i].buf.fastmap = searchbufs[i].fastmap; | |
3386 searchbufs[i].regexp = Qnil; | |
3387 staticpro_nodump (&searchbufs[i].regexp); | |
3388 searchbufs[i].next = (i == REGEXP_CACHE_SIZE-1 ? 0 : &searchbufs[i+1]); | |
3389 } | |
3390 searchbuf_head = &searchbufs[0]; | |
3391 } | |
3392 | |
3393 void | |
3394 vars_of_search (void) | |
3395 { | |
3396 DEFVAR_LISP ("forward-word-regexp", &Vforward_word_regexp /* | |
3397 *Regular expression to be used in `forward-word'. | |
3398 #### Not yet implemented. | |
3399 */ ); | |
3400 Vforward_word_regexp = Qnil; | |
3401 | |
3402 DEFVAR_LISP ("backward-word-regexp", &Vbackward_word_regexp /* | |
3403 *Regular expression to be used in `backward-word'. | |
3404 #### Not yet implemented. | |
3405 */ ); | |
3406 Vbackward_word_regexp = Qnil; | |
502 | 3407 |
3408 DEFVAR_INT ("warn-about-possibly-incompatible-back-references", | |
3409 &warn_about_possibly_incompatible_back_references /* | |
3410 If true, issue warnings when new-semantics back references occur. | |
3411 This is to catch places where old code might inadvertently have changed | |
3412 semantics. This will occur in old code only where more than nine groups | |
3413 occur and a back reference to one of them is directly followed by a digit. | |
3414 */ ); | |
3415 warn_about_possibly_incompatible_back_references = 1; | |
814 | 3416 |
2421 | 3417 Vskip_chars_range_table = Fmake_range_table (Qstart_closed_end_closed); |
428 | 3418 staticpro (&Vskip_chars_range_table); |
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3419 #ifdef DEBUG_XEMACS |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3420 DEFSYMBOL (Qsearch_algorithm_used); |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3421 DEFSYMBOL (Qboyer_moore); |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3422 DEFSYMBOL (Qsimple_search); |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3423 |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3424 DEFVAR_INT ("debug-xemacs-searches", &debug_xemacs_searches /* |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3425 If non-zero, bind `search-algorithm-used' to `boyer-moore' or `simple-search', |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3426 depending on the algorithm used for each search. Used for testing. |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3427 */ ); |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3428 debug_xemacs_searches = 0; |
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3429 #endif |
428 | 3430 } |