commit 80a8c2d192e1c37ac792ce892eb63a8aea992959
parent 7ae8ccd4ae5a9e2876fee14b171d5b2e0b53b7a6
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sat, 12 Dec 2015 20:31:07 +0100
Convert match() into a match function
Match() was a function that looked up for
the first line which matched the current
regular expression, and in fact, it was
a search function instead of a match.
Diffstat:
M | ed.c | | | 31 | ++++++++++++++++++------------- |
1 file changed, 18 insertions(+), 13 deletions(-)
diff --git a/ed.c b/ed.c
@@ -208,12 +208,9 @@ repeat:
}
static void
-setglobal(int i, int dir)
+setglobal(int i, int v)
{
- int lnk = getindex(i);
- int m = !regexec(pattern, gettxt(i), 0, NULL, 0);
-
- zero[lnk].global = (m == dir && i >= line1 && i <= line2);
+ zero[getindex(i)].global = v;
}
static void
@@ -321,14 +318,20 @@ compile(int delim)
}
static int
-match(int way)
+match(int num)
+{
+ return !regexec(pattern, gettxt(num), 0, NULL, 0);
+}
+
+static int
+search(int way)
{
int i;
i = curln;
do {
i = (way == '?') ? prevln(i) : nextln(i);
- if (!regexec(pattern, gettxt(i), 0, NULL, 0))
+ if (match(i))
return i;
} while (i != curln);
@@ -389,7 +392,7 @@ linenum(int *line)
case '?':
case '/':
compile(c);
- ln = match(c);
+ ln = search(c);
break;
case '^':
case '-':
@@ -1118,7 +1121,7 @@ save_last_cmd:
static int
chkglobal(void)
{
- int delim, c, nmatch, i;
+ int delim, c, dir, i, v;
uflag = 1;
gflag = 0;
@@ -1128,12 +1131,12 @@ chkglobal(void)
case 'g':
uflag = 0;
case 'G':
- nmatch = 1;
+ dir = 1;
break;
case 'v':
uflag = 0;
case 'V':
- nmatch = 0;
+ dir = 0;
break;
default:
back(c);
@@ -1144,8 +1147,10 @@ chkglobal(void)
delim = input();
compile(delim);
- for (i = line1; i <= line2; i++)
- setglobal(i, nmatch);
+ for (i = 1; i <= lastln; i = nextln(i)) {
+ v = i >= line1 && i <= line2 && match(i) == dir;
+ setglobal(i, v);
+ }
return 1;
}