Mercurial > hg > xemacs-beta
annotate src/search.c @ 4505:a5f1da0eb001
Document XFT support in the FAQ, better describe XEmacs pronunciation.
2008-08-31 Aidan Kehoe <kehoea@parhasard.net>
* xemacs-faq.texi (Q5.0.7): Add a section on how one can use
antialiased fonts under X11; thank you Giacomo Boffi.
(Q1.0.3): Clarify pronunciation using a serious phonetic
alphabet.
| author | Aidan Kehoe <kehoea@parhasard.net> |
|---|---|
| date | Sun, 31 Aug 2008 13:53:36 +0200 |
| parents | 69b803c646cd |
| children | 91a023144e72 19a72041c5ed |
| 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 { | |
| 563 | 187 maybe_signal_error (Qinvalid_regexp, 0, build_string (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 } |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1428 } 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
|
1429 |
|
4421
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1430 if (!checked) |
|
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1431 { |
|
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1432 #ifdef DEBUG_XEMACS |
|
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1433 if (debug_xemacs_searches) |
|
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1434 { |
|
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1435 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
|
1436 sym->value = Qnil; |
|
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1437 } |
|
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1438 #endif |
|
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1439 /* 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
|
1440 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
|
1441 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
|
1442 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
|
1443 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
|
1444 return n > 0 ? -n : n; |
|
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1445 } |
|
69b803c646cd
Fail searches immediately if searching for non-representable characters.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4414
diff
changeset
|
1446 |
|
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1447 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
|
1448 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
|
1449 { |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1450 /* 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
|
1451 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
|
1452 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
|
1453 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
|
1454 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
|
1455 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
|
1456 { |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1457 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
|
1458 |
|
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 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
|
1460 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
|
1461 |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1462 } 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
|
1463 |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1464 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
|
1465 |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1466 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
|
1467 } |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1468 } |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1469 |
| 446 | 1470 memcpy (pat, tmp_str, new_bytelen); |
| 1471 pat += new_bytelen; | |
| 1472 base_pat += orig_bytelen; | |
| 1473 len -= orig_bytelen; | |
| 1474 } | |
|
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1475 |
|
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1476 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
|
1477 { |
|
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1478 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
|
1479 } |
|
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1480 |
| 446 | 1481 #else /* not MULE */ |
| 1482 while (--len >= 0) | |
| 1483 { | |
| 1484 /* If we got here and the RE flag is set, it's because | |
| 1485 we're dealing with a regexp known to be trivial, so the | |
| 1486 backslash just quotes the next character. */ | |
| 1487 if (RE && *base_pat == '\\') | |
| 1488 { | |
| 1489 len--; | |
| 1490 base_pat++; | |
| 1491 } | |
| 1492 *pat++ = TRANSLATE (trt, *base_pat++); | |
| 1493 } | |
| 1494 #endif /* MULE */ | |
| 1495 len = pat - patbuf; | |
| 1496 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
|
1497 |
|
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1498 #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
|
1499 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
|
1500 { |
|
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1501 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
|
1502 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
|
1503 } |
|
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1504 #endif |
|
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1505 |
| 446 | 1506 if (boyer_moore_ok) |
| 1507 return boyer_moore (buf, base_pat, len, pos, lim, n, | |
| 1508 trt, inverse_trt, charset_base); | |
| 1509 else | |
| 1510 return simple_search (buf, base_pat, len, pos, lim, n, trt); | |
| 1511 } | |
| 1512 } | |
| 1513 | |
| 826 | 1514 /* Do a simple string search N times for the string PAT, whose length is |
| 1515 LEN/LEN_BYTE, from buffer position POS until LIM. TRT is the | |
| 1516 translation table. | |
| 446 | 1517 |
| 1518 Return the character position where the match is found. | |
| 1519 Otherwise, if M matches remained to be found, return -M. | |
| 1520 | |
| 1521 This kind of search works regardless of what is in PAT and | |
| 1522 regardless of what is in TRT. It is used in cases where | |
| 1523 boyer_moore cannot work. */ | |
| 1524 | |
| 665 | 1525 static Charbpos |
| 867 | 1526 simple_search (struct buffer *buf, Ibyte *base_pat, Bytecount len, |
| 826 | 1527 Bytebpos pos, Bytebpos lim, EMACS_INT n, Lisp_Object trt) |
| 446 | 1528 { |
| 1529 int forward = n > 0; | |
| 1530 Bytecount buf_len = 0; /* Shut up compiler. */ | |
| 1531 | |
| 826 | 1532 if (lim > pos) |
| 446 | 1533 while (n > 0) |
| 428 | 1534 { |
| 446 | 1535 while (1) |
| 428 | 1536 { |
| 826 | 1537 Bytecount this_len = len; |
| 1538 Bytebpos this_pos = pos; | |
| 867 | 1539 Ibyte *p = base_pat; |
| 826 | 1540 if (pos >= lim) |
| 446 | 1541 goto stop; |
| 1542 | |
| 1543 while (this_len > 0) | |
| 1544 { | |
| 867 | 1545 Ichar pat_ch, buf_ch; |
| 446 | 1546 Bytecount pat_len; |
| 1547 | |
| 867 | 1548 pat_ch = itext_ichar (p); |
| 826 | 1549 buf_ch = BYTE_BUF_FETCH_CHAR (buf, this_pos); |
| 446 | 1550 |
| 1551 buf_ch = TRANSLATE (trt, buf_ch); | |
| 1552 | |
| 1553 if (buf_ch != pat_ch) | |
| 1554 break; | |
| 1555 | |
| 867 | 1556 pat_len = itext_ichar_len (p); |
| 446 | 1557 p += pat_len; |
| 1558 this_len -= pat_len; | |
| 826 | 1559 INC_BYTEBPOS (buf, this_pos); |
| 446 | 1560 } |
| 1561 if (this_len == 0) | |
| 428 | 1562 { |
| 826 | 1563 buf_len = this_pos - pos; |
| 1564 pos = this_pos; | |
| 446 | 1565 break; |
| 428 | 1566 } |
| 826 | 1567 INC_BYTEBPOS (buf, pos); |
| 428 | 1568 } |
| 446 | 1569 n--; |
| 1570 } | |
| 1571 else | |
|
4322
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1572 { |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1573 /* 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
|
1574 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
|
1575 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
|
1576 DEC_BYTEBPOS below. */ |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1577 if (lim < len) |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1578 lim = len; |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1579 while (n < 0) |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1580 { |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1581 while (1) |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1582 { |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1583 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
|
1584 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
|
1585 Ibyte *p; |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1586 if (pos <= lim) |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1587 goto stop; |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1588 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
|
1589 |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1590 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
|
1591 { |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1592 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
|
1593 |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1594 DEC_IBYTEPTR (p); |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1595 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
|
1596 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
|
1597 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
|
1598 |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1599 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
|
1600 |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1601 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
|
1602 break; |
|
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 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
|
1605 } |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1606 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
|
1607 { |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1608 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
|
1609 pos = this_pos; |
| 446 | 1610 break; |
|
4322
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 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
|
1613 } |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1614 n++; |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1615 } |
|
f70e56bb52a7
src/search.c (simple_search): Fix underrun in reverse search.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4199
diff
changeset
|
1616 } |
| 446 | 1617 stop: |
| 1618 if (n == 0) | |
| 1619 { | |
| 665 | 1620 Charbpos beg, end, retval; |
| 446 | 1621 if (forward) |
| 1622 { | |
| 826 | 1623 beg = bytebpos_to_charbpos (buf, pos - buf_len); |
| 1624 retval = end = bytebpos_to_charbpos (buf, pos); | |
| 446 | 1625 } |
| 1626 else | |
| 428 | 1627 { |
| 826 | 1628 retval = beg = bytebpos_to_charbpos (buf, pos); |
| 1629 end = bytebpos_to_charbpos (buf, pos + buf_len); | |
| 428 | 1630 } |
| 446 | 1631 set_search_regs (buf, beg, end - beg); |
| 1632 | |
| 1633 return retval; | |
| 1634 } | |
| 1635 else if (n > 0) | |
| 1636 return -n; | |
| 1637 else | |
| 1638 return n; | |
| 1639 } | |
| 1640 | |
| 1641 /* Do Boyer-Moore search N times for the string PAT, | |
| 1642 whose length is LEN/LEN_BYTE, | |
| 1643 from buffer position POS/POS_BYTE until LIM/LIM_BYTE. | |
| 1644 DIRECTION says which direction we search in. | |
| 1645 TRT and INVERSE_TRT are translation tables. | |
| 1646 | |
| 1647 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
|
1648 (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
|
1649 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
|
1650 so after just a simple test of the context. |
| 446 | 1651 |
|
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
|
1652 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
|
1653 get an assertion failure. */ |
| 446 | 1654 |
| 665 | 1655 static Charbpos |
| 867 | 1656 boyer_moore (struct buffer *buf, Ibyte *base_pat, Bytecount len, |
| 665 | 1657 Bytebpos pos, Bytebpos lim, EMACS_INT n, Lisp_Object trt, |
| 2333 | 1658 Lisp_Object inverse_trt, int USED_IF_MULE (charset_base)) |
| 446 | 1659 { |
| 1660 /* #### Someone really really really needs to comment the workings | |
| 1661 of this junk somewhat better. | |
| 1662 | |
| 1663 BTW "BM" stands for Boyer-Moore, which is one of the standard | |
| 1664 string-searching algorithms. It's the best string-searching | |
| 1665 algorithm out there, provided that: | |
| 1666 | |
| 1667 a) You're not fazed by algorithm complexity. (Rabin-Karp, which | |
| 1668 uses hashing, is much much easier to code but not as fast.) | |
| 1669 b) You can freely move backwards in the string that you're | |
| 1670 searching through. | |
| 1671 | |
| 1672 As the comment below tries to explain (but garbles in typical | |
| 1673 programmer-ese), the idea is that you don't have to do a | |
| 1674 string match at every successive position in the text. For | |
| 1675 example, let's say the pattern is "a very long string". We | |
| 1676 compare the last character in the string (`g') with the | |
| 1677 corresponding character in the text. If it mismatches, and | |
| 1678 it is, say, `z', then we can skip forward by the entire | |
| 1679 length of the pattern because `z' does not occur anywhere | |
| 1680 in the pattern. If the mismatching character does occur | |
| 1681 in the pattern, we can usually still skip forward by more | |
| 1682 than one: e.g. if it is `l', then we can skip forward | |
| 1683 by the length of the substring "ong string" -- i.e. the | |
| 1684 largest end section of the pattern that does not contain | |
| 1685 the mismatched character. So what we do is compute, for | |
| 1686 each possible character, the distance we can skip forward | |
| 1687 (the "stride") and use it in the string matching. This | |
| 1688 is what the BM_tab holds. */ | |
| 1689 REGISTER EMACS_INT *BM_tab; | |
| 1690 EMACS_INT *BM_tab_base; | |
| 1691 REGISTER Bytecount dirlen; | |
| 1692 EMACS_INT infinity; | |
| 665 | 1693 Bytebpos limit; |
| 446 | 1694 Bytecount stride_for_teases = 0; |
| 1695 REGISTER EMACS_INT i, j; | |
| 867 | 1696 Ibyte *pat, *pat_end; |
| 1697 REGISTER Ibyte *cursor, *p_limit, *ptr2; | |
| 1698 Ibyte simple_translate[0400]; | |
| 446 | 1699 REGISTER int direction = ((n > 0) ? 1 : -1); |
| 1700 #ifdef MULE | |
| 867 | 1701 Ibyte translate_prev_byte = 0; |
| 1702 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
|
1703 /* 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
|
1704 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
|
1705 (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
|
1706 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
|
1707 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
|
1708 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
|
1709 buf->text->num_8_bit_fixed_chars == BUF_Z(buf) - BUF_BEG (buf); |
| 446 | 1710 #endif |
| 1711 #ifdef C_ALLOCA | |
| 1712 EMACS_INT BM_tab_space[0400]; | |
| 1713 BM_tab = &BM_tab_space[0]; | |
| 1714 #else | |
| 1715 BM_tab = alloca_array (EMACS_INT, 256); | |
| 1716 #endif | |
| 1717 | |
| 1718 /* The general approach is that we are going to maintain that we | |
| 1719 know the first (closest to the present position, in whatever | |
| 1720 direction we're searching) character that could possibly be | |
| 1721 the last (furthest from present position) character of a | |
| 1722 valid match. We advance the state of our knowledge by | |
| 1723 looking at that character and seeing whether it indeed | |
| 1724 matches the last character of the pattern. If it does, we | |
| 1725 take a closer look. If it does not, we move our pointer (to | |
| 1726 putative last characters) as far as is logically possible. | |
| 1727 This amount of movement, which I call a stride, will be the | |
| 1728 length of the pattern if the actual character appears nowhere | |
| 1729 in the pattern, otherwise it will be the distance from the | |
| 1730 last occurrence of that character to the end of the pattern. | |
| 1731 As a coding trick, an enormous stride is coded into the table | |
| 1732 for characters that match the last character. This allows | |
| 1733 use of only a single test, a test for having gone past the | |
| 1734 end of the permissible match region, to test for both | |
| 1735 possible matches (when the stride goes past the end | |
| 1736 immediately) and failure to match (where you get nudged past | |
| 1737 the end one stride at a time). | |
| 1738 | |
| 1739 Here we make a "mickey mouse" BM table. The stride of the | |
| 1740 search is determined only by the last character of the | |
| 1741 putative match. If that character does not match, we will | |
| 1742 stride the proper distance to propose a match that | |
| 1743 superimposes it on the last instance of a character that | |
| 1744 matches it (per trt), or misses it entirely if there is | |
| 1745 none. */ | |
| 1746 | |
| 1747 dirlen = len * direction; | |
| 1748 infinity = dirlen - (lim + pos + len + len) * direction; | |
| 1749 /* Record position after the end of the pattern. */ | |
| 1750 pat_end = base_pat + len; | |
| 1751 if (direction < 0) | |
| 1752 base_pat = pat_end - 1; | |
| 1753 BM_tab_base = BM_tab; | |
| 1754 BM_tab += 0400; | |
| 1755 j = dirlen; /* to get it in a register */ | |
| 1756 /* A character that does not appear in the pattern induces a | |
| 1757 stride equal to the pattern length. */ | |
| 1758 while (BM_tab_base != BM_tab) | |
| 1759 { | |
| 1760 *--BM_tab = j; | |
| 1761 *--BM_tab = j; | |
| 1762 *--BM_tab = j; | |
| 1763 *--BM_tab = j; | |
| 1764 } | |
| 1765 /* We use this for translation, instead of TRT itself. We | |
| 1766 fill this in to handle the characters that actually occur | |
| 1767 in the pattern. Others don't matter anyway! */ | |
| 1768 xzero (simple_translate); | |
| 1769 for (i = 0; i < 0400; i++) | |
| 867 | 1770 simple_translate[i] = (Ibyte) i; |
| 446 | 1771 i = 0; |
| 1425 | 1772 |
| 446 | 1773 while (i != infinity) |
| 1774 { | |
| 867 | 1775 Ibyte *ptr = base_pat + i; |
| 446 | 1776 i += direction; |
| 1777 if (i == dirlen) | |
| 1778 i = infinity; | |
| 1779 if (!NILP (trt)) | |
| 428 | 1780 { |
| 446 | 1781 #ifdef MULE |
| 867 | 1782 Ichar ch, untranslated; |
| 446 | 1783 int this_translated = 1; |
| 1784 | |
| 1785 /* Is *PTR the last byte of a character? */ | |
| 867 | 1786 if (pat_end - ptr == 1 || ibyte_first_byte_p (ptr[1])) |
| 428 | 1787 { |
| 867 | 1788 Ibyte *charstart = ptr; |
| 1789 while (!ibyte_first_byte_p (*charstart)) | |
| 446 | 1790 charstart--; |
| 867 | 1791 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
|
1792 |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1793 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
|
1794 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
|
1795 { |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1796 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
|
1797 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
|
1798 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
|
1799 } |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1800 |
|
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1801 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
|
1802 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
|
1803 { |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1804 /* 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
|
1805 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
|
1806 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
|
1807 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
|
1808 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
|
1809 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
|
1810 |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1811 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
|
1812 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
|
1813 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
|
1814 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
|
1815 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
|
1816 { |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1817 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
|
1818 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
|
1819 break; |
|
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1820 ++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
|
1821 } 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
|
1822 |
|
4414
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
1823 /* 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
|
1824 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
|
1825 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
|
1826 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
|
1827 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
|
1828 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
|
1829 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
|
1830 assert (1 == count || starting_ch != ch); |
| 446 | 1831 } |
| 428 | 1832 } |
| 1833 else | |
| 1834 { | |
| 446 | 1835 ch = *ptr; |
| 1836 this_translated = 0; | |
| 1837 } | |
| 1838 if (ch > 0400) | |
| 1839 j = ((unsigned char) ch | 0200); | |
| 1840 else | |
| 1841 j = (unsigned char) ch; | |
| 1842 | |
| 1843 if (i == infinity) | |
| 1844 stride_for_teases = BM_tab[j]; | |
| 1845 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
|
1846 /* 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
|
1847 comment in casetab.c. */ |
| 446 | 1848 if (this_translated) |
| 1849 { | |
| 867 | 1850 Ichar starting_ch = ch; |
| 446 | 1851 EMACS_INT starting_j = j; |
|
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
|
1852 do |
| 446 | 1853 { |
| 1854 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
|
1855 |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1856 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
|
1857 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
|
1858 |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1859 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
|
1860 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
|
1861 |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1862 if (ch > 0400) |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1863 j = ((unsigned char) ch | 0200); |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1864 else |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1865 j = (unsigned char) 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
|
1866 |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1867 /* 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
|
1868 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
|
1869 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
|
1870 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
|
1871 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
|
1872 |
|
4ee73bbe4f8e
Always use boyer_moore in ASCII or Latin-1 buffers with ASCII search strings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4322
diff
changeset
|
1873 } while (ch != starting_ch); |
| 446 | 1874 } |
| 1875 #else | |
| 1876 EMACS_INT k; | |
| 1877 j = *ptr; | |
| 1878 k = (j = TRANSLATE (trt, j)); | |
| 1879 if (i == infinity) | |
| 1880 stride_for_teases = BM_tab[j]; | |
| 1881 BM_tab[j] = dirlen - i; | |
| 1882 /* A translation table is accompanied by its inverse -- | |
| 826 | 1883 see comment in casetab.c. */ |
| 446 | 1884 while ((j = TRANSLATE (inverse_trt, j)) != k) |
| 1885 { | |
| 867 | 1886 simple_translate[j] = (Ibyte) k; |
| 428 | 1887 BM_tab[j] = dirlen - i; |
| 1888 } | |
| 446 | 1889 #endif |
| 1890 } | |
| 1891 else | |
| 1892 { | |
| 1893 j = *ptr; | |
| 1894 | |
| 1895 if (i == infinity) | |
| 1896 stride_for_teases = BM_tab[j]; | |
| 1897 BM_tab[j] = dirlen - i; | |
| 428 | 1898 } |
| 446 | 1899 /* stride_for_teases tells how much to stride if we get a |
| 1900 match on the far character but are subsequently | |
| 1901 disappointed, by recording what the stride would have been | |
| 1902 for that character if the last character had been | |
| 1903 different. */ | |
| 1904 } | |
| 1905 infinity = dirlen - infinity; | |
| 1906 pos += dirlen - ((direction > 0) ? direction : 0); | |
| 1907 /* loop invariant - pos points at where last char (first char if | |
| 1908 reverse) of pattern would align in a possible match. */ | |
| 1909 while (n != 0) | |
| 1910 { | |
| 665 | 1911 Bytebpos tail_end; |
| 867 | 1912 Ibyte *tail_end_ptr; |
| 446 | 1913 /* It's been reported that some (broken) compiler thinks |
| 1914 that Boolean expressions in an arithmetic context are | |
| 1915 unsigned. Using an explicit ?1:0 prevents this. */ | |
| 1916 if ((lim - pos - ((direction > 0) ? 1 : 0)) * direction < 0) | |
| 1917 return n * (0 - direction); | |
| 1918 /* First we do the part we can by pointers (maybe | |
| 1919 nothing) */ | |
| 1920 QUIT; | |
| 1921 pat = base_pat; | |
| 1922 limit = pos - dirlen + direction; | |
| 1923 /* XEmacs change: definitions of CEILING_OF and FLOOR_OF | |
| 1924 have changed. See buffer.h. */ | |
| 1925 limit = ((direction > 0) | |
| 826 | 1926 ? BYTE_BUF_CEILING_OF (buf, limit) - 1 |
| 1927 : BYTE_BUF_FLOOR_OF (buf, limit + 1)); | |
| 446 | 1928 /* LIMIT is now the last (not beyond-last!) value POS can |
| 1929 take on without hitting edge of buffer or the gap. */ | |
| 1930 limit = ((direction > 0) | |
| 1931 ? min (lim - 1, min (limit, pos + 20000)) | |
| 1932 : max (lim, max (limit, pos - 20000))); | |
| 826 | 1933 tail_end = BYTE_BUF_CEILING_OF (buf, pos); |
| 1934 tail_end_ptr = BYTE_BUF_BYTE_ADDRESS (buf, tail_end); | |
| 446 | 1935 |
| 1936 if ((limit - pos) * direction > 20) | |
| 428 | 1937 { |
| 826 | 1938 /* We have to be careful because the code can generate addresses |
| 1939 that don't point to the beginning of characters. */ | |
| 1940 p_limit = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, limit); | |
| 1941 ptr2 = (cursor = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos)); | |
| 446 | 1942 /* In this loop, pos + cursor - ptr2 is the surrogate |
| 1943 for pos */ | |
| 1944 while (1) /* use one cursor setting as long as i can */ | |
| 1945 { | |
| 1946 if (direction > 0) /* worth duplicating */ | |
| 1947 { | |
| 1948 /* Use signed comparison if appropriate to make | |
| 1949 cursor+infinity sure to be > p_limit. | |
| 1950 Assuming that the buffer lies in a range of | |
| 1951 addresses that are all "positive" (as ints) | |
| 1952 or all "negative", either kind of comparison | |
| 1953 will work as long as we don't step by | |
| 1954 infinity. So pick the kind that works when | |
| 1955 we do step by infinity. */ | |
| 1956 if ((EMACS_INT) (p_limit + infinity) > | |
| 1957 (EMACS_INT) p_limit) | |
| 1958 while ((EMACS_INT) cursor <= | |
| 1959 (EMACS_INT) p_limit) | |
| 1960 cursor += BM_tab[*cursor]; | |
| 1961 else | |
| 1962 while ((EMACS_UINT) cursor <= | |
| 1963 (EMACS_UINT) p_limit) | |
| 1964 cursor += BM_tab[*cursor]; | |
| 1965 } | |
| 1966 else | |
| 1967 { | |
| 1968 if ((EMACS_INT) (p_limit + infinity) < | |
| 1969 (EMACS_INT) p_limit) | |
| 1970 while ((EMACS_INT) cursor >= | |
| 1971 (EMACS_INT) p_limit) | |
| 1972 cursor += BM_tab[*cursor]; | |
| 1973 else | |
| 1974 while ((EMACS_UINT) cursor >= | |
| 1975 (EMACS_UINT) p_limit) | |
| 1976 cursor += BM_tab[*cursor]; | |
| 1977 } | |
| 1978 /* If you are here, cursor is beyond the end of the | |
| 1979 searched region. This can happen if you match on | |
| 1980 the far character of the pattern, because the | |
| 1981 "stride" of that character is infinity, a number | |
| 1982 able to throw you well beyond the end of the | |
| 1983 search. It can also happen if you fail to match | |
| 1984 within the permitted region and would otherwise | |
| 1985 try a character beyond that region */ | |
| 1986 if ((cursor - p_limit) * direction <= len) | |
| 1987 break; /* a small overrun is genuine */ | |
| 1988 cursor -= infinity; /* large overrun = hit */ | |
| 1989 i = dirlen - direction; | |
| 1990 if (!NILP (trt)) | |
| 1991 { | |
| 1992 while ((i -= direction) + direction != 0) | |
| 1993 { | |
| 1994 #ifdef MULE | |
| 867 | 1995 Ichar ch; |
| 446 | 1996 cursor -= direction; |
| 1997 /* Translate only the last byte of a character. */ | |
| 1998 if ((cursor == tail_end_ptr | |
| 867 | 1999 || ibyte_first_byte_p (cursor[1])) |
| 2000 && (ibyte_first_byte_p (cursor[0]) | |
| 446 | 2001 || (translate_prev_byte == cursor[-1] |
| 867 | 2002 && (ibyte_first_byte_p (translate_prev_byte) |
| 446 | 2003 || translate_anteprev_byte == cursor[-2])))) |
| 2004 ch = simple_translate[*cursor]; | |
| 2005 else | |
| 2006 ch = *cursor; | |
| 2007 if (pat[i] != ch) | |
| 2008 break; | |
| 2009 #else | |
| 2010 if (pat[i] != TRANSLATE (trt, *(cursor -= direction))) | |
| 2011 break; | |
| 2012 #endif | |
| 2013 } | |
| 2014 } | |
| 2015 else | |
| 2016 { | |
| 2017 while ((i -= direction) + direction != 0) | |
| 2018 if (pat[i] != *(cursor -= direction)) | |
| 2019 break; | |
| 2020 } | |
| 2021 cursor += dirlen - i - direction; /* fix cursor */ | |
| 2022 if (i + direction == 0) | |
| 2023 { | |
| 2024 cursor -= direction; | |
| 2025 | |
| 2026 { | |
| 665 | 2027 Bytebpos bytstart = (pos + cursor - ptr2 + |
| 446 | 2028 ((direction > 0) |
| 2029 ? 1 - len : 0)); | |
| 665 | 2030 Charbpos bufstart = bytebpos_to_charbpos (buf, bytstart); |
| 2031 Charbpos bufend = bytebpos_to_charbpos (buf, bytstart + len); | |
| 446 | 2032 |
| 2033 set_search_regs (buf, bufstart, bufend - bufstart); | |
| 2034 } | |
| 2035 | |
| 2036 if ((n -= direction) != 0) | |
| 2037 cursor += dirlen; /* to resume search */ | |
| 2038 else | |
| 2039 return ((direction > 0) | |
| 2040 ? search_regs.end[0] : search_regs.start[0]); | |
| 2041 } | |
| 2042 else | |
| 2043 cursor += stride_for_teases; /* <sigh> we lose - */ | |
| 2044 } | |
| 2045 pos += cursor - ptr2; | |
| 2046 } | |
| 2047 else | |
| 2048 /* Now we'll pick up a clump that has to be done the hard | |
| 2049 way because it covers a discontinuity */ | |
| 2050 { | |
| 428 | 2051 /* XEmacs change: definitions of CEILING_OF and FLOOR_OF |
| 2052 have changed. See buffer.h. */ | |
| 2053 limit = ((direction > 0) | |
| 826 | 2054 ? BYTE_BUF_CEILING_OF (buf, pos - dirlen + 1) - 1 |
| 2055 : BYTE_BUF_FLOOR_OF (buf, pos - dirlen)); | |
| 428 | 2056 limit = ((direction > 0) |
| 446 | 2057 ? min (limit + len, lim - 1) |
| 2058 : max (limit - len, lim)); | |
| 2059 /* LIMIT is now the last value POS can have | |
| 2060 and still be valid for a possible match. */ | |
| 2061 while (1) | |
| 428 | 2062 { |
| 446 | 2063 /* This loop can be coded for space rather than |
| 2064 speed because it will usually run only once. | |
| 2065 (the reach is at most len + 21, and typically | |
| 2066 does not exceed len) */ | |
| 2067 while ((limit - pos) * direction >= 0) | |
| 826 | 2068 /* *not* BYTE_BUF_FETCH_CHAR. We are working here |
| 446 | 2069 with bytes, not characters. */ |
| 826 | 2070 pos += BM_tab[*BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos)]; |
| 446 | 2071 /* now run the same tests to distinguish going off |
| 2072 the end, a match or a phony match. */ | |
| 2073 if ((pos - limit) * direction <= len) | |
| 2074 break; /* ran off the end */ | |
| 2075 /* Found what might be a match. | |
| 2076 Set POS back to last (first if reverse) char pos. */ | |
| 2077 pos -= infinity; | |
| 2078 i = dirlen - direction; | |
| 2079 while ((i -= direction) + direction != 0) | |
| 428 | 2080 { |
| 446 | 2081 #ifdef MULE |
| 867 | 2082 Ichar ch; |
| 2083 Ibyte *ptr; | |
| 446 | 2084 #endif |
| 2085 pos -= direction; | |
| 2086 #ifdef MULE | |
| 826 | 2087 ptr = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos); |
| 446 | 2088 if ((ptr == tail_end_ptr |
| 867 | 2089 || ibyte_first_byte_p (ptr[1])) |
| 2090 && (ibyte_first_byte_p (ptr[0]) | |
| 446 | 2091 || (translate_prev_byte == ptr[-1] |
| 867 | 2092 && (ibyte_first_byte_p (translate_prev_byte) |
| 446 | 2093 || translate_anteprev_byte == ptr[-2])))) |
| 2094 ch = simple_translate[*ptr]; | |
| 428 | 2095 else |
| 446 | 2096 ch = *ptr; |
| 2097 if (pat[i] != ch) | |
| 2098 break; | |
| 2099 | |
| 2100 #else | |
| 826 | 2101 if (pat[i] != |
| 2102 TRANSLATE (trt, | |
| 2103 *BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos))) | |
| 446 | 2104 break; |
| 2105 #endif | |
| 428 | 2106 } |
| 446 | 2107 /* Above loop has moved POS part or all the way back |
| 2108 to the first char pos (last char pos if reverse). | |
| 2109 Set it once again at the last (first if reverse) | |
| 2110 char. */ | |
| 2111 pos += dirlen - i- direction; | |
| 2112 if (i + direction == 0) | |
| 428 | 2113 { |
| 446 | 2114 pos -= direction; |
| 2115 | |
| 2116 { | |
| 665 | 2117 Bytebpos bytstart = (pos + |
| 446 | 2118 ((direction > 0) |
| 2119 ? 1 - len : 0)); | |
| 665 | 2120 Charbpos bufstart = bytebpos_to_charbpos (buf, bytstart); |
| 2121 Charbpos bufend = bytebpos_to_charbpos (buf, bytstart + len); | |
| 446 | 2122 |
| 2123 set_search_regs (buf, bufstart, bufend - bufstart); | |
| 2124 } | |
| 2125 | |
| 2126 if ((n -= direction) != 0) | |
| 2127 pos += dirlen; /* to resume search */ | |
| 428 | 2128 else |
| 446 | 2129 return ((direction > 0) |
| 2130 ? search_regs.end[0] : search_regs.start[0]); | |
| 428 | 2131 } |
| 446 | 2132 else |
| 2133 pos += stride_for_teases; | |
| 2134 } | |
| 428 | 2135 } |
| 446 | 2136 /* We have done one clump. Can we continue? */ |
| 2137 if ((lim - pos) * direction < 0) | |
| 2138 return (0 - n) * direction; | |
| 428 | 2139 } |
| 665 | 2140 return bytebpos_to_charbpos (buf, pos); |
| 428 | 2141 } |
| 2142 | |
| 1024 | 2143 /* Record the whole-match data (beginning BEG and end BEG + LEN) and the |
| 2144 buffer for a match just found. */ | |
| 428 | 2145 |
| 2146 static void | |
| 665 | 2147 set_search_regs (struct buffer *buf, Charbpos beg, Charcount len) |
| 428 | 2148 { |
| 2149 /* Make sure we have registers in which to store | |
| 2150 the match position. */ | |
| 2151 if (search_regs.num_regs == 0) | |
| 2152 { | |
| 2153 search_regs.start = xnew (regoff_t); | |
| 2154 search_regs.end = xnew (regoff_t); | |
| 2155 search_regs.num_regs = 1; | |
| 2156 } | |
| 2157 | |
| 1468 | 2158 clear_search_regs (); |
| 428 | 2159 search_regs.start[0] = beg; |
| 2160 search_regs.end[0] = beg + len; | |
| 793 | 2161 last_thing_searched = wrap_buffer (buf); |
| 428 | 2162 } |
| 2163 | |
| 1468 | 2164 /* Clear search registers so match data will be null. */ |
| 1024 | 2165 |
| 2166 static void | |
| 1468 | 2167 clear_search_regs (void) |
| 1024 | 2168 { |
| 2169 /* This function has been Mule-ized. */ | |
| 2170 int i; | |
| 2171 | |
| 1468 | 2172 for (i = 0; i < search_regs.num_regs; i++) |
| 2173 search_regs.start[i] = search_regs.end[i] = -1; | |
| 1024 | 2174 } |
| 2175 | |
| 428 | 2176 |
| 2177 /* Given a string of words separated by word delimiters, | |
| 442 | 2178 compute a regexp that matches those exact words |
| 2179 separated by arbitrary punctuation. */ | |
| 428 | 2180 |
| 2181 static Lisp_Object | |
| 2182 wordify (Lisp_Object buffer, Lisp_Object string) | |
| 2183 { | |
| 2184 Charcount i, len; | |
| 2185 EMACS_INT punct_count = 0, word_count = 0; | |
| 2186 struct buffer *buf = decode_buffer (buffer, 0); | |
| 826 | 2187 Lisp_Object syntax_table = buf->mirror_syntax_table; |
| 428 | 2188 |
| 2189 CHECK_STRING (string); | |
| 826 | 2190 len = string_char_length (string); |
| 428 | 2191 |
| 2192 for (i = 0; i < len; i++) | |
| 867 | 2193 if (!WORD_SYNTAX_P (syntax_table, string_ichar (string, i))) |
| 428 | 2194 { |
| 2195 punct_count++; | |
| 2196 if (i > 0 && WORD_SYNTAX_P (syntax_table, | |
| 867 | 2197 string_ichar (string, i - 1))) |
| 428 | 2198 word_count++; |
| 2199 } | |
| 867 | 2200 if (WORD_SYNTAX_P (syntax_table, string_ichar (string, len - 1))) |
| 428 | 2201 word_count++; |
| 2202 if (!word_count) return build_string (""); | |
| 2203 | |
| 2204 { | |
| 2205 /* The following value is an upper bound on the amount of storage we | |
| 2206 need. In non-Mule, it is exact. */ | |
| 867 | 2207 Ibyte *storage = |
| 2367 | 2208 alloca_ibytes (XSTRING_LENGTH (string) - punct_count + |
| 428 | 2209 5 * (word_count - 1) + 4); |
| 867 | 2210 Ibyte *o = storage; |
| 428 | 2211 |
| 2212 *o++ = '\\'; | |
| 2213 *o++ = 'b'; | |
| 2214 | |
| 2215 for (i = 0; i < len; i++) | |
| 2216 { | |
| 867 | 2217 Ichar ch = string_ichar (string, i); |
| 428 | 2218 |
| 2219 if (WORD_SYNTAX_P (syntax_table, ch)) | |
| 867 | 2220 o += set_itext_ichar (o, ch); |
| 428 | 2221 else if (i > 0 |
| 2222 && WORD_SYNTAX_P (syntax_table, | |
| 867 | 2223 string_ichar (string, i - 1)) |
| 428 | 2224 && --word_count) |
| 2225 { | |
| 2226 *o++ = '\\'; | |
| 2227 *o++ = 'W'; | |
| 2228 *o++ = '\\'; | |
| 2229 *o++ = 'W'; | |
| 2230 *o++ = '*'; | |
| 2231 } | |
| 2232 } | |
| 2233 | |
| 2234 *o++ = '\\'; | |
| 2235 *o++ = 'b'; | |
| 2236 | |
| 2237 return make_string (storage, o - storage); | |
| 2238 } | |
| 2239 } | |
| 2240 | |
| 2241 DEFUN ("search-backward", Fsearch_backward, 1, 5, "sSearch backward: ", /* | |
| 2242 Search backward from point for STRING. | |
| 2243 Set point to the beginning of the occurrence found, and return point. | |
| 444 | 2244 |
| 2245 Optional second argument LIMIT bounds the search; it is a buffer | |
| 2246 position. The match found must not extend before that position. | |
| 2247 The value nil is equivalent to (point-min). | |
| 2248 | |
| 2249 Optional third argument NOERROR, if t, means just return nil (no | |
| 2250 error) if the search fails. If neither nil nor t, set point to LIMIT | |
| 2251 and return nil. | |
| 2252 | |
| 2253 Optional fourth argument COUNT is a repeat count--search for | |
| 2254 successive occurrences. | |
| 2255 | |
| 428 | 2256 Optional fifth argument BUFFER specifies the buffer to search in and |
| 444 | 2257 defaults to the current buffer. |
| 2258 | |
| 1468 | 2259 When the match is successful, this function modifies the match data |
| 2260 that `match-beginning', `match-end' and `match-data' access; save the | |
| 2261 match data with `match-data' and restore it with `store-match-data' if | |
| 2262 you want to preserve them. If the match fails, the match data from the | |
| 2263 previous success match is preserved. | |
| 2264 | |
| 2265 See also the function `replace-match'. | |
| 428 | 2266 */ |
| 444 | 2267 (string, limit, noerror, count, buffer)) |
| 428 | 2268 { |
| 444 | 2269 return search_command (string, limit, noerror, count, buffer, -1, 0, 0); |
| 428 | 2270 } |
| 2271 | |
| 2272 DEFUN ("search-forward", Fsearch_forward, 1, 5, "sSearch: ", /* | |
| 2273 Search forward from point for STRING. | |
| 2274 Set point to the end of the occurrence found, and return point. | |
| 444 | 2275 |
| 2276 Optional second argument LIMIT bounds the search; it is a buffer | |
| 2277 position. The match found must not extend after that position. The | |
| 2278 value nil is equivalent to (point-max). | |
| 2279 | |
| 2280 Optional third argument NOERROR, if t, means just return nil (no | |
| 2281 error) if the search fails. If neither nil nor t, set point to LIMIT | |
| 2282 and return nil. | |
| 2283 | |
| 2284 Optional fourth argument COUNT is a repeat count--search for | |
| 2285 successive occurrences. | |
| 2286 | |
| 428 | 2287 Optional fifth argument BUFFER specifies the buffer to search in and |
| 444 | 2288 defaults to the current buffer. |
| 2289 | |
| 1468 | 2290 When the match is successful, this function modifies the match data |
| 2291 that `match-beginning', `match-end' and `match-data' access; save the | |
| 2292 match data with `match-data' and restore it with `store-match-data' if | |
| 2293 you want to preserve them. If the match fails, the match data from the | |
| 2294 previous success match is preserved. | |
| 2295 | |
| 2296 See also the function `replace-match'. | |
| 428 | 2297 */ |
| 444 | 2298 (string, limit, noerror, count, buffer)) |
| 428 | 2299 { |
| 444 | 2300 return search_command (string, limit, noerror, count, buffer, 1, 0, 0); |
| 428 | 2301 } |
| 2302 | |
| 2303 DEFUN ("word-search-backward", Fword_search_backward, 1, 5, | |
| 2304 "sWord search backward: ", /* | |
| 2305 Search backward from point for STRING, ignoring differences in punctuation. | |
| 2306 Set point to the beginning of the occurrence found, and return point. | |
| 444 | 2307 |
| 2308 Optional second argument LIMIT bounds the search; it is a buffer | |
| 2309 position. The match found must not extend before that position. | |
| 2310 The value nil is equivalent to (point-min). | |
| 2311 | |
| 2312 Optional third argument NOERROR, if t, means just return nil (no | |
| 2313 error) if the search fails. If neither nil nor t, set point to LIMIT | |
| 2314 and return nil. | |
| 2315 | |
| 2316 Optional fourth argument COUNT is a repeat count--search for | |
| 2317 successive occurrences. | |
| 2318 | |
| 428 | 2319 Optional fifth argument BUFFER specifies the buffer to search in and |
| 444 | 2320 defaults to the current buffer. |
| 2321 | |
| 1468 | 2322 When the match is successful, this function modifies the match data |
| 2323 that `match-beginning', `match-end' and `match-data' access; save the | |
| 2324 match data with `match-data' and restore it with `store-match-data' if | |
| 2325 you want to preserve them. If the match fails, the match data from the | |
| 2326 previous success match is preserved. | |
| 2327 | |
| 2328 See also the function `replace-match'. | |
| 428 | 2329 */ |
| 444 | 2330 (string, limit, noerror, count, buffer)) |
| 428 | 2331 { |
| 444 | 2332 return search_command (wordify (buffer, string), limit, noerror, count, |
| 428 | 2333 buffer, -1, 1, 0); |
| 2334 } | |
| 2335 | |
| 2336 DEFUN ("word-search-forward", Fword_search_forward, 1, 5, "sWord search: ", /* | |
| 2337 Search forward from point for STRING, ignoring differences in punctuation. | |
| 2338 Set point to the end of the occurrence found, and return point. | |
| 444 | 2339 |
| 2340 Optional second argument LIMIT bounds the search; it is a buffer | |
| 2341 position. The match found must not extend after that position. The | |
| 2342 value nil is equivalent to (point-max). | |
| 2343 | |
| 2344 Optional third argument NOERROR, if t, means just return nil (no | |
| 2345 error) if the search fails. If neither nil nor t, set point to LIMIT | |
| 2346 and return nil. | |
| 2347 | |
| 2348 Optional fourth argument COUNT is a repeat count--search for | |
| 2349 successive occurrences. | |
| 2350 | |
| 428 | 2351 Optional fifth argument BUFFER specifies the buffer to search in and |
| 444 | 2352 defaults to the current buffer. |
| 2353 | |
| 1468 | 2354 When the match is successful, this function modifies the match data |
| 2355 that `match-beginning', `match-end' and `match-data' access; save the | |
| 2356 match data with `match-data' and restore it with `store-match-data' if | |
| 2357 you want to preserve them. If the match fails, the match data from the | |
| 2358 previous success match is preserved. | |
| 2359 | |
| 2360 See also the function `replace-match'. | |
| 428 | 2361 */ |
| 444 | 2362 (string, limit, noerror, count, buffer)) |
| 428 | 2363 { |
| 444 | 2364 return search_command (wordify (buffer, string), limit, noerror, count, |
| 428 | 2365 buffer, 1, 1, 0); |
| 2366 } | |
| 2367 | |
| 2368 DEFUN ("re-search-backward", Fre_search_backward, 1, 5, | |
| 2369 "sRE search backward: ", /* | |
| 2370 Search backward from point for match for regular expression REGEXP. | |
| 2371 Set point to the beginning of the match, and return point. | |
| 2372 The match found is the one starting last in the buffer | |
| 2373 and yet ending before the origin of the search. | |
| 444 | 2374 |
| 2375 Optional second argument LIMIT bounds the search; it is a buffer | |
| 2376 position. The match found must not extend before that position. | |
| 2377 The value nil is equivalent to (point-min). | |
| 2378 | |
| 2379 Optional third argument NOERROR, if t, means just return nil (no | |
| 2380 error) if the search fails. If neither nil nor t, set point to LIMIT | |
| 2381 and return nil. | |
| 2382 | |
| 2383 Optional fourth argument COUNT is a repeat count--search for | |
| 2384 successive occurrences. | |
| 2385 | |
| 428 | 2386 Optional fifth argument BUFFER specifies the buffer to search in and |
| 444 | 2387 defaults to the current buffer. |
| 2388 | |
| 1468 | 2389 When the match is successful, this function modifies the match data |
| 2390 that `match-beginning', `match-end' and `match-data' access; save the | |
| 2391 match data with `match-data' and restore it with `store-match-data' if | |
| 2392 you want to preserve them. If the match fails, the match data from the | |
| 2393 previous success match is preserved. | |
| 2394 | |
| 2395 See also the function `replace-match'. | |
| 428 | 2396 */ |
| 444 | 2397 (regexp, limit, noerror, count, buffer)) |
| 428 | 2398 { |
| 444 | 2399 return search_command (regexp, limit, noerror, count, buffer, -1, 1, 0); |
| 428 | 2400 } |
| 2401 | |
| 2402 DEFUN ("re-search-forward", Fre_search_forward, 1, 5, "sRE search: ", /* | |
| 2403 Search forward from point for regular expression REGEXP. | |
| 2404 Set point to the end of the occurrence found, and return point. | |
| 444 | 2405 |
| 2406 Optional second argument LIMIT bounds the search; it is a buffer | |
| 2407 position. The match found must not extend after that position. The | |
| 2408 value nil is equivalent to (point-max). | |
| 2409 | |
| 2410 Optional third argument NOERROR, if t, means just return nil (no | |
| 2411 error) if the search fails. If neither nil nor t, set point to LIMIT | |
| 2412 and return nil. | |
| 2413 | |
| 2414 Optional fourth argument COUNT is a repeat count--search for | |
| 2415 successive occurrences. | |
| 2416 | |
| 428 | 2417 Optional fifth argument BUFFER specifies the buffer to search in and |
| 444 | 2418 defaults to the current buffer. |
| 2419 | |
| 1468 | 2420 When the match is successful, this function modifies the match data |
| 2421 that `match-beginning', `match-end' and `match-data' access; save the | |
| 2422 match data with `match-data' and restore it with `store-match-data' if | |
| 2423 you want to preserve them. If the match fails, the match data from the | |
| 2424 previous success match is preserved. | |
| 2425 | |
| 2426 See also the function `replace-match'. | |
| 428 | 2427 */ |
| 444 | 2428 (regexp, limit, noerror, count, buffer)) |
| 428 | 2429 { |
| 444 | 2430 return search_command (regexp, limit, noerror, count, buffer, 1, 1, 0); |
| 428 | 2431 } |
| 2432 | |
| 2433 DEFUN ("posix-search-backward", Fposix_search_backward, 1, 5, | |
| 2434 "sPosix search backward: ", /* | |
| 2435 Search backward from point for match for regular expression REGEXP. | |
| 2436 Find the longest match in accord with Posix regular expression rules. | |
| 2437 Set point to the beginning of the match, and return point. | |
| 2438 The match found is the one starting last in the buffer | |
| 2439 and yet ending before the origin of the search. | |
| 444 | 2440 |
| 2441 Optional second argument LIMIT bounds the search; it is a buffer | |
| 2442 position. The match found must not extend before that position. | |
| 2443 The value nil is equivalent to (point-min). | |
| 2444 | |
| 2445 Optional third argument NOERROR, if t, means just return nil (no | |
| 2446 error) if the search fails. If neither nil nor t, set point to LIMIT | |
| 2447 and return nil. | |
| 2448 | |
| 2449 Optional fourth argument COUNT is a repeat count--search for | |
| 2450 successive occurrences. | |
| 2451 | |
| 428 | 2452 Optional fifth argument BUFFER specifies the buffer to search in and |
| 444 | 2453 defaults to the current buffer. |
| 2454 | |
| 1468 | 2455 When the match is successful, this function modifies the match data |
| 2456 that `match-beginning', `match-end' and `match-data' access; save the | |
| 2457 match data with `match-data' and restore it with `store-match-data' if | |
| 2458 you want to preserve them. If the match fails, the match data from the | |
| 2459 previous success match is preserved. | |
| 2460 | |
| 2461 See also the function `replace-match'. | |
| 428 | 2462 */ |
| 444 | 2463 (regexp, limit, noerror, count, buffer)) |
| 428 | 2464 { |
| 444 | 2465 return search_command (regexp, limit, noerror, count, buffer, -1, 1, 1); |
| 428 | 2466 } |
| 2467 | |
| 2468 DEFUN ("posix-search-forward", Fposix_search_forward, 1, 5, "sPosix search: ", /* | |
| 2469 Search forward from point for regular expression REGEXP. | |
| 2470 Find the longest match in accord with Posix regular expression rules. | |
| 2471 Set point to the end of the occurrence found, and return point. | |
| 444 | 2472 |
| 2473 Optional second argument LIMIT bounds the search; it is a buffer | |
| 2474 position. The match found must not extend after that position. The | |
| 2475 value nil is equivalent to (point-max). | |
| 2476 | |
| 2477 Optional third argument NOERROR, if t, means just return nil (no | |
| 2478 error) if the search fails. If neither nil nor t, set point to LIMIT | |
| 2479 and return nil. | |
| 2480 | |
| 2481 Optional fourth argument COUNT is a repeat count--search for | |
| 2482 successive occurrences. | |
| 2483 | |
| 428 | 2484 Optional fifth argument BUFFER specifies the buffer to search in and |
| 444 | 2485 defaults to the current buffer. |
| 2486 | |
| 1468 | 2487 When the match is successful, this function modifies the match data |
| 2488 that `match-beginning', `match-end' and `match-data' access; save the | |
| 2489 match data with `match-data' and restore it with `store-match-data' if | |
| 2490 you want to preserve them. If the match fails, the match data from the | |
| 2491 previous success match is preserved. | |
| 2492 | |
| 2493 See also the function `replace-match'. | |
| 428 | 2494 */ |
| 444 | 2495 (regexp, limit, noerror, count, buffer)) |
| 428 | 2496 { |
| 444 | 2497 return search_command (regexp, limit, noerror, count, buffer, 1, 1, 1); |
| 428 | 2498 } |
| 2499 | |
| 2500 | |
| 2501 static Lisp_Object | |
| 2502 free_created_dynarrs (Lisp_Object cons) | |
| 2503 { | |
| 2504 Dynarr_free (get_opaque_ptr (XCAR (cons))); | |
| 2505 Dynarr_free (get_opaque_ptr (XCDR (cons))); | |
| 2506 free_opaque_ptr (XCAR (cons)); | |
| 2507 free_opaque_ptr (XCDR (cons)); | |
| 853 | 2508 free_cons (cons); |
| 428 | 2509 return Qnil; |
| 2510 } | |
| 2511 | |
| 2512 DEFUN ("replace-match", Freplace_match, 1, 5, 0, /* | |
| 444 | 2513 Replace text matched by last search with REPLACEMENT. |
| 4199 | 2514 Leaves point at end of replacement text. |
| 2515 Optional boolean FIXEDCASE inhibits matching case of REPLACEMENT to source. | |
| 2516 Optional boolean LITERAL inhibits interpretation of escape sequences. | |
| 2517 Optional STRING provides the source text to replace. | |
| 2518 Optional STRBUFFER may be a buffer, providing match context, or an integer | |
| 2519 specifying the subexpression to replace. | |
| 2520 | |
| 2521 If FIXEDCASE is non-nil, do not alter case of replacement text. | |
| 428 | 2522 Otherwise maybe capitalize the whole text, or maybe just word initials, |
| 2523 based on the replaced text. | |
| 4199 | 2524 If the replaced text has only capital letters and has at least one |
| 2525 multiletter word, convert REPLACEMENT to all caps. | |
| 428 | 2526 If the replaced text has at least one word starting with a capital letter, |
| 444 | 2527 then capitalize each word in REPLACEMENT. |
| 428 | 2528 |
| 4199 | 2529 If LITERAL is non-nil, insert REPLACEMENT literally. |
| 428 | 2530 Otherwise treat `\\' as special: |
| 444 | 2531 `\\&' in REPLACEMENT means substitute original matched text. |
| 428 | 2532 `\\N' means substitute what matched the Nth `\\(...\\)'. |
| 2533 If Nth parens didn't match, substitute nothing. | |
| 2534 `\\\\' means insert one `\\'. | |
| 2535 `\\u' means upcase the next character. | |
| 2536 `\\l' means downcase the next character. | |
| 2537 `\\U' means begin upcasing all following characters. | |
| 2538 `\\L' means begin downcasing all following characters. | |
| 2539 `\\E' means terminate the effect of any `\\U' or `\\L'. | |
| 2540 Case changes made with `\\u', `\\l', `\\U', and `\\L' override | |
| 2541 all other case changes that may be made in the replaced text. | |
| 4199 | 2542 |
| 2543 If non-nil, STRING is the source string, and a new string with the specified | |
| 2544 replacements is created and returned. Otherwise the current buffer is the | |
| 2545 source text. | |
| 2546 | |
| 2547 If non-nil, STRBUFFER may be an integer, interpreted as the index of the | |
| 2548 subexpression to replace in the source text, or a buffer to provide the | |
| 2549 syntax table and case table. If nil, then the \"subexpression\" is 0, i.e., | |
| 2550 the whole match, and the current buffer provides the syntax and case tables. | |
| 2551 If STRING is nil, STRBUFFER must be nil or an integer. | |
| 2552 | |
| 2553 Specifying a subexpression is only useful after a regular expression match, | |
| 2554 since a fixed string search has no non-trivial subexpressions. | |
| 2555 | |
| 2556 It is not possible to specify both a buffer and a subexpression. If that is | |
| 2557 desired, the idiom `(with-current-buffer BUFFER (replace-match ... INTEGER))' | |
| 2558 may be appropriate. | |
| 2559 | |
| 2560 If STRING is nil but the last thing matched (or searched) was a string, or | |
| 2561 STRING is a string but the last thing matched was a buffer, an | |
| 2562 `invalid-argument' error will be signaled. (XEmacs does not check that the | |
| 2563 last thing searched is the source string, but it is not useful to use a | |
| 2564 different string as source.) | |
| 2565 | |
| 2566 If no match (including searches) has been successful or the requested | |
| 1468 | 2567 subexpression was not matched, an `args-out-of-range' error will be |
| 2568 signaled. (If no match has ever been conducted in this instance of | |
| 2569 XEmacs, an `invalid-operation' error will be signaled. This is very | |
| 2570 rare.) | |
| 428 | 2571 */ |
| 444 | 2572 (replacement, fixedcase, literal, string, strbuffer)) |
| 428 | 2573 { |
| 2574 /* This function can GC */ | |
| 2575 enum { nochange, all_caps, cap_initial } case_action; | |
| 665 | 2576 Charbpos pos, last; |
| 428 | 2577 int some_multiletter_word; |
| 2578 int some_lowercase; | |
| 2579 int some_uppercase; | |
| 2580 int some_nonuppercase_initial; | |
| 867 | 2581 Ichar c, prevc; |
| 428 | 2582 Charcount inslen; |
| 2583 struct buffer *buf; | |
| 826 | 2584 Lisp_Object syntax_table; |
| 428 | 2585 int mc_count; |
| 2586 Lisp_Object buffer; | |
| 2587 int_dynarr *ul_action_dynarr = 0; | |
| 2588 int_dynarr *ul_pos_dynarr = 0; | |
| 502 | 2589 int sub = 0; |
| 428 | 2590 int speccount; |
| 2591 | |
| 444 | 2592 CHECK_STRING (replacement); |
| 428 | 2593 |
| 4199 | 2594 /* Because GNU decided to be incompatible here, we support the following |
| 2595 baroque and bogus API for the STRING and STRBUFFER arguments: | |
| 2596 types interpretations | |
| 2597 STRING STRBUFFER STRING STRBUFFER | |
| 2598 nil nil none 0 = index of subexpression to replace | |
| 2599 nil integer none index of subexpression to replace | |
| 2600 nil other ***** error ***** | |
| 2601 string nil source current buffer provides syntax table | |
| 2602 subexpression = 0 (whole match) | |
| 2603 string buffer source buffer providing syntax table | |
| 2604 subexpression = 0 (whole match) | |
| 2605 string integer source current buffer provides syntax table | |
| 2606 subexpression = STRBUFFER | |
| 2607 string other ***** error ***** | |
| 2608 */ | |
| 2609 | |
| 2610 /* Do STRBUFFER first; if STRING is nil, we'll overwrite BUF and BUFFER. */ | |
| 2611 | |
| 2612 /* If the match data were abstracted into a special "match data" type | |
| 2613 instead of the typical half-assed "let the implementation be visible" | |
| 2614 form it's in, we could extend it to include the last string matched | |
| 2615 and the buffer used for that matching. But of course we can't change | |
| 2616 it as it is. | |
| 2617 */ | |
| 2618 if (NILP (strbuffer) || BUFFERP (strbuffer)) | |
| 2619 { | |
| 2620 buf = decode_buffer (strbuffer, 0); | |
| 2621 } | |
| 2622 else if (!NILP (strbuffer)) | |
| 2623 { | |
| 2624 CHECK_INT (strbuffer); | |
| 2625 sub = XINT (strbuffer); | |
| 2626 if (sub < 0 || sub >= (int) search_regs.num_regs) | |
| 2627 invalid_argument ("match data register invalid", strbuffer); | |
| 2628 if (search_regs.start[sub] < 0) | |
| 2629 invalid_argument ("match data register not set", strbuffer); | |
| 2630 buf = current_buffer; | |
| 2631 } | |
| 2632 else | |
| 2633 invalid_argument ("STRBUFFER must be nil, a buffer, or an integer", | |
| 2634 strbuffer); | |
| 2635 buffer = wrap_buffer (buf); | |
| 2636 | |
| 428 | 2637 if (! NILP (string)) |
| 2638 { | |
| 2639 CHECK_STRING (string); | |
| 2640 if (!EQ (last_thing_searched, Qt)) | |
| 4199 | 2641 invalid_argument ("last thing matched was not a string", Qunbound); |
| 428 | 2642 } |
| 2643 else | |
| 2644 { | |
| 2645 if (!BUFFERP (last_thing_searched)) | |
| 4199 | 2646 invalid_argument ("last thing matched was not a buffer", Qunbound); |
| 428 | 2647 buffer = last_thing_searched; |
| 2648 buf = XBUFFER (buffer); | |
| 2649 } | |
| 2650 | |
| 826 | 2651 syntax_table = buf->mirror_syntax_table; |
| 428 | 2652 |
| 2653 case_action = nochange; /* We tried an initialization */ | |
| 2654 /* but some C compilers blew it */ | |
| 2655 | |
| 2656 if (search_regs.num_regs == 0) | |
| 826 | 2657 signal_error (Qinvalid_operation, |
| 2658 "replace-match called before any match found", Qunbound); | |
| 428 | 2659 |
| 2660 if (NILP (string)) | |
| 2661 { | |
| 469 | 2662 if (search_regs.start[sub] < BUF_BEGV (buf) |
| 2663 || search_regs.start[sub] > search_regs.end[sub] | |
| 2664 || search_regs.end[sub] > BUF_ZV (buf)) | |
| 2665 args_out_of_range (make_int (search_regs.start[sub]), | |
| 2666 make_int (search_regs.end[sub])); | |
| 428 | 2667 } |
| 2668 else | |
| 2669 { | |
| 2670 if (search_regs.start[0] < 0 | |
| 2671 || search_regs.start[0] > search_regs.end[0] | |
| 826 | 2672 || search_regs.end[0] > string_char_length (string)) |
| 428 | 2673 args_out_of_range (make_int (search_regs.start[0]), |
| 2674 make_int (search_regs.end[0])); | |
| 2675 } | |
| 2676 | |
| 2677 if (NILP (fixedcase)) | |
| 2678 { | |
| 2679 /* Decide how to casify by examining the matched text. */ | |
| 2680 | |
| 707 | 2681 last = search_regs.end[sub]; |
| 428 | 2682 prevc = '\n'; |
| 2683 case_action = all_caps; | |
| 2684 | |
| 2685 /* some_multiletter_word is set nonzero if any original word | |
| 2686 is more than one letter long. */ | |
| 2687 some_multiletter_word = 0; | |
| 2688 some_lowercase = 0; | |
| 2689 some_nonuppercase_initial = 0; | |
| 2690 some_uppercase = 0; | |
| 2691 | |
| 707 | 2692 for (pos = search_regs.start[sub]; pos < last; pos++) |
| 428 | 2693 { |
| 2694 if (NILP (string)) | |
| 2695 c = BUF_FETCH_CHAR (buf, pos); | |
| 2696 else | |
| 867 | 2697 c = string_ichar (string, pos); |
| 428 | 2698 |
| 2699 if (LOWERCASEP (buf, c)) | |
| 2700 { | |
| 2701 /* Cannot be all caps if any original char is lower case */ | |
| 2702 | |
| 2703 some_lowercase = 1; | |
| 2704 if (!WORD_SYNTAX_P (syntax_table, prevc)) | |
| 2705 some_nonuppercase_initial = 1; | |
| 2706 else | |
| 2707 some_multiletter_word = 1; | |
| 2708 } | |
| 2709 else if (!NOCASEP (buf, c)) | |
| 2710 { | |
| 2711 some_uppercase = 1; | |
| 2712 if (!WORD_SYNTAX_P (syntax_table, prevc)) | |
| 2713 ; | |
| 2714 else | |
| 2715 some_multiletter_word = 1; | |
| 2716 } | |
| 2717 else | |
| 2718 { | |
| 2719 /* If the initial is a caseless word constituent, | |
| 2720 treat that like a lowercase initial. */ | |
| 2721 if (!WORD_SYNTAX_P (syntax_table, prevc)) | |
| 2722 some_nonuppercase_initial = 1; | |
| 2723 } | |
| 2724 | |
| 2725 prevc = c; | |
| 2726 } | |
| 2727 | |
| 2728 /* Convert to all caps if the old text is all caps | |
| 2729 and has at least one multiletter word. */ | |
| 2730 if (! some_lowercase && some_multiletter_word) | |
| 2731 case_action = all_caps; | |
| 2732 /* Capitalize each word, if the old text has all capitalized words. */ | |
| 2733 else if (!some_nonuppercase_initial && some_multiletter_word) | |
| 2734 case_action = cap_initial; | |
| 2735 else if (!some_nonuppercase_initial && some_uppercase) | |
| 2736 /* Should x -> yz, operating on X, give Yz or YZ? | |
| 2737 We'll assume the latter. */ | |
| 2738 case_action = all_caps; | |
| 2739 else | |
| 2740 case_action = nochange; | |
| 2741 } | |
| 2742 | |
| 2743 /* Do replacement in a string. */ | |
| 2744 if (!NILP (string)) | |
| 2745 { | |
| 2746 Lisp_Object before, after; | |
| 2747 | |
| 2748 speccount = specpdl_depth (); | |
| 4199 | 2749 before = Fsubstring (string, Qzero, make_int (search_regs.start[sub])); |
| 2750 after = Fsubstring (string, make_int (search_regs.end[sub]), Qnil); | |
| 428 | 2751 |
| 444 | 2752 /* Do case substitution into REPLACEMENT if desired. */ |
| 428 | 2753 if (NILP (literal)) |
| 2754 { | |
| 826 | 2755 Charcount stlen = string_char_length (replacement); |
| 428 | 2756 Charcount strpos; |
| 2757 /* XEmacs change: rewrote this loop somewhat to make it | |
| 2758 cleaner. Also added \U, \E, etc. */ | |
| 2759 Charcount literal_start = 0; | |
| 2760 /* We build up the substituted string in ACCUM. */ | |
| 2761 Lisp_Object accum; | |
| 2762 | |
| 2763 accum = Qnil; | |
| 2764 | |
| 2765 /* OK, the basic idea here is that we scan through the | |
| 2766 replacement string until we find a backslash, which | |
| 2767 represents a substring of the original string to be | |
| 2768 substituted. We then append onto ACCUM the literal | |
| 2769 text before the backslash (LASTPOS marks the | |
| 2770 beginning of this) followed by the substring of the | |
| 2771 original string that needs to be inserted. */ | |
| 2772 for (strpos = 0; strpos < stlen; strpos++) | |
| 2773 { | |
| 2774 /* If LITERAL_END is set, we've encountered a backslash | |
| 2775 (the end of literal text to be inserted). */ | |
| 2776 Charcount literal_end = -1; | |
| 2777 /* If SUBSTART is set, we need to also insert the | |
| 2778 text from SUBSTART to SUBEND in the original string. */ | |
| 2779 Charcount substart = -1; | |
| 2780 Charcount subend = -1; | |
| 2781 | |
| 867 | 2782 c = string_ichar (replacement, strpos); |
| 428 | 2783 if (c == '\\' && strpos < stlen - 1) |
| 2784 { | |
| 867 | 2785 c = string_ichar (replacement, ++strpos); |
| 428 | 2786 if (c == '&') |
| 2787 { | |
| 2788 literal_end = strpos - 1; | |
| 2789 substart = search_regs.start[0]; | |
| 2790 subend = search_regs.end[0]; | |
| 2791 } | |
| 4199 | 2792 /* #### This logic is totally broken, |
| 2793 since we can have backrefs like "\99", right? */ | |
| 428 | 2794 else if (c >= '1' && c <= '9' && |
| 2795 c <= search_regs.num_regs + '0') | |
| 2796 { | |
| 2797 if (search_regs.start[c - '0'] >= 0) | |
| 2798 { | |
| 2799 literal_end = strpos - 1; | |
| 2800 substart = search_regs.start[c - '0']; | |
| 2801 subend = search_regs.end[c - '0']; | |
| 2802 } | |
| 2803 } | |
| 2804 else if (c == 'U' || c == 'u' || c == 'L' || c == 'l' || | |
| 2805 c == 'E') | |
| 2806 { | |
| 2807 /* Keep track of all case changes requested, but don't | |
| 2808 make them now. Do them later so we override | |
| 2809 everything else. */ | |
| 2810 if (!ul_pos_dynarr) | |
| 2811 { | |
| 2812 ul_pos_dynarr = Dynarr_new (int); | |
| 2813 ul_action_dynarr = Dynarr_new (int); | |
| 2814 record_unwind_protect | |
| 2815 (free_created_dynarrs, | |
| 2816 noseeum_cons | |
| 2817 (make_opaque_ptr (ul_pos_dynarr), | |
| 2818 make_opaque_ptr (ul_action_dynarr))); | |
| 2819 } | |
| 2820 literal_end = strpos - 1; | |
| 2821 Dynarr_add (ul_pos_dynarr, | |
| 2822 (!NILP (accum) | |
| 826 | 2823 ? string_char_length (accum) |
| 428 | 2824 : 0) + (literal_end - literal_start)); |
| 2825 Dynarr_add (ul_action_dynarr, c); | |
| 2826 } | |
| 2827 else if (c == '\\') | |
| 2828 /* So we get just one backslash. */ | |
| 2829 literal_end = strpos; | |
| 2830 } | |
| 2831 if (literal_end >= 0) | |
| 2832 { | |
| 2833 Lisp_Object literal_text = Qnil; | |
| 2834 Lisp_Object substring = Qnil; | |
| 2835 if (literal_end != literal_start) | |
| 444 | 2836 literal_text = Fsubstring (replacement, |
| 428 | 2837 make_int (literal_start), |
| 2838 make_int (literal_end)); | |
| 2839 if (substart >= 0 && subend != substart) | |
| 2840 substring = Fsubstring (string, | |
| 2841 make_int (substart), | |
| 2842 make_int (subend)); | |
| 2843 if (!NILP (literal_text) || !NILP (substring)) | |
| 2844 accum = concat3 (accum, literal_text, substring); | |
| 2845 literal_start = strpos + 1; | |
| 2846 } | |
| 2847 } | |
| 2848 | |
| 2849 if (strpos != literal_start) | |
| 2850 /* some literal text at end to be inserted */ | |
| 444 | 2851 replacement = concat2 (accum, Fsubstring (replacement, |
| 2852 make_int (literal_start), | |
| 2853 make_int (strpos))); | |
| 428 | 2854 else |
| 444 | 2855 replacement = accum; |
| 428 | 2856 } |
| 2857 | |
| 444 | 2858 /* replacement can be nil. */ |
| 2859 if (NILP (replacement)) | |
| 2860 replacement = build_string (""); | |
| 2861 | |
| 428 | 2862 if (case_action == all_caps) |
| 444 | 2863 replacement = Fupcase (replacement, buffer); |
| 428 | 2864 else if (case_action == cap_initial) |
| 444 | 2865 replacement = Fupcase_initials (replacement, buffer); |
| 428 | 2866 |
| 2867 /* Now finally, we need to process the \U's, \E's, etc. */ | |
| 2868 if (ul_pos_dynarr) | |
| 2869 { | |
| 2870 int i = 0; | |
| 2871 int cur_action = 'E'; | |
| 826 | 2872 Charcount stlen = string_char_length (replacement); |
| 428 | 2873 Charcount strpos; |
| 2874 | |
| 2875 for (strpos = 0; strpos < stlen; strpos++) | |
| 2876 { | |
| 867 | 2877 Ichar curchar = string_ichar (replacement, strpos); |
| 2878 Ichar newchar = -1; | |
| 428 | 2879 if (i < Dynarr_length (ul_pos_dynarr) && |
| 2880 strpos == Dynarr_at (ul_pos_dynarr, i)) | |
| 2881 { | |
| 2882 int new_action = Dynarr_at (ul_action_dynarr, i); | |
| 2883 i++; | |
| 2884 if (new_action == 'u') | |
| 2885 newchar = UPCASE (buf, curchar); | |
| 2886 else if (new_action == 'l') | |
| 2887 newchar = DOWNCASE (buf, curchar); | |
| 2888 else | |
| 2889 cur_action = new_action; | |
| 2890 } | |
| 2891 if (newchar == -1) | |
| 2892 { | |
| 2893 if (cur_action == 'U') | |
| 2894 newchar = UPCASE (buf, curchar); | |
| 2895 else if (cur_action == 'L') | |
| 2896 newchar = DOWNCASE (buf, curchar); | |
| 2897 else | |
| 2898 newchar = curchar; | |
| 2899 } | |
| 2900 if (newchar != curchar) | |
| 793 | 2901 set_string_char (replacement, strpos, newchar); |
| 428 | 2902 } |
| 2903 } | |
| 2904 | |
| 2905 /* frees the Dynarrs if necessary. */ | |
| 771 | 2906 unbind_to (speccount); |
| 444 | 2907 return concat3 (before, replacement, after); |
| 428 | 2908 } |
| 2909 | |
| 707 | 2910 mc_count = begin_multiple_change (buf, search_regs.start[sub], |
| 2911 search_regs.end[sub]); | |
| 428 | 2912 |
| 2913 /* begin_multiple_change() records an unwind-protect, so we need to | |
| 2914 record this value now. */ | |
| 2915 speccount = specpdl_depth (); | |
| 2916 | |
| 2917 /* We insert the replacement text before the old text, and then | |
| 2918 delete the original text. This means that markers at the | |
| 2919 beginning or end of the original will float to the corresponding | |
| 2920 position in the replacement. */ | |
| 707 | 2921 BUF_SET_PT (buf, search_regs.start[sub]); |
| 428 | 2922 if (!NILP (literal)) |
| 444 | 2923 Finsert (1, &replacement); |
| 428 | 2924 else |
| 2925 { | |
| 826 | 2926 Charcount stlen = string_char_length (replacement); |
| 428 | 2927 Charcount strpos; |
| 2928 struct gcpro gcpro1; | |
| 444 | 2929 GCPRO1 (replacement); |
| 428 | 2930 for (strpos = 0; strpos < stlen; strpos++) |
| 2931 { | |
| 707 | 2932 /* on the first iteration assert(offset==0), |
| 2933 exactly complementing BUF_SET_PT() above. | |
| 2934 During the loop, it keeps track of the amount inserted. | |
| 2935 */ | |
| 2936 Charcount offset = BUF_PT (buf) - search_regs.start[sub]; | |
| 428 | 2937 |
| 867 | 2938 c = string_ichar (replacement, strpos); |
| 428 | 2939 if (c == '\\' && strpos < stlen - 1) |
| 2940 { | |
| 707 | 2941 /* XXX FIXME: replacing just a substring non-literally |
| 2942 using backslash refs to the match looks dangerous. But | |
| 2943 <15366.18513.698042.156573@ns.caldera.de> from Torsten Duwe | |
| 2944 <duwe@caldera.de> claims Finsert_buffer_substring already | |
| 2945 handles this correctly. | |
| 2946 */ | |
| 867 | 2947 c = string_ichar (replacement, ++strpos); |
| 428 | 2948 if (c == '&') |
| 2949 Finsert_buffer_substring | |
| 2950 (buffer, | |
| 2951 make_int (search_regs.start[0] + offset), | |
| 2952 make_int (search_regs.end[0] + offset)); | |
| 4199 | 2953 /* #### This logic is totally broken, |
| 2954 since we can have backrefs like "\99", right? */ | |
| 428 | 2955 else if (c >= '1' && c <= '9' && |
| 2956 c <= search_regs.num_regs + '0') | |
| 2957 { | |
| 2958 if (search_regs.start[c - '0'] >= 1) | |
| 2959 Finsert_buffer_substring | |
| 2960 (buffer, | |
| 2961 make_int (search_regs.start[c - '0'] + offset), | |
| 2962 make_int (search_regs.end[c - '0'] + offset)); | |
| 2963 } | |
| 2964 else if (c == 'U' || c == 'u' || c == 'L' || c == 'l' || | |
| 2965 c == 'E') | |
| 2966 { | |
| 2967 /* Keep track of all case changes requested, but don't | |
| 2968 make them now. Do them later so we override | |
| 2969 everything else. */ | |
| 2970 if (!ul_pos_dynarr) | |
| 2971 { | |
| 2972 ul_pos_dynarr = Dynarr_new (int); | |
| 2973 ul_action_dynarr = Dynarr_new (int); | |
| 2974 record_unwind_protect | |
| 2975 (free_created_dynarrs, | |
| 2976 Fcons (make_opaque_ptr (ul_pos_dynarr), | |
| 2977 make_opaque_ptr (ul_action_dynarr))); | |
| 2978 } | |
| 2979 Dynarr_add (ul_pos_dynarr, BUF_PT (buf)); | |
| 2980 Dynarr_add (ul_action_dynarr, c); | |
| 2981 } | |
| 2982 else | |
| 2983 buffer_insert_emacs_char (buf, c); | |
| 2984 } | |
| 2985 else | |
| 2986 buffer_insert_emacs_char (buf, c); | |
| 2987 } | |
| 2988 UNGCPRO; | |
| 2989 } | |
| 2990 | |
| 707 | 2991 inslen = BUF_PT (buf) - (search_regs.start[sub]); |
| 2992 buffer_delete_range (buf, search_regs.start[sub] + inslen, | |
| 2993 search_regs.end[sub] + inslen, 0); | |
| 428 | 2994 |
| 2995 if (case_action == all_caps) | |
| 2996 Fupcase_region (make_int (BUF_PT (buf) - inslen), | |
| 2997 make_int (BUF_PT (buf)), buffer); | |
| 2998 else if (case_action == cap_initial) | |
| 2999 Fupcase_initials_region (make_int (BUF_PT (buf) - inslen), | |
| 3000 make_int (BUF_PT (buf)), buffer); | |
| 3001 | |
| 3002 /* Now go through and make all the case changes that were requested | |
| 3003 in the replacement string. */ | |
| 3004 if (ul_pos_dynarr) | |
| 3005 { | |
| 665 | 3006 Charbpos eend = BUF_PT (buf); |
| 428 | 3007 int i = 0; |
| 3008 int cur_action = 'E'; | |
| 3009 | |
| 3010 for (pos = BUF_PT (buf) - inslen; pos < eend; pos++) | |
| 3011 { | |
| 867 | 3012 Ichar curchar = BUF_FETCH_CHAR (buf, pos); |
| 3013 Ichar newchar = -1; | |
| 428 | 3014 if (i < Dynarr_length (ul_pos_dynarr) && |
| 3015 pos == Dynarr_at (ul_pos_dynarr, i)) | |
| 3016 { | |
| 3017 int new_action = Dynarr_at (ul_action_dynarr, i); | |
| 3018 i++; | |
| 3019 if (new_action == 'u') | |
| 3020 newchar = UPCASE (buf, curchar); | |
| 3021 else if (new_action == 'l') | |
| 3022 newchar = DOWNCASE (buf, curchar); | |
| 3023 else | |
| 3024 cur_action = new_action; | |
| 3025 } | |
| 3026 if (newchar == -1) | |
| 3027 { | |
| 3028 if (cur_action == 'U') | |
| 3029 newchar = UPCASE (buf, curchar); | |
| 3030 else if (cur_action == 'L') | |
| 3031 newchar = DOWNCASE (buf, curchar); | |
| 3032 else | |
| 3033 newchar = curchar; | |
| 3034 } | |
| 3035 if (newchar != curchar) | |
| 3036 buffer_replace_char (buf, pos, newchar, 0, 0); | |
| 3037 } | |
| 3038 } | |
| 3039 | |
| 3040 /* frees the Dynarrs if necessary. */ | |
| 771 | 3041 unbind_to (speccount); |
| 428 | 3042 end_multiple_change (buf, mc_count); |
| 3043 | |
| 3044 return Qnil; | |
| 3045 } | |
| 3046 | |
| 3047 static Lisp_Object | |
| 3048 match_limit (Lisp_Object num, int beginningp) | |
| 3049 { | |
| 3050 int n; | |
| 3051 | |
| 3052 CHECK_INT (num); | |
| 3053 n = XINT (num); | |
| 3054 if (n < 0 || n >= search_regs.num_regs) | |
| 3055 args_out_of_range (num, make_int (search_regs.num_regs)); | |
| 3056 if (search_regs.num_regs == 0 || | |
| 3057 search_regs.start[n] < 0) | |
| 3058 return Qnil; | |
| 3059 return make_int (beginningp ? search_regs.start[n] : search_regs.end[n]); | |
| 3060 } | |
| 3061 | |
| 3062 DEFUN ("match-beginning", Fmatch_beginning, 1, 1, 0, /* | |
| 3063 Return position of start of text matched by last regexp search. | |
| 3064 NUM, specifies which parenthesized expression in the last regexp. | |
| 3065 Value is nil if NUMth pair didn't match, or there were less than NUM pairs. | |
| 3066 Zero means the entire text matched by the whole regexp or whole string. | |
| 3067 */ | |
| 3068 (num)) | |
| 3069 { | |
| 3070 return match_limit (num, 1); | |
| 3071 } | |
| 3072 | |
| 3073 DEFUN ("match-end", Fmatch_end, 1, 1, 0, /* | |
| 3074 Return position of end of text matched by last regexp search. | |
| 3075 NUM specifies which parenthesized expression in the last regexp. | |
| 3076 Value is nil if NUMth pair didn't match, or there were less than NUM pairs. | |
| 3077 Zero means the entire text matched by the whole regexp or whole string. | |
| 3078 */ | |
| 3079 (num)) | |
| 3080 { | |
| 3081 return match_limit (num, 0); | |
| 3082 } | |
| 3083 | |
| 3084 DEFUN ("match-data", Fmatch_data, 0, 2, 0, /* | |
| 3085 Return a list containing all info on what the last regexp search matched. | |
| 3086 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'. | |
| 3087 All the elements are markers or nil (nil if the Nth pair didn't match) | |
| 3088 if the last match was on a buffer; integers or nil if a string was matched. | |
| 3089 Use `store-match-data' to reinstate the data in this list. | |
| 3090 | |
| 3091 If INTEGERS (the optional first argument) is non-nil, always use integers | |
| 3092 \(rather than markers) to represent buffer positions. | |
| 3093 If REUSE is a list, reuse it as part of the value. If REUSE is long enough | |
| 3094 to hold all the values, and if INTEGERS is non-nil, no consing is done. | |
| 3095 */ | |
| 3096 (integers, reuse)) | |
| 3097 { | |
| 3098 Lisp_Object tail, prev; | |
| 3099 Lisp_Object *data; | |
| 3100 int i; | |
| 3101 Charcount len; | |
| 3102 | |
| 3103 if (NILP (last_thing_searched)) | |
| 563 | 3104 /*error ("match-data called before any match found", Qunbound);*/ |
| 428 | 3105 return Qnil; |
| 3106 | |
| 3107 data = alloca_array (Lisp_Object, 2 * search_regs.num_regs); | |
| 3108 | |
| 3109 len = -1; | |
| 3110 for (i = 0; i < search_regs.num_regs; i++) | |
| 3111 { | |
| 665 | 3112 Charbpos start = search_regs.start[i]; |
| 428 | 3113 if (start >= 0) |
| 3114 { | |
| 3115 if (EQ (last_thing_searched, Qt) | |
| 3116 || !NILP (integers)) | |
| 3117 { | |
| 3118 data[2 * i] = make_int (start); | |
| 3119 data[2 * i + 1] = make_int (search_regs.end[i]); | |
| 3120 } | |
| 3121 else if (BUFFERP (last_thing_searched)) | |
| 3122 { | |
| 3123 data[2 * i] = Fmake_marker (); | |
| 3124 Fset_marker (data[2 * i], | |
| 3125 make_int (start), | |
| 3126 last_thing_searched); | |
| 3127 data[2 * i + 1] = Fmake_marker (); | |
| 3128 Fset_marker (data[2 * i + 1], | |
| 3129 make_int (search_regs.end[i]), | |
| 3130 last_thing_searched); | |
| 3131 } | |
| 3132 else | |
| 3133 /* last_thing_searched must always be Qt, a buffer, or Qnil. */ | |
| 2500 | 3134 ABORT (); |
| 428 | 3135 |
| 3136 len = i; | |
| 3137 } | |
| 3138 else | |
| 3139 data[2 * i] = data [2 * i + 1] = Qnil; | |
| 3140 } | |
| 3141 if (!CONSP (reuse)) | |
| 3142 return Flist (2 * len + 2, data); | |
| 3143 | |
| 3144 /* If REUSE is a list, store as many value elements as will fit | |
| 3145 into the elements of REUSE. */ | |
| 3146 for (prev = Qnil, i = 0, tail = reuse; CONSP (tail); i++, tail = XCDR (tail)) | |
| 3147 { | |
| 3148 if (i < 2 * len + 2) | |
| 3149 XCAR (tail) = data[i]; | |
| 3150 else | |
| 3151 XCAR (tail) = Qnil; | |
| 3152 prev = tail; | |
| 3153 } | |
| 3154 | |
| 3155 /* If we couldn't fit all value elements into REUSE, | |
| 3156 cons up the rest of them and add them to the end of REUSE. */ | |
| 3157 if (i < 2 * len + 2) | |
| 3158 XCDR (prev) = Flist (2 * len + 2 - i, data + i); | |
| 3159 | |
| 3160 return reuse; | |
| 3161 } | |
| 3162 | |
| 3163 | |
| 3164 DEFUN ("store-match-data", Fstore_match_data, 1, 1, 0, /* | |
| 3165 Set internal data on last search match from elements of LIST. | |
| 1468 | 3166 LIST should have been created by calling `match-data' previously, |
| 3167 or be nil, to clear the internal match data. | |
| 428 | 3168 */ |
| 3169 (list)) | |
| 3170 { | |
| 3171 REGISTER int i; | |
| 3172 REGISTER Lisp_Object marker; | |
| 3173 int num_regs; | |
| 3174 int length; | |
| 3175 | |
| 853 | 3176 /* Some FSF junk with running_asynch_code, to preserve the match |
| 3177 data. Not necessary because we don't call process filters | |
| 3178 asynchronously (i.e. from within QUIT). */ | |
| 428 | 3179 |
| 3180 CONCHECK_LIST (list); | |
| 3181 | |
| 3182 /* Unless we find a marker with a buffer in LIST, assume that this | |
| 3183 match data came from a string. */ | |
| 3184 last_thing_searched = Qt; | |
| 3185 | |
| 3186 /* Allocate registers if they don't already exist. */ | |
| 3187 length = XINT (Flength (list)) / 2; | |
| 3188 num_regs = search_regs.num_regs; | |
| 3189 | |
| 3190 if (length > num_regs) | |
| 3191 { | |
| 3192 if (search_regs.num_regs == 0) | |
| 3193 { | |
| 3194 search_regs.start = xnew_array (regoff_t, length); | |
| 3195 search_regs.end = xnew_array (regoff_t, length); | |
| 3196 } | |
| 3197 else | |
| 3198 { | |
| 3199 XREALLOC_ARRAY (search_regs.start, regoff_t, length); | |
| 3200 XREALLOC_ARRAY (search_regs.end, regoff_t, length); | |
| 3201 } | |
| 3202 | |
| 3203 search_regs.num_regs = length; | |
| 3204 } | |
| 3205 | |
| 3206 for (i = 0; i < num_regs; i++) | |
| 3207 { | |
| 3208 marker = Fcar (list); | |
| 3209 if (NILP (marker)) | |
| 3210 { | |
| 3211 search_regs.start[i] = -1; | |
| 3212 list = Fcdr (list); | |
| 3213 } | |
| 3214 else | |
| 3215 { | |
| 3216 if (MARKERP (marker)) | |
| 3217 { | |
| 3218 if (XMARKER (marker)->buffer == 0) | |
| 3219 marker = Qzero; | |
| 3220 else | |
| 793 | 3221 last_thing_searched = wrap_buffer (XMARKER (marker)->buffer); |
| 428 | 3222 } |
| 3223 | |
| 3224 CHECK_INT_COERCE_MARKER (marker); | |
| 3225 search_regs.start[i] = XINT (marker); | |
| 3226 list = Fcdr (list); | |
| 3227 | |
| 3228 marker = Fcar (list); | |
| 3229 if (MARKERP (marker) && XMARKER (marker)->buffer == 0) | |
| 3230 marker = Qzero; | |
| 3231 | |
| 3232 CHECK_INT_COERCE_MARKER (marker); | |
| 3233 search_regs.end[i] = XINT (marker); | |
| 3234 } | |
| 3235 list = Fcdr (list); | |
| 3236 } | |
| 3237 | |
| 3238 return Qnil; | |
| 3239 } | |
| 3240 | |
| 3241 /* Quote a string to inactivate reg-expr chars */ | |
| 3242 | |
| 3243 DEFUN ("regexp-quote", Fregexp_quote, 1, 1, 0, /* | |
| 3244 Return a regexp string which matches exactly STRING and nothing else. | |
| 3245 */ | |
| 444 | 3246 (string)) |
| 428 | 3247 { |
| 867 | 3248 REGISTER Ibyte *in, *out, *end; |
| 3249 REGISTER Ibyte *temp; | |
| 428 | 3250 |
| 444 | 3251 CHECK_STRING (string); |
| 428 | 3252 |
| 2367 | 3253 temp = alloca_ibytes (XSTRING_LENGTH (string) * 2); |
| 428 | 3254 |
| 3255 /* Now copy the data into the new string, inserting escapes. */ | |
| 3256 | |
| 444 | 3257 in = XSTRING_DATA (string); |
| 3258 end = in + XSTRING_LENGTH (string); | |
| 428 | 3259 out = temp; |
| 3260 | |
| 3261 while (in < end) | |
| 3262 { | |
| 867 | 3263 Ichar c = itext_ichar (in); |
| 428 | 3264 |
| 3265 if (c == '[' || c == ']' | |
| 3266 || c == '*' || c == '.' || c == '\\' | |
| 3267 || c == '?' || c == '+' | |
| 3268 || c == '^' || c == '$') | |
| 3269 *out++ = '\\'; | |
| 867 | 3270 out += set_itext_ichar (out, c); |
| 3271 INC_IBYTEPTR (in); | |
| 428 | 3272 } |
| 3273 | |
| 3274 return make_string (temp, out - temp); | |
| 3275 } | |
| 3276 | |
| 3277 DEFUN ("set-word-regexp", Fset_word_regexp, 1, 1, 0, /* | |
| 3278 Set the regexp to be used to match a word in regular-expression searching. | |
| 3279 #### Not yet implemented. Currently does nothing. | |
| 3280 #### Do not use this yet. Its calling interface is likely to change. | |
| 3281 */ | |
| 2286 | 3282 (UNUSED (regexp))) |
| 428 | 3283 { |
| 3284 return Qnil; | |
| 3285 } | |
| 3286 | |
| 3287 | |
| 3288 /************************************************************************/ | |
| 3289 /* initialization */ | |
| 3290 /************************************************************************/ | |
| 3291 | |
| 3292 void | |
| 3293 syms_of_search (void) | |
| 3294 { | |
| 3295 | |
| 442 | 3296 DEFERROR_STANDARD (Qsearch_failed, Qinvalid_operation); |
| 3297 DEFERROR_STANDARD (Qinvalid_regexp, Qsyntax_error); | |
| 563 | 3298 Fput (Qinvalid_regexp, Qerror_lacks_explanatory_string, Qt); |
| 428 | 3299 |
| 3300 DEFSUBR (Flooking_at); | |
| 3301 DEFSUBR (Fposix_looking_at); | |
| 3302 DEFSUBR (Fstring_match); | |
| 3303 DEFSUBR (Fposix_string_match); | |
| 3304 DEFSUBR (Fskip_chars_forward); | |
| 3305 DEFSUBR (Fskip_chars_backward); | |
| 3306 DEFSUBR (Fskip_syntax_forward); | |
| 3307 DEFSUBR (Fskip_syntax_backward); | |
| 3308 DEFSUBR (Fsearch_forward); | |
| 3309 DEFSUBR (Fsearch_backward); | |
| 3310 DEFSUBR (Fword_search_forward); | |
| 3311 DEFSUBR (Fword_search_backward); | |
| 3312 DEFSUBR (Fre_search_forward); | |
| 3313 DEFSUBR (Fre_search_backward); | |
| 3314 DEFSUBR (Fposix_search_forward); | |
| 3315 DEFSUBR (Fposix_search_backward); | |
| 3316 DEFSUBR (Freplace_match); | |
| 3317 DEFSUBR (Fmatch_beginning); | |
| 3318 DEFSUBR (Fmatch_end); | |
| 3319 DEFSUBR (Fmatch_data); | |
| 3320 DEFSUBR (Fstore_match_data); | |
| 3321 DEFSUBR (Fregexp_quote); | |
| 3322 DEFSUBR (Fset_word_regexp); | |
| 3323 } | |
| 3324 | |
| 3325 void | |
| 3326 reinit_vars_of_search (void) | |
| 3327 { | |
| 3328 int i; | |
| 3329 | |
| 3330 last_thing_searched = Qnil; | |
| 3331 staticpro_nodump (&last_thing_searched); | |
| 3332 | |
| 3333 for (i = 0; i < REGEXP_CACHE_SIZE; ++i) | |
| 3334 { | |
| 3335 searchbufs[i].buf.allocated = 100; | |
| 3336 searchbufs[i].buf.buffer = (unsigned char *) xmalloc (100); | |
| 3337 searchbufs[i].buf.fastmap = searchbufs[i].fastmap; | |
| 3338 searchbufs[i].regexp = Qnil; | |
| 3339 staticpro_nodump (&searchbufs[i].regexp); | |
| 3340 searchbufs[i].next = (i == REGEXP_CACHE_SIZE-1 ? 0 : &searchbufs[i+1]); | |
| 3341 } | |
| 3342 searchbuf_head = &searchbufs[0]; | |
| 3343 } | |
| 3344 | |
| 3345 void | |
| 3346 vars_of_search (void) | |
| 3347 { | |
| 3348 DEFVAR_LISP ("forward-word-regexp", &Vforward_word_regexp /* | |
| 3349 *Regular expression to be used in `forward-word'. | |
| 3350 #### Not yet implemented. | |
| 3351 */ ); | |
| 3352 Vforward_word_regexp = Qnil; | |
| 3353 | |
| 3354 DEFVAR_LISP ("backward-word-regexp", &Vbackward_word_regexp /* | |
| 3355 *Regular expression to be used in `backward-word'. | |
| 3356 #### Not yet implemented. | |
| 3357 */ ); | |
| 3358 Vbackward_word_regexp = Qnil; | |
| 502 | 3359 |
| 3360 DEFVAR_INT ("warn-about-possibly-incompatible-back-references", | |
| 3361 &warn_about_possibly_incompatible_back_references /* | |
| 3362 If true, issue warnings when new-semantics back references occur. | |
| 3363 This is to catch places where old code might inadvertently have changed | |
| 3364 semantics. This will occur in old code only where more than nine groups | |
| 3365 occur and a back reference to one of them is directly followed by a digit. | |
| 3366 */ ); | |
| 3367 warn_about_possibly_incompatible_back_references = 1; | |
| 814 | 3368 |
| 2421 | 3369 Vskip_chars_range_table = Fmake_range_table (Qstart_closed_end_closed); |
| 428 | 3370 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
|
3371 #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
|
3372 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
|
3373 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
|
3374 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
|
3375 |
|
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3376 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
|
3377 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
|
3378 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
|
3379 */ ); |
|
df576f30c1d8
Correct case-insensitive search for non-case, non-ASCII chars. Add tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4407
diff
changeset
|
3380 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
|
3381 #endif |
| 428 | 3382 } |
