comparison src/search.c @ 1425:74cb069b8417

[xemacs-hg @ 2003-04-23 15:42:44 by stephent] stale match data <87fzo99rje.fsf@tleepslib.sk.tsukuba.ac.jp> new split-string <87d6jd9qis.fsf@tleepslib.sk.tsukuba.ac.jp> support (info "(file)node") <87adeh9qa7.fsf@tleepslib.sk.tsukuba.ac.jp>
author stephent
date Wed, 23 Apr 2003 15:42:52 +0000
parents 8d350b095c21
children 0f42689481f0
comparison
equal deleted inserted replaced
1424:c35e2ad2f97d 1425:74cb069b8417
91 that gets real ugly real fast since the buffer might have changed and 91 that gets real ugly real fast since the buffer might have changed and
92 the positions might be out of sync or out of range. 92 the positions might be out of sync or out of range.
93 */ 93 */
94 static struct re_registers search_regs; 94 static struct re_registers search_regs;
95 95
96 /* Every function that _may_ set the match data _must_ clear the search
97 registers on entry. An unsuccessful search should leave the search
98 registers cleared. Applications that are no-ops by definition (eg,
99 searches with a repetition count of 0) _must not_ clear the search
100 registers.
101
102 XEmacs 21.5 up to beta 11 may have permitted the following idiom to
103 "win" in the sense that the match data was set to the last successful
104 match's match data, and not cleared as the current implemenation does:
105
106 (while (search_forward "string"))
107 (use-match-data-of-last-successful-search)
108
109 This no longer can work. You must use save-match-data to preserve the
110 match data:
111
112 (let (md)
113 (while (when (search-forward "string") (setq md (match-data))))
114 (set-match-data md))
115 (use-match-data-of-last-successful-search)
116 */
117 static void set_search_regs (struct buffer *buf, Charbpos beg, Charcount len);
118 static void clear_search_regs (struct re_registers *regp);
119
96 /* The buffer in which the last search was performed, or 120 /* The buffer in which the last search was performed, or
97 Qt if the last search was done in a string; 121 Qt if the last search was done in a string;
98 Qnil if no searching has been done yet. */ 122 Qnil if no searching has been done yet. */
99 static Lisp_Object last_thing_searched; 123 static Lisp_Object last_thing_searched;
100 124
108 Fixnum warn_about_possibly_incompatible_back_references; 132 Fixnum warn_about_possibly_incompatible_back_references;
109 133
110 /* range table for use with skip_chars. Only needed for Mule. */ 134 /* range table for use with skip_chars. Only needed for Mule. */
111 Lisp_Object Vskip_chars_range_table; 135 Lisp_Object Vskip_chars_range_table;
112 136
113 static void set_search_regs (struct buffer *buf, Charbpos beg, Charcount len);
114 static void clear_unused_search_regs (struct re_registers *regp, int no_sub);
115 static Charbpos simple_search (struct buffer *buf, Ibyte *base_pat, 137 static Charbpos simple_search (struct buffer *buf, Ibyte *base_pat,
116 Bytecount len, Bytebpos pos, Bytebpos lim, 138 Bytecount len, Bytebpos pos, Bytebpos lim,
117 EMACS_INT n, Lisp_Object trt); 139 EMACS_INT n, Lisp_Object trt);
118 static Charbpos boyer_moore (struct buffer *buf, Ibyte *base_pat, 140 static Charbpos boyer_moore (struct buffer *buf, Ibyte *base_pat,
119 Bytecount len, Bytebpos pos, Bytebpos lim, 141 Bytecount len, Bytebpos pos, Bytebpos lim,
302 REGISTER int i; 324 REGISTER int i;
303 struct re_pattern_buffer *bufp; 325 struct re_pattern_buffer *bufp;
304 struct syntax_cache scache_struct; 326 struct syntax_cache scache_struct;
305 struct syntax_cache *scache = &scache_struct; 327 struct syntax_cache *scache = &scache_struct;
306 328
329 /* clear search registers *now*. no mercy, not even for errors */
330 clear_search_regs (&search_regs);
331
307 CHECK_STRING (string); 332 CHECK_STRING (string);
308 bufp = compile_pattern (string, &search_regs, 333 bufp = compile_pattern (string, &search_regs,
309 (!NILP (buf->case_fold_search) 334 (!NILP (buf->case_fold_search)
310 ? XCASE_TABLE_DOWNCASE (buf->case_table) : Qnil), 335 ? XCASE_TABLE_DOWNCASE (buf->case_table) : Qnil),
311 wrap_buffer (buf), buf, posix, ERROR_ME); 336 wrap_buffer (buf), buf, posix, ERROR_ME);
391 struct re_pattern_buffer *bufp; 416 struct re_pattern_buffer *bufp;
392 417
393 /* Some FSF junk with running_asynch_code, to preserve the match 418 /* Some FSF junk with running_asynch_code, to preserve the match
394 data. Not necessary because we don't call process filters 419 data. Not necessary because we don't call process filters
395 asynchronously (i.e. from within QUIT). */ 420 asynchronously (i.e. from within QUIT). */
421
422 /* clear search registers *now*. no mercy, not even for errors */
423 clear_search_regs (&search_regs);
396 424
397 CHECK_STRING (regexp); 425 CHECK_STRING (regexp);
398 CHECK_STRING (string); 426 CHECK_STRING (string);
399 427
400 if (NILP (start)) 428 if (NILP (start))
1217 1245
1218 /* Some FSF junk with running_asynch_code, to preserve the match 1246 /* Some FSF junk with running_asynch_code, to preserve the match
1219 data. Not necessary because we don't call process filters 1247 data. Not necessary because we don't call process filters
1220 asynchronously (i.e. from within QUIT). */ 1248 asynchronously (i.e. from within QUIT). */
1221 1249
1222 /* Null string is found at starting position. */
1223 if (len == 0)
1224 {
1225 set_search_regs (buf, charbpos, 0);
1226 clear_unused_search_regs (&search_regs, 0);
1227 return charbpos;
1228 }
1229
1230 /* Searching 0 times means noop---don't move, don't touch registers. */ 1250 /* Searching 0 times means noop---don't move, don't touch registers. */
1231 if (n == 0) 1251 if (n == 0)
1232 return charbpos; 1252 return charbpos;
1253
1254 /* clear the search regs now */
1255 clear_search_regs (&search_regs);
1256
1257 /* Null string is found at starting position. */
1258 if (len == 0)
1259 {
1260 set_search_regs (buf, charbpos, 0);
1261 return charbpos;
1262 }
1233 1263
1234 pos = charbpos_to_bytebpos (buf, charbpos); 1264 pos = charbpos_to_bytebpos (buf, charbpos);
1235 lim = charbpos_to_bytebpos (buf, buflim); 1265 lim = charbpos_to_bytebpos (buf, buflim);
1236 if (RE && !trivial_regexp_p (string)) 1266 if (RE && !trivial_regexp_p (string))
1237 { 1267 {
1481 { 1511 {
1482 retval = beg = bytebpos_to_charbpos (buf, pos); 1512 retval = beg = bytebpos_to_charbpos (buf, pos);
1483 end = bytebpos_to_charbpos (buf, pos + buf_len); 1513 end = bytebpos_to_charbpos (buf, pos + buf_len);
1484 } 1514 }
1485 set_search_regs (buf, beg, end - beg); 1515 set_search_regs (buf, beg, end - beg);
1486 clear_unused_search_regs (&search_regs, 0);
1487 1516
1488 return retval; 1517 return retval;
1489 } 1518 }
1490 else if (n > 0) 1519 else if (n > 0)
1491 return -n; 1520 return -n;
1615 in the pattern. Others don't matter anyway! */ 1644 in the pattern. Others don't matter anyway! */
1616 xzero (simple_translate); 1645 xzero (simple_translate);
1617 for (i = 0; i < 0400; i++) 1646 for (i = 0; i < 0400; i++)
1618 simple_translate[i] = (Ibyte) i; 1647 simple_translate[i] = (Ibyte) i;
1619 i = 0; 1648 i = 0;
1649
1650 /* clear search regs now */
1651 clear_search_regs (&search_regs);
1652
1620 while (i != infinity) 1653 while (i != infinity)
1621 { 1654 {
1622 Ibyte *ptr = base_pat + i; 1655 Ibyte *ptr = base_pat + i;
1623 i += direction; 1656 i += direction;
1624 if (i == dirlen) 1657 if (i == dirlen)
1845 ? 1 - len : 0)); 1878 ? 1 - len : 0));
1846 Charbpos bufstart = bytebpos_to_charbpos (buf, bytstart); 1879 Charbpos bufstart = bytebpos_to_charbpos (buf, bytstart);
1847 Charbpos bufend = bytebpos_to_charbpos (buf, bytstart + len); 1880 Charbpos bufend = bytebpos_to_charbpos (buf, bytstart + len);
1848 1881
1849 set_search_regs (buf, bufstart, bufend - bufstart); 1882 set_search_regs (buf, bufstart, bufend - bufstart);
1850 clear_unused_search_regs (&search_regs, 0);
1851 } 1883 }
1852 1884
1853 if ((n -= direction) != 0) 1885 if ((n -= direction) != 0)
1854 cursor += dirlen; /* to resume search */ 1886 cursor += dirlen; /* to resume search */
1855 else 1887 else
1936 ? 1 - len : 0)); 1968 ? 1 - len : 0));
1937 Charbpos bufstart = bytebpos_to_charbpos (buf, bytstart); 1969 Charbpos bufstart = bytebpos_to_charbpos (buf, bytstart);
1938 Charbpos bufend = bytebpos_to_charbpos (buf, bytstart + len); 1970 Charbpos bufend = bytebpos_to_charbpos (buf, bytstart + len);
1939 1971
1940 set_search_regs (buf, bufstart, bufend - bufstart); 1972 set_search_regs (buf, bufstart, bufend - bufstart);
1941 clear_unused_search_regs (&search_regs, 0);
1942 } 1973 }
1943 1974
1944 if ((n -= direction) != 0) 1975 if ((n -= direction) != 0)
1945 pos += dirlen; /* to resume search */ 1976 pos += dirlen; /* to resume search */
1946 else 1977 else
1976 search_regs.start[0] = beg; 2007 search_regs.start[0] = beg;
1977 search_regs.end[0] = beg + len; 2008 search_regs.end[0] = beg + len;
1978 last_thing_searched = wrap_buffer (buf); 2009 last_thing_searched = wrap_buffer (buf);
1979 } 2010 }
1980 2011
1981 /* Clear unused search registers so match data will be null. 2012 /* Clear search registers so match data will be null.
1982 REGP is a pointer to the register structure to clear, usually the global 2013 REGP is a pointer to the register structure to clear, usually the global
1983 search_regs. 2014 search_regs. */
1984 NO_SUB is the number of subexpressions to allow for. (Does not count
1985 the whole match, ie, for a string search NO_SUB == 0.)
1986 It is an error if NO_SUB > REGP.num_regs - 1. */
1987 2015
1988 static void 2016 static void
1989 clear_unused_search_regs (struct re_registers *regp, int no_sub) 2017 clear_search_regs (struct re_registers *regp)
1990 { 2018 {
1991 /* This function has been Mule-ized. */ 2019 /* This function has been Mule-ized. */
1992 int i; 2020 int i;
1993 2021
1994 assert (no_sub >= 0 && no_sub < regp->num_regs); 2022 for (i = 0; i < regp->num_regs; i++)
1995 for (i = no_sub + 1; i < regp->num_regs; i++)
1996 regp->start[i] = regp->end[i] = -1; 2023 regp->start[i] = regp->end[i] = -1;
1997 } 2024 }
1998 2025
1999 2026
2000 /* Given a string of words separated by word delimiters, 2027 /* Given a string of words separated by word delimiters,
2321 2348
2322 When fourth argument is nil, STRBUFFER specifies a subexpression of 2349 When fourth argument is nil, STRBUFFER specifies a subexpression of
2323 the match. It says to replace just that subexpression instead of the 2350 the match. It says to replace just that subexpression instead of the
2324 whole match. This is useful only after a regular expression search or 2351 whole match. This is useful only after a regular expression search or
2325 match since only regular expressions have distinguished subexpressions. 2352 match since only regular expressions have distinguished subexpressions.
2353
2354 If no match (including searches) has been conducted, the last match
2355 operation failed, or the requested subexpression was not matched, an
2356 `args-out-of-range' error will be signaled. (If no match has ever been
2357 conducted in this instance of XEmacs, an `invalid-operation' error will
2358 be signaled. This is very rare.)
2326 */ 2359 */
2327 (replacement, fixedcase, literal, string, strbuffer)) 2360 (replacement, fixedcase, literal, string, strbuffer))
2328 { 2361 {
2329 /* This function can GC */ 2362 /* This function can GC */
2330 enum { nochange, all_caps, cap_initial } case_action; 2363 enum { nochange, all_caps, cap_initial } case_action;