char * binary_search(char *front, char *back) { char *p; p = front + (back - front) / 2; SKIP_PAST_NEWLINE(p, back); /* * If the file changes underneath us, make sure we don't * infinitely loop. */ while (p < back && back > front) { if (compare(p, back) == GREATER) front = p; else back = p; p = front + (back - front) / 2; SKIP_PAST_NEWLINE(p, back); } return (front); }
static char * binary_search(char *string, uim_look_ctx *ctx) { char *p; char *front = ctx->front, *back = ctx->back; p = front + (back - front) / 2; SKIP_PAST_NEWLINE(p, back); /* * If the file changes underneath us, make sure we don't * infinitely loop. */ while (p < back && back > front) { if (compare(string, p, ctx) == GREATER) front = p; else back = p; p = front + (back - front) / 2; SKIP_PAST_NEWLINE(p, back); } return (front); }
/* * Find the first line that starts with string, linearly searching from front * to back. * * Return NULL for no such line. * * This routine assumes: * * o front points at the first character in a line. * o front is before or at the first line to be printed. */ char * linear_search(wchar_t *string, unsigned char *front, unsigned char *back) { while (front < back) { switch (compare(string, front, back)) { case EQUAL: /* Found it. */ return (front); case LESS: /* No such string. */ return (NULL); case GREATER: /* Keep going. */ break; } SKIP_PAST_NEWLINE(front, back); } return (NULL); }
/* * Print as many lines as match string, starting at front. */ void print_from(char *front, char *back) { int eol; while (front < back && compare(front, back) == EQUAL) { if (compare(front, back) == EQUAL) { eol = 0; while (front < back && !eol) { if (putchar(*front) == EOF) err(EXIT_FAILURE, "stdout"); if (*front++ == '\n') eol = 1; } } else SKIP_PAST_NEWLINE(front, back); } }
/* * Find the first line that starts with string, linearly searching from front * to back. * * Return NULL for no such line. * * This routine assumes: * * o front points at the first character in a line. * o front is before or at the first line to be printed. */ static char * linear_search(char *string, uim_look_ctx *ctx) { char *front = ctx->front, *back = ctx->back; while (front < back) { switch (compare(string, front, ctx)) { case EQUAL: /* Found it. */ return (front); break; case LESS: /* No such string. */ return (NULL); break; case GREATER: /* Keep going. */ break; } SKIP_PAST_NEWLINE(front, back); } return (NULL); }