comparison src/search.c @ 103001:6b3176775d00

* search.c: Use EMACS_INT for buffer positions. Add prototypes. * lisp.h: Adjust prototypes.
author Andreas Schwab <schwab@linux-m68k.org>
date Thu, 16 Apr 2009 16:01:14 +0000
parents 4dbc3b20d83b
children 2f74d2880f10
comparison
equal deleted inserted replaced
103000:f5a6ea840fe4 103001:6b3176775d00
95 /* If non-nil, the match data will not be changed during call to 95 /* If non-nil, the match data will not be changed during call to
96 searching or matching functions. This variable is for internal use 96 searching or matching functions. This variable is for internal use
97 only. */ 97 only. */
98 Lisp_Object Vinhibit_changing_match_data; 98 Lisp_Object Vinhibit_changing_match_data;
99 99
100 static void set_search_regs (); 100 static void set_search_regs P_ ((EMACS_INT, EMACS_INT));
101 static void save_search_regs (); 101 static void save_search_regs P_ ((void));
102 static int simple_search (); 102 static EMACS_INT simple_search P_ ((int, unsigned char *, int, int,
103 static int boyer_moore (); 103 Lisp_Object, EMACS_INT, EMACS_INT,
104 static int search_buffer (); 104 EMACS_INT, EMACS_INT));
105 static EMACS_INT boyer_moore P_ ((int, unsigned char *, int, int,
106 Lisp_Object, Lisp_Object,
107 EMACS_INT, EMACS_INT,
108 EMACS_INT, EMACS_INT, int));
109 static EMACS_INT search_buffer P_ ((Lisp_Object, EMACS_INT, EMACS_INT,
110 EMACS_INT, EMACS_INT, int, int,
111 Lisp_Object, Lisp_Object, int));
105 static void matcher_overflow () NO_RETURN; 112 static void matcher_overflow () NO_RETURN;
106 113
107 static void 114 static void
108 matcher_overflow () 115 matcher_overflow ()
109 { 116 {
286 Lisp_Object string; 293 Lisp_Object string;
287 int posix; 294 int posix;
288 { 295 {
289 Lisp_Object val; 296 Lisp_Object val;
290 unsigned char *p1, *p2; 297 unsigned char *p1, *p2;
291 int s1, s2; 298 EMACS_INT s1, s2;
292 register int i; 299 register int i;
293 struct re_pattern_buffer *bufp; 300 struct re_pattern_buffer *bufp;
294 301
295 if (running_asynch_code) 302 if (running_asynch_code)
296 save_search_regs (); 303 save_search_regs ();
388 Lisp_Object regexp, string, start; 395 Lisp_Object regexp, string, start;
389 int posix; 396 int posix;
390 { 397 {
391 int val; 398 int val;
392 struct re_pattern_buffer *bufp; 399 struct re_pattern_buffer *bufp;
393 int pos, pos_byte; 400 EMACS_INT pos, pos_byte;
394 int i; 401 int i;
395 402
396 if (running_asynch_code) 403 if (running_asynch_code)
397 save_search_regs (); 404 save_search_regs ();
398 405
570 Lisp_Object string; 577 Lisp_Object string;
571 { 578 {
572 int multibyte; 579 int multibyte;
573 struct re_pattern_buffer *buf; 580 struct re_pattern_buffer *buf;
574 unsigned char *p1, *p2; 581 unsigned char *p1, *p2;
575 int s1, s2; 582 EMACS_INT s1, s2;
576 EMACS_INT len; 583 EMACS_INT len;
577 584
578 if (STRINGP (string)) 585 if (STRINGP (string))
579 { 586 {
580 if (pos_byte < 0) 587 if (pos_byte < 0)
674 except when inside redisplay. */ 681 except when inside redisplay. */
675 682
676 int 683 int
677 scan_buffer (target, start, end, count, shortage, allow_quit) 684 scan_buffer (target, start, end, count, shortage, allow_quit)
678 register int target; 685 register int target;
679 int start, end; 686 EMACS_INT start, end;
680 int count; 687 int count;
681 int *shortage; 688 int *shortage;
682 int allow_quit; 689 int allow_quit;
683 { 690 {
684 struct region_cache *newline_cache; 691 struct region_cache *newline_cache;
709 /* Our innermost scanning loop is very simple; it doesn't know 716 /* Our innermost scanning loop is very simple; it doesn't know
710 about gaps, buffer ends, or the newline cache. ceiling is 717 about gaps, buffer ends, or the newline cache. ceiling is
711 the position of the last character before the next such 718 the position of the last character before the next such
712 obstacle --- the last character the dumb search loop should 719 obstacle --- the last character the dumb search loop should
713 examine. */ 720 examine. */
714 int ceiling_byte = CHAR_TO_BYTE (end) - 1; 721 EMACS_INT ceiling_byte = CHAR_TO_BYTE (end) - 1;
715 int start_byte = CHAR_TO_BYTE (start); 722 EMACS_INT start_byte = CHAR_TO_BYTE (start);
716 int tem; 723 EMACS_INT tem;
717 724
718 /* If we're looking for a newline, consult the newline cache 725 /* If we're looking for a newline, consult the newline cache
719 to see where we can avoid some scanning. */ 726 to see where we can avoid some scanning. */
720 if (target == '\n' && newline_cache) 727 if (target == '\n' && newline_cache)
721 { 728 {
782 } 789 }
783 else 790 else
784 while (start > end) 791 while (start > end)
785 { 792 {
786 /* The last character to check before the next obstacle. */ 793 /* The last character to check before the next obstacle. */
787 int ceiling_byte = CHAR_TO_BYTE (end); 794 EMACS_INT ceiling_byte = CHAR_TO_BYTE (end);
788 int start_byte = CHAR_TO_BYTE (start); 795 EMACS_INT start_byte = CHAR_TO_BYTE (start);
789 int tem; 796 EMACS_INT tem;
790 797
791 /* Consult the newline cache, if appropriate. */ 798 /* Consult the newline cache, if appropriate. */
792 if (target == '\n' && newline_cache) 799 if (target == '\n' && newline_cache)
793 { 800 {
794 int next_change; 801 int next_change;
870 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do 877 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
871 except in special cases. */ 878 except in special cases. */
872 879
873 int 880 int
874 scan_newline (start, start_byte, limit, limit_byte, count, allow_quit) 881 scan_newline (start, start_byte, limit, limit_byte, count, allow_quit)
875 int start, start_byte; 882 EMACS_INT start, start_byte;
876 int limit, limit_byte; 883 EMACS_INT limit, limit_byte;
877 register int count; 884 register int count;
878 int allow_quit; 885 int allow_quit;
879 { 886 {
880 int direction = ((count > 0) ? 1 : -1); 887 int direction = ((count > 0) ? 1 : -1);
881 888
882 register unsigned char *cursor; 889 register unsigned char *cursor;
883 unsigned char *base; 890 unsigned char *base;
884 891
885 register int ceiling; 892 EMACS_INT ceiling;
886 register unsigned char *ceiling_addr; 893 register unsigned char *ceiling_addr;
887 894
888 int old_immediate_quit = immediate_quit; 895 int old_immediate_quit = immediate_quit;
889 896
890 /* The code that follows is like scan_buffer 897 /* The code that follows is like scan_buffer
968 return count * direction; 975 return count * direction;
969 } 976 }
970 977
971 int 978 int
972 find_next_newline_no_quit (from, cnt) 979 find_next_newline_no_quit (from, cnt)
973 register int from, cnt; 980 EMACS_INT from;
981 int cnt;
974 { 982 {
975 return scan_buffer ('\n', from, 0, cnt, (int *) 0, 0); 983 return scan_buffer ('\n', from, 0, cnt, (int *) 0, 0);
976 } 984 }
977 985
978 /* Like find_next_newline, but returns position before the newline, 986 /* Like find_next_newline, but returns position before the newline,
979 not after, and only search up to TO. This isn't just 987 not after, and only search up to TO. This isn't just
980 find_next_newline (...)-1, because you might hit TO. */ 988 find_next_newline (...)-1, because you might hit TO. */
981 989
982 int 990 int
983 find_before_next_newline (from, to, cnt) 991 find_before_next_newline (from, to, cnt)
984 int from, to, cnt; 992 EMACS_INT from, to;
993 int cnt;
985 { 994 {
986 int shortage; 995 int shortage;
987 int pos = scan_buffer ('\n', from, to, cnt, &shortage, 1); 996 int pos = scan_buffer ('\n', from, to, cnt, &shortage, 1);
988 997
989 if (shortage == 0) 998 if (shortage == 0)
1140 /* Only used in search_buffer, to record the end position of the match 1149 /* Only used in search_buffer, to record the end position of the match
1141 when searching regexps and SEARCH_REGS should not be changed 1150 when searching regexps and SEARCH_REGS should not be changed
1142 (i.e. Vinhibit_changing_match_data is non-nil). */ 1151 (i.e. Vinhibit_changing_match_data is non-nil). */
1143 static struct re_registers search_regs_1; 1152 static struct re_registers search_regs_1;
1144 1153
1145 static int 1154 static EMACS_INT
1146 search_buffer (string, pos, pos_byte, lim, lim_byte, n, 1155 search_buffer (string, pos, pos_byte, lim, lim_byte, n,
1147 RE, trt, inverse_trt, posix) 1156 RE, trt, inverse_trt, posix)
1148 Lisp_Object string; 1157 Lisp_Object string;
1149 int pos; 1158 EMACS_INT pos;
1150 int pos_byte; 1159 EMACS_INT pos_byte;
1151 int lim; 1160 EMACS_INT lim;
1152 int lim_byte; 1161 EMACS_INT lim_byte;
1153 int n; 1162 int n;
1154 int RE; 1163 int RE;
1155 Lisp_Object trt; 1164 Lisp_Object trt;
1156 Lisp_Object inverse_trt; 1165 Lisp_Object inverse_trt;
1157 int posix; 1166 int posix;
1491 1500
1492 This kind of search works regardless of what is in PAT and 1501 This kind of search works regardless of what is in PAT and
1493 regardless of what is in TRT. It is used in cases where 1502 regardless of what is in TRT. It is used in cases where
1494 boyer_moore cannot work. */ 1503 boyer_moore cannot work. */
1495 1504
1496 static int 1505 static EMACS_INT
1497 simple_search (n, pat, len, len_byte, trt, pos, pos_byte, lim, lim_byte) 1506 simple_search (n, pat, len, len_byte, trt, pos, pos_byte, lim, lim_byte)
1498 int n; 1507 int n;
1499 unsigned char *pat; 1508 unsigned char *pat;
1500 int len, len_byte; 1509 int len, len_byte;
1501 Lisp_Object trt; 1510 Lisp_Object trt;
1502 int pos, pos_byte; 1511 EMACS_INT pos, pos_byte;
1503 int lim, lim_byte; 1512 EMACS_INT lim, lim_byte;
1504 { 1513 {
1505 int multibyte = ! NILP (current_buffer->enable_multibyte_characters); 1514 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
1506 int forward = n > 0; 1515 int forward = n > 0;
1507 /* Number of buffer bytes matched. Note that this may be different 1516 /* Number of buffer bytes matched. Note that this may be different
1508 from len_byte in a multibyte buffer. */ 1517 from len_byte in a multibyte buffer. */
1512 while (n > 0) 1521 while (n > 0)
1513 { 1522 {
1514 while (1) 1523 while (1)
1515 { 1524 {
1516 /* Try matching at position POS. */ 1525 /* Try matching at position POS. */
1517 int this_pos = pos; 1526 EMACS_INT this_pos = pos;
1518 int this_pos_byte = pos_byte; 1527 EMACS_INT this_pos_byte = pos_byte;
1519 int this_len = len; 1528 int this_len = len;
1520 int this_len_byte = len_byte; 1529 int this_len_byte = len_byte;
1521 unsigned char *p = pat; 1530 unsigned char *p = pat;
1522 if (pos + len > lim || pos_byte + len_byte > lim_byte) 1531 if (pos + len > lim || pos_byte + len_byte > lim_byte)
1523 goto stop; 1532 goto stop;
1561 while (n > 0) 1570 while (n > 0)
1562 { 1571 {
1563 while (1) 1572 while (1)
1564 { 1573 {
1565 /* Try matching at position POS. */ 1574 /* Try matching at position POS. */
1566 int this_pos = pos; 1575 EMACS_INT this_pos = pos;
1567 int this_len = len; 1576 int this_len = len;
1568 unsigned char *p = pat; 1577 unsigned char *p = pat;
1569 1578
1570 if (pos + len > lim) 1579 if (pos + len > lim)
1571 goto stop; 1580 goto stop;
1600 while (n < 0) 1609 while (n < 0)
1601 { 1610 {
1602 while (1) 1611 while (1)
1603 { 1612 {
1604 /* Try matching at position POS. */ 1613 /* Try matching at position POS. */
1605 int this_pos = pos - len; 1614 EMACS_INT this_pos = pos - len;
1606 int this_pos_byte; 1615 EMACS_INT this_pos_byte;
1607 int this_len = len; 1616 int this_len = len;
1608 int this_len_byte = len_byte; 1617 int this_len_byte = len_byte;
1609 unsigned char *p = pat; 1618 unsigned char *p = pat;
1610 1619
1611 if (this_pos < lim || (pos_byte - len_byte) < lim_byte) 1620 if (this_pos < lim || (pos_byte - len_byte) < lim_byte)
1650 while (n < 0) 1659 while (n < 0)
1651 { 1660 {
1652 while (1) 1661 while (1)
1653 { 1662 {
1654 /* Try matching at position POS. */ 1663 /* Try matching at position POS. */
1655 int this_pos = pos - len; 1664 EMACS_INT this_pos = pos - len;
1656 int this_len = len; 1665 int this_len = len;
1657 unsigned char *p = pat; 1666 unsigned char *p = pat;
1658 1667
1659 if (this_pos < lim) 1668 if (this_pos < lim)
1660 goto stop; 1669 goto stop;
1713 character, and do so after just a simple test of the context. 1722 character, and do so after just a simple test of the context.
1714 CHAR_BASE is nonzero if there is such a non-ASCII character. 1723 CHAR_BASE is nonzero if there is such a non-ASCII character.
1715 1724
1716 If that criterion is not satisfied, do not call this function. */ 1725 If that criterion is not satisfied, do not call this function. */
1717 1726
1718 static int 1727 static EMACS_INT
1719 boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt, 1728 boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
1720 pos, pos_byte, lim, lim_byte, char_base) 1729 pos, pos_byte, lim, lim_byte, char_base)
1721 int n; 1730 int n;
1722 unsigned char *base_pat; 1731 unsigned char *base_pat;
1723 int len, len_byte; 1732 int len, len_byte;
1724 Lisp_Object trt; 1733 Lisp_Object trt;
1725 Lisp_Object inverse_trt; 1734 Lisp_Object inverse_trt;
1726 int pos, pos_byte; 1735 EMACS_INT pos, pos_byte;
1727 int lim, lim_byte; 1736 EMACS_INT lim, lim_byte;
1728 int char_base; 1737 int char_base;
1729 { 1738 {
1730 int direction = ((n > 0) ? 1 : -1); 1739 int direction = ((n > 0) ? 1 : -1);
1731 register int dirlen; 1740 register int dirlen;
1732 int limit, stride_for_teases = 0; 1741 EMACS_INT limit;
1742 int stride_for_teases = 0;
1733 int BM_tab[0400]; 1743 int BM_tab[0400];
1734 register unsigned char *cursor, *p_limit; 1744 register unsigned char *cursor, *p_limit;
1735 register int i, j; 1745 register int i, j;
1736 unsigned char *pat, *pat_end; 1746 unsigned char *pat, *pat_end;
1737 int multibyte = ! NILP (current_buffer->enable_multibyte_characters); 1747 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
1886 pos_byte += dirlen - ((direction > 0) ? direction : 0); 1896 pos_byte += dirlen - ((direction > 0) ? direction : 0);
1887 /* loop invariant - POS_BYTE points at where last char (first 1897 /* loop invariant - POS_BYTE points at where last char (first
1888 char if reverse) of pattern would align in a possible match. */ 1898 char if reverse) of pattern would align in a possible match. */
1889 while (n != 0) 1899 while (n != 0)
1890 { 1900 {
1891 int tail_end; 1901 EMACS_INT tail_end;
1892 unsigned char *tail_end_ptr; 1902 unsigned char *tail_end_ptr;
1893 1903
1894 /* It's been reported that some (broken) compiler thinks that 1904 /* It's been reported that some (broken) compiler thinks that
1895 Boolean expressions in an arithmetic context are unsigned. 1905 Boolean expressions in an arithmetic context are unsigned.
1896 Using an explicit ?1:0 prevents this. */ 1906 Using an explicit ?1:0 prevents this. */
1990 } 2000 }
1991 } 2001 }
1992 cursor += dirlen - i - direction; /* fix cursor */ 2002 cursor += dirlen - i - direction; /* fix cursor */
1993 if (i + direction == 0) 2003 if (i + direction == 0)
1994 { 2004 {
1995 int position, start, end; 2005 EMACS_INT position, start, end;
1996 2006
1997 cursor -= direction; 2007 cursor -= direction;
1998 2008
1999 position = pos_byte + cursor - p2 + ((direction > 0) 2009 position = pos_byte + cursor - p2 + ((direction > 0)
2000 ? 1 - len_byte : 0); 2010 ? 1 - len_byte : 0);
2082 back to the first pos (last pos if reverse). 2092 back to the first pos (last pos if reverse).
2083 Set it once again at the last (first if reverse) char. */ 2093 Set it once again at the last (first if reverse) char. */
2084 pos_byte += dirlen - i - direction; 2094 pos_byte += dirlen - i - direction;
2085 if (i + direction == 0) 2095 if (i + direction == 0)
2086 { 2096 {
2087 int position, start, end; 2097 EMACS_INT position, start, end;
2088 pos_byte -= direction; 2098 pos_byte -= direction;
2089 2099
2090 position = pos_byte + ((direction > 0) ? 1 - len_byte : 0); 2100 position = pos_byte + ((direction > 0) ? 1 - len_byte : 0);
2091 set_search_regs (position, len_byte); 2101 set_search_regs (position, len_byte);
2092 2102
2124 for the overall match just found in the current buffer. 2134 for the overall match just found in the current buffer.
2125 Also clear out the match data for registers 1 and up. */ 2135 Also clear out the match data for registers 1 and up. */
2126 2136
2127 static void 2137 static void
2128 set_search_regs (beg_byte, nbytes) 2138 set_search_regs (beg_byte, nbytes)
2129 int beg_byte, nbytes; 2139 EMACS_INT beg_byte, nbytes;
2130 { 2140 {
2131 int i; 2141 int i;
2132 2142
2133 if (!NILP (Vinhibit_changing_match_data)) 2143 if (!NILP (Vinhibit_changing_match_data))
2134 return; 2144 return;
2476 int some_lowercase; 2486 int some_lowercase;
2477 int some_uppercase; 2487 int some_uppercase;
2478 int some_nonuppercase_initial; 2488 int some_nonuppercase_initial;
2479 register int c, prevc; 2489 register int c, prevc;
2480 int sub; 2490 int sub;
2481 int opoint, newpoint; 2491 EMACS_INT opoint, newpoint;
2482 2492
2483 CHECK_STRING (newtext); 2493 CHECK_STRING (newtext);
2484 2494
2485 if (! NILP (string)) 2495 if (! NILP (string))
2486 CHECK_STRING (string); 2496 CHECK_STRING (string);
2519 } 2529 }
2520 2530
2521 if (NILP (fixedcase)) 2531 if (NILP (fixedcase))
2522 { 2532 {
2523 /* Decide how to casify by examining the matched text. */ 2533 /* Decide how to casify by examining the matched text. */
2524 int last; 2534 EMACS_INT last;
2525 2535
2526 pos = search_regs.start[sub]; 2536 pos = search_regs.start[sub];
2527 last = search_regs.end[sub]; 2537 last = search_regs.end[sub];
2528 2538
2529 if (NILP (string)) 2539 if (NILP (string))
2606 2616
2607 /* Substitute parts of the match into NEWTEXT 2617 /* Substitute parts of the match into NEWTEXT
2608 if desired. */ 2618 if desired. */
2609 if (NILP (literal)) 2619 if (NILP (literal))
2610 { 2620 {
2611 int lastpos = 0; 2621 EMACS_INT lastpos = 0;
2612 int lastpos_byte = 0; 2622 EMACS_INT lastpos_byte = 0;
2613 /* We build up the substituted string in ACCUM. */ 2623 /* We build up the substituted string in ACCUM. */
2614 Lisp_Object accum; 2624 Lisp_Object accum;
2615 Lisp_Object middle; 2625 Lisp_Object middle;
2616 int length = SBYTES (newtext); 2626 int length = SBYTES (newtext);
2617 2627
2797 2807
2798 /* If we want to copy part of a previous match, 2808 /* If we want to copy part of a previous match,
2799 set up ADD_STUFF and ADD_LEN to point to it. */ 2809 set up ADD_STUFF and ADD_LEN to point to it. */
2800 if (idx >= 0) 2810 if (idx >= 0)
2801 { 2811 {
2802 int begbyte = CHAR_TO_BYTE (search_regs.start[idx]); 2812 EMACS_INT begbyte = CHAR_TO_BYTE (search_regs.start[idx]);
2803 add_len = CHAR_TO_BYTE (search_regs.end[idx]) - begbyte; 2813 add_len = CHAR_TO_BYTE (search_regs.end[idx]) - begbyte;
2804 if (search_regs.start[idx] < GPT && GPT < search_regs.end[idx]) 2814 if (search_regs.start[idx] < GPT && GPT < search_regs.end[idx])
2805 move_gap (search_regs.start[idx]); 2815 move_gap (search_regs.start[idx]);
2806 add_stuff = BYTE_POS_ADDR (begbyte); 2816 add_stuff = BYTE_POS_ADDR (begbyte);
2807 } 2817 }
2851 Fupcase_initials_region (make_number (search_regs.start[sub]), 2861 Fupcase_initials_region (make_number (search_regs.start[sub]),
2852 make_number (newpoint)); 2862 make_number (newpoint));
2853 2863
2854 /* Adjust search data for this change. */ 2864 /* Adjust search data for this change. */
2855 { 2865 {
2856 int oldend = search_regs.end[sub]; 2866 EMACS_INT oldend = search_regs.end[sub];
2857 int oldstart = search_regs.start[sub]; 2867 EMACS_INT oldstart = search_regs.start[sub];
2858 int change = newpoint - search_regs.end[sub]; 2868 EMACS_INT change = newpoint - search_regs.end[sub];
2859 int i; 2869 int i;
2860 2870
2861 for (i = 0; i < search_regs.num_regs; i++) 2871 for (i = 0; i < search_regs.num_regs; i++)
2862 { 2872 {
2863 if (search_regs.start[i] >= oldend) 2873 if (search_regs.start[i] >= oldend)
3109 search_regs.start[i] = -1; 3119 search_regs.start[i] = -1;
3110 list = XCDR (list); 3120 list = XCDR (list);
3111 } 3121 }
3112 else 3122 else
3113 { 3123 {
3114 int from; 3124 EMACS_INT from;
3115 Lisp_Object m; 3125 Lisp_Object m;
3116 3126
3117 m = marker; 3127 m = marker;
3118 if (MARKERP (marker)) 3128 if (MARKERP (marker))
3119 { 3129 {