vi: speed up some string ops

This commit is contained in:
Denis Vlasenko 2007-12-23 02:36:51 +00:00
parent 528a8b9769
commit f882f08627

View File

@ -1162,9 +1162,9 @@ static void Hit_Return(void)
{ {
char c; char c;
standout_start(); // start reverse video standout_start();
write1("[Hit return to continue]"); write1("[Hit return to continue]");
standout_end(); // end reverse video standout_end();
while ((c = get_one_char()) != '\n' && c != '\r') while ((c = get_one_char()) != '\n' && c != '\r')
continue; continue;
redraw(TRUE); // force redraw all redraw(TRUE); // force redraw all
@ -1179,14 +1179,11 @@ static int next_tabstop(int col)
static void sync_cursor(char *d, int *row, int *col) static void sync_cursor(char *d, int *row, int *col)
{ {
char *beg_cur; // begin and end of "d" line char *beg_cur; // begin and end of "d" line
char *end_scr; // begin and end of screen
char *tp; char *tp;
int cnt, ro, co; int cnt, ro, co;
beg_cur = begin_line(d); // first char of cur line beg_cur = begin_line(d); // first char of cur line
end_scr = end_screen(); // last char of screen
if (beg_cur < screenbegin) { if (beg_cur < screenbegin) {
// "d" is before top line on screen // "d" is before top line on screen
// how many lines do we have to move // how many lines do we have to move
@ -1199,7 +1196,10 @@ static void sync_cursor(char *d, int *row, int *col)
screenbegin = prev_line(screenbegin); screenbegin = prev_line(screenbegin);
} }
} }
} else if (beg_cur > end_scr) { } else {
char *end_scr; // begin and end of screen
end_scr = end_screen(); // last char of screen
if (beg_cur > end_scr) {
// "d" is after bottom line on screen // "d" is after bottom line on screen
// how many lines do we have to move // how many lines do we have to move
cnt = count_lines(end_scr, beg_cur); cnt = count_lines(end_scr, beg_cur);
@ -1213,6 +1213,7 @@ static void sync_cursor(char *d, int *row, int *col)
end_scr = end_line(end_scr); end_scr = end_line(end_scr);
} }
} }
}
// "d" is on screen- find out which row // "d" is on screen- find out which row
tp = screenbegin; tp = screenbegin;
for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
@ -1273,22 +1274,28 @@ static void sync_cursor(char *d, int *row, int *col)
//----- Text Movement Routines --------------------------------- //----- Text Movement Routines ---------------------------------
static char *begin_line(char *p) // return pointer to first char cur line static char *begin_line(char *p) // return pointer to first char cur line
{ {
while (p > text && p[-1] != '\n') if (p > text) {
p--; // go to cur line B-o-l p = memrchr(text, '\n', p - text);
if (!p)
return text;
return p + 1;
}
return p; return p;
} }
static char *end_line(char *p) // return pointer to NL of cur line line static char *end_line(char *p) // return pointer to NL of cur line line
{ {
while (p < end - 1 && *p != '\n') if (p < end - 1) {
p++; // go to cur line E-o-l p = memchr(p, '\n', end - p - 1);
if (!p)
return end - 1;
}
return p; return p;
} }
static char *dollar_line(char *p) // return pointer to just before NL line static char *dollar_line(char *p) // return pointer to just before NL line
{ {
while (p < end - 1 && *p != '\n') p = end_line(p);
p++; // go to cur line E-o-l
// Try to stay off of the Newline // Try to stay off of the Newline
if (*p == '\n' && (p - begin_line(p)) > 0) if (*p == '\n' && (p - begin_line(p)) > 0)
p--; p--;
@ -1326,7 +1333,8 @@ static char *end_screen(void)
return q; return q;
} }
static int count_lines(char * start, char * stop) // count line from start to stop // count line from start to stop
static int count_lines(char *start, char *stop)
{ {
char *q; char *q;
int cnt; int cnt;
@ -1337,10 +1345,12 @@ static int count_lines(char * start, char * stop) // count line from start to st
stop = q; stop = q;
} }
cnt = 0; cnt = 0;
stop = end_line(stop); // get to end of this line stop = end_line(stop);
for (q = start; q <= stop && q <= end - 1; q++) { while (start <= stop && start <= end - 1) {
if (*q == '\n') start = end_line(start);
if (*start == '\n')
cnt++; cnt++;
start++;
} }
return cnt; return cnt;
} }
@ -2837,12 +2847,16 @@ static void refresh(int full_screen)
// compare text[] to screen[] and mark screen[] lines that need updating // compare text[] to screen[] and mark screen[] lines that need updating
for (li = 0; li < rows - 1; li++) { for (li = 0; li < rows - 1; li++) {
int cs, ce; // column start & end int cs, ce; // column start & end
char *out_buf;
// format current text line // format current text line
char *out_buf = format_line(tp, li); out_buf = format_line(tp, li);
// skip to the end of the current text[] line // skip to the end of the current text[] line
while (tp < end && *tp++ != '\n') if (tp < end) {
continue; char *t = memchr(tp, '\n', end - tp);
if (!t) t = end - 1;
tp = t + 1;
}
// see if there are any changes between vitual screen and out_buf // see if there are any changes between vitual screen and out_buf
changed = FALSE; // assume no change changed = FALSE; // assume no change