📄 search.c
字号:
If `n' is negative, searching is backward and `lim' must be less than `from'. Returns -x if only `n'-x occurrences found (x > 0), or else the position at the beginning of the `n'th occurrence (if searching backward) or the end (if searching forward). *//* INTERFACE CHANGE ALERT!!!! search_buffer now returns -x if only *//* n-x occurences are found. */search_buffer (string, pos, lim, n, RE, trt) Lisp_Object string; int pos; int lim; int n; int RE; register unsigned char *trt;{ int len = XSTRING (string)->size; unsigned char *base_pat = XSTRING (string)->data; register int *BM_tab; int *BM_tab_base; register int direction = ((n > 0) ? 1 : -1); register int dirlen; int infinity, limit, k, stride_for_teases; register unsigned char *pat, *cursor, *p_limit; register int i, j; unsigned char *p1, *p2; int s1, s2; if (!len) return (0); if (RE) compile_pattern (string, &searchbuf, (char *) trt); if (RE /* Here we detect whether the */ /* generality of an RE search is */ /* really needed. */ && *(searchbuf.buffer) == (char) exactn /* first item is "exact match" */ && searchbuf.buffer[1] + 2 == searchbuf.used) /*first is ONLY item */ { RE = 0; /* can do straight (non RE) search */ pat = (base_pat = (unsigned char *) searchbuf.buffer + 2); /* trt already applied */ len = searchbuf.used - 2; } else if (!RE) { pat = (unsigned char *) alloca (len); for (i = len; i--;) /* Copy the pattern; apply trt */ *pat++ = (((int) trt) ? trt [*base_pat++] : *base_pat++); pat -= len; base_pat = pat; } if (RE) { immediate_quit = 1; /* Quit immediately if user types ^G, because letting this function finish can take too long. */ QUIT; /* Do a pending quit right away, to avoid paradoxical behavior */ /* Get pointers and sizes of the two strings that make up the visible portion of the buffer. */ p1 = BEGV_ADDR; s1 = GPT - BEGV; p2 = GAP_END_ADDR; s2 = ZV - GPT; if (s1 < 0) { p2 = p1; s2 = ZV - BEGV; s1 = 0; } if (s2 < 0) { s1 = ZV - BEGV; s2 = 0; } while (n < 0) { if (re_search_2 (&searchbuf, p1, s1, p2, s2, pos - BEGV, lim - pos, &search_regs, /* Don't allow match past current point */ pos - BEGV) >= 0) { j = BEGV; for (i = 0; i < RE_NREGS; i++) if (search_regs.start[i] >= 0) { search_regs.start[i] += j; search_regs.end[i] += j; } /* Set pos to the new position. */ pos = search_regs.start[0]; } else { immediate_quit = 0; return (n); } n++; } while (n > 0) { if (re_search_2 (&searchbuf, p1, s1, p2, s2, pos - BEGV, lim - pos, &search_regs, lim - BEGV) >= 0) { j = BEGV; for (i = 0; i < RE_NREGS; i++) if (search_regs.start[i] >= 0) { search_regs.start[i] += j; search_regs.end[i] += j; } pos = search_regs.end[0]; } else { immediate_quit = 0; return (0 - n); } n--; } immediate_quit = 0; return (pos); } else /* non-RE case */ {#ifdef C_ALLOCA int BM_tab_space[0400]; BM_tab = &BM_tab_space[0];#else BM_tab = (int *) alloca (0400 * sizeof (int));#endif /* The general approach is that we are going to maintain that we know */ /* the first (closest to the present position, in whatever direction */ /* we're searching) character that could possibly be the last */ /* (furthest from present position) character of a valid match. We */ /* advance the state of our knowledge by looking at that character */ /* and seeing whether it indeed matches the last character of the */ /* pattern. If it does, we take a closer look. If it does not, we */ /* move our pointer (to putative last characters) as far as is */ /* logically possible. This amount of movement, which I call a */ /* stride, will be the length of the pattern if the actual character */ /* appears nowhere in the pattern, otherwise it will be the distance */ /* from the last occurrence of that character to the end of the */ /* pattern. */ /* As a coding trick, an enormous stride is coded into the table for */ /* characters that match the last character. This allows use of only */ /* a single test, a test for having gone past the end of the */ /* permissible match region, to test for both possible matches (when */ /* the stride goes past the end immediately) and failure to */ /* match (where you get nudged past the end one stride at a time). */ /* Here we make a "mickey mouse" BM table. The stride of the search */ /* is determined only by the last character of the putative match. */ /* If that character does not match, we will stride the proper */ /* distance to propose a match that superimposes it on the last */ /* instance of a character that matches it (per trt), or misses */ /* it entirely if there is none. */ dirlen = len * direction; infinity = dirlen - (lim + pos + len + len) * direction; if (direction < 0) pat = (base_pat += len - 1); BM_tab_base = BM_tab; BM_tab += 0400; j = dirlen; /* to get it in a register */ /* A character that does not appear in the pattern induces a */ /* stride equal to the pattern length. */ while (BM_tab_base != BM_tab) { *--BM_tab = j; *--BM_tab = j; *--BM_tab = j; *--BM_tab = j; } i = 0; while (i != infinity) { j = pat[i]; i += direction; if (i == dirlen) i = infinity; if ((int) trt) { k = (j = trt[j]); if (i == infinity) stride_for_teases = BM_tab[j]; BM_tab[j] = dirlen - i; /* A translation table is followed by its inverse -- see */ /* comment following downcase_table for details */ while ((j = trt[0400+j]) != k) BM_tab[j] = dirlen - i; } else { if (i == infinity) stride_for_teases = BM_tab[j]; BM_tab[j] = dirlen - i; } /* stride_for_teases tells how much to stride if we get a */ /* match on the far character but are subsequently */ /* disappointed, by recording what the stride would have been */ /* for that character if the last character had been */ /* different. */ } infinity = dirlen - infinity; pos += dirlen - ((direction > 0) ? direction : 0); /* loop invariant - pos points at where last char (first char if reverse) of pattern would align in a possible match. */ while (n != 0) { if ((lim - pos - (direction > 0)) * direction < 0) return (n * (0 - direction)); /* First we do the part we can by pointers (maybe nothing) */ QUIT; pat = base_pat; limit = pos - dirlen + direction; limit = ((direction > 0) ? BufferSafeCeiling (limit) : BufferSafeFloor (limit)); /* LIMIT is now the last (not beyond-last!) value POS can take on without hitting edge of buffer or the gap. */ limit = ((direction > 0) ? min (lim - 1, min (limit, pos + 20000)) : max (lim, max (limit, pos - 20000))); if ((limit - pos) * direction > 20) { p_limit = &FETCH_CHAR (limit); p2 = (cursor = &FETCH_CHAR (pos)); /* In this loop, pos + cursor - p2 is the surrogate for pos */ while (1) /* use one cursor setting as long as i can */ { if (direction > 0) /* worth duplicating */ { /* Use signed comparison if appropriate to make cursor+infinity sure to be > p_limit. Assuming that the buffer lies in a range of addresses that are all "positive" (as ints) or all "negative", either kind of comparison will work as long as we don't step by infinity. So pick the kind that works when we do step by infinity. */ if ((int) (p_limit + infinity) > (int) p_limit) while ((int) cursor <= (int) p_limit) cursor += BM_tab[*cursor]; else while ((unsigned int) cursor <= (unsigned int) p_limit) cursor += BM_tab[*cursor]; } else { if ((int) (p_limit + infinity) < (int) p_limit) while ((int) cursor >= (int) p_limit) cursor += BM_tab[*cursor]; else while ((unsigned int) cursor >= (unsigned int) p_limit) cursor += BM_tab[*cursor]; }/* If you are here, cursor is beyond the end of the searched region. */ /* This can happen if you match on the far character of the pattern, */ /* because the "stride" of that character is infinity, a number able */ /* to throw you well beyond the end of the search. It can also */ /* happen if you fail to match within the permitted region and would */ /* otherwise try a character beyond that region */ if ((cursor - p_limit) * direction <= len) break; /* a small overrun is genuine */ cursor -= infinity; /* large overrun = hit */ i = dirlen - direction; if ((int) trt) { while ((i -= direction) + direction != 0) if (pat[i] != trt[*(cursor -= direction)]) break; } else { while ((i -= direction) + direction != 0) if (pat[i] != *(cursor -= direction)) break; } cursor += dirlen - i - direction; /* fix cursor */ if (i + direction == 0) { cursor -= direction; search_regs.start[0] = pos + cursor - p2 + ((direction > 0) ? 1 - len : 0); search_regs.end[0] = len + search_regs.start[0]; if ((n -= direction) != 0) cursor += dirlen; /* to resume search */ else return ((direction > 0) ? search_regs.end[0] : search_regs.start[0]); } else cursor += stride_for_teases; /* <sigh> we lose - */ } pos += cursor - p2; } else /* Now we'll pick up a clump that has to be done the hard */ /* way because it covers a discontinuity */ { limit = ((direction > 0) ? BufferSafeCeiling (pos - dirlen + 1) : BufferSafeFloor (pos - dirlen - 1)); limit = ((direction > 0) ? min (limit + len, lim - 1) : max (limit - len, lim)); /* LIMIT is now the last value POS can have and still be valid for a possible match. */ while (1) { /* This loop can be coded for space rather than */ /* speed because it will usually run only once. */ /* (the reach is at most len + 21, and typically */ /* does not exceed len) */ while ((limit - pos) * direction >= 0) pos += BM_tab[FETCH_CHAR(pos)]; /* now run the same tests to distinguish going off the */ /* end, a match or a phoney match. */ if ((pos - limit) * direction <= len) break; /* ran off the end */ /* Found what might be a match. Set POS back to last (first if reverse) char pos. */ pos -= infinity; i = dirlen - direction; while ((i -= direction) + direction != 0) { pos -= direction; if (pat[i] != (((int) trt) ? trt[FETCH_CHAR(pos)] : FETCH_CHAR (pos))) break; } /* Above loop has moved POS part or all the way back to the first char pos (last char pos if reverse). Set it once again at the last (first if reverse) char. */ pos += dirlen - i- direction; if (i + direction == 0) { pos -= direction; search_regs.start[0] = pos + ((direction > 0) ? 1 - len : 0); search_regs.end[0] = len + search_regs.start[0]; if ((n -= direction) != 0) pos += dirlen; /* to resume search */ else return ((direction > 0) ? search_regs.end[0] : search_regs.start[0]); } else pos += stride_for_teases; } } /* We have done one clump. Can we continue? */ if ((lim - pos) * direction < 0) return ((0 - n) * direction); } return pos; }}/* Given a string of words separated by word delimiters, compute a regexp that matches those exact words separated by arbitrary punctuation. */static Lisp_Objectwordify (string) Lisp_Object string;{ register unsigned char *p, *o; register int i, len, punct_count = 0, word_count = 0; Lisp_Object val; CHECK_STRING (string, 0); p = XSTRING (string)->data; len = XSTRING (string)->size; for (i = 0; i < len; i++) if (SYNTAX (p[i]) != Sword) { punct_count++; if (i > 0 && SYNTAX (p[i-1]) == Sword) word_count++; } if (SYNTAX (p[len-1]) == Sword) word_count++; if (!word_count) return build_string (""); val = make_string (p, len - punct_count + 5 * (word_count - 1) + 4); o = XSTRING (val)->data; *o++ = '\\'; *o++ = 'b'; for (i = 0; i < len; i++) if (SYNTAX (p[i]) == Sword) *o++ = p[i]; else if (i > 0 && SYNTAX (p[i-1]) == Sword && --word_count) { *o++ = '\\'; *o++ = 'W'; *o++ = '\\'; *o++ = 'W'; *o++ = '*'; } *o++ = '\\'; *o++ = 'b'; return val;}DEFUN ("search-backward", Fsearch_backward, Ssearch_backward, 1, 4, "sSearch backward: ", "Search backward from point for STRING.\n\Set point to the beginning of the occurrence found, and return t.\n\An optional second argument bounds the search; it is a buffer position.\n\The match found must not extend before that position.\n\Optional third argument, if t, means if fail just return nil (no error).\n\ If not nil and not t, position at limit of search and return nil.\n\Optional fourth argument is repeat count--search for successive occurrences.") (string, bound, noerror, count) Lisp_Object string, bound, noerror, count;{ return search_command (string, bound, noerror, count, -1, 0);}DEFUN ("search-forward", Fsearch_forward, Ssearch_forward, 1, 4, "sSearch: ", "Search forward from point for STRING.\n\Set point to the end of the occurrence found, and return t.\n\An optional second argument bounds the search; it is a buffer position.\n\The match found must not extend after that position.\n\
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -