Fix pattern substitution

Ed was falling doing substitution different of the first or all
(s//%/, s//%/\1, s//%/g), because it was not adding the matches
which were not going to be substituted.
This commit is contained in:
Roberto E. Vargas Caballero 2016-01-07 11:44:49 +01:00 committed by sin
parent 54ad6d512b
commit 0b117ab6c0

26
ed.c
View File

@ -957,12 +957,20 @@ addpost(char **s, size_t *cap, size_t *siz)
*s = addchar('\0', *s, cap, siz); *s = addchar('\0', *s, cap, siz);
} }
static void static int
addsub(char **s, size_t *cap, size_t *siz) addsub(char **s, size_t *cap, size_t *siz, int nth, int nmatch)
{ {
char *end, *q, *p, c; char *end, *q, *p, c;
int sub; int sub;
if (nth != nmatch && nth != -1) {
q = lastmatch + matchs[0].rm_so;
end = lastmatch + matchs[0].rm_eo;
while (q < end)
*s = addchar(*q++, *s, cap, siz);
return 0;
}
for (p = rhs; (c = *p); ++p) { for (p = rhs; (c = *p); ++p) {
switch (c) { switch (c) {
case '&': case '&':
@ -970,7 +978,7 @@ addsub(char **s, size_t *cap, size_t *siz)
goto copy_match; goto copy_match;
case '\\': case '\\':
if ((c = *++p) == '\0') if ((c = *++p) == '\0')
return; return 1;
if (!isdigit(c)) if (!isdigit(c))
goto copy_char; goto copy_char;
sub = c - '0'; sub = c - '0';
@ -986,24 +994,20 @@ addsub(char **s, size_t *cap, size_t *siz)
break; break;
} }
} }
return 1;
} }
static void static void
subline(int num, int nth) subline(int num, int nth)
{ {
int m, changed; int i, m, changed;
static char *s; static char *s;
static size_t siz, cap; static size_t siz, cap;
siz = 0; i = changed = siz = 0;
for (m = match(num); m; m = rematch(num)) { for (m = match(num); m; m = rematch(num)) {
addpre(&s, &cap, &siz); addpre(&s, &cap, &siz);
if (--nth > 0) changed |= addsub(&s, &cap, &siz, nth, ++i);
continue;
changed = 1;
addsub(&s, &cap, &siz);
if (nth == 0)
break;
} }
if (!changed) if (!changed)
return; return;