awk: fix -F 'regex' bug (miscounted fields if last field is empty)
This commit is contained in:
parent
b78c782c85
commit
af1bd09625
@ -1523,8 +1523,11 @@ static int awk_split(const char *s, node *spl, char **slist)
|
|||||||
c[2] = '\n';
|
c[2] = '\n';
|
||||||
|
|
||||||
if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
|
if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
|
||||||
while (*s) {
|
if (!*s)
|
||||||
l = strcspn(s, c+2);
|
return n; /* "": zero fields */
|
||||||
|
n++; /* at least one field will be there */
|
||||||
|
do {
|
||||||
|
l = strcspn(s, c+2); /* len till next NUL or \n */
|
||||||
if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0
|
if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0
|
||||||
&& pmatch[0].rm_so <= l
|
&& pmatch[0].rm_so <= l
|
||||||
) {
|
) {
|
||||||
@ -1533,24 +1536,27 @@ static int awk_split(const char *s, node *spl, char **slist)
|
|||||||
l++;
|
l++;
|
||||||
pmatch[0].rm_eo++;
|
pmatch[0].rm_eo++;
|
||||||
}
|
}
|
||||||
|
n++; /* we saw yet another delimiter */
|
||||||
} else {
|
} else {
|
||||||
pmatch[0].rm_eo = l;
|
pmatch[0].rm_eo = l;
|
||||||
if (s[l]) pmatch[0].rm_eo++;
|
if (s[l]) pmatch[0].rm_eo++;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(s1, s, l);
|
memcpy(s1, s, l);
|
||||||
s1[l] = '\0';
|
s1[l] = '\0';
|
||||||
nextword(&s1);
|
nextword(&s1);
|
||||||
s += pmatch[0].rm_eo;
|
s += pmatch[0].rm_eo;
|
||||||
n++;
|
} while (*s);
|
||||||
|
return n;
|
||||||
}
|
}
|
||||||
} else if (c[0] == '\0') { /* null split */
|
if (c[0] == '\0') { /* null split */
|
||||||
while (*s) {
|
while (*s) {
|
||||||
*s1++ = *s++;
|
*s1++ = *s++;
|
||||||
*s1++ = '\0';
|
*s1++ = '\0';
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
} else if (c[0] != ' ') { /* single-character split */
|
return n;
|
||||||
|
}
|
||||||
|
if (c[0] != ' ') { /* single-character split */
|
||||||
if (icase) {
|
if (icase) {
|
||||||
c[0] = toupper(c[0]);
|
c[0] = toupper(c[0]);
|
||||||
c[1] = tolower(c[1]);
|
c[1] = tolower(c[1]);
|
||||||
@ -1560,7 +1566,9 @@ static int awk_split(const char *s, node *spl, char **slist)
|
|||||||
*s1++ = '\0';
|
*s1++ = '\0';
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
} else { /* space split */
|
return n;
|
||||||
|
}
|
||||||
|
/* space split */
|
||||||
while (*s) {
|
while (*s) {
|
||||||
s = skip_whitespace(s);
|
s = skip_whitespace(s);
|
||||||
if (!*s) break;
|
if (!*s) break;
|
||||||
@ -1569,12 +1577,12 @@ static int awk_split(const char *s, node *spl, char **slist)
|
|||||||
*s1++ = *s++;
|
*s1++ = *s++;
|
||||||
*s1++ = '\0';
|
*s1++ = '\0';
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void split_f0(void)
|
static void split_f0(void)
|
||||||
{
|
{
|
||||||
|
/* static char *fstrings; */
|
||||||
#define fstrings (G.split_f0__fstrings)
|
#define fstrings (G.split_f0__fstrings)
|
||||||
|
|
||||||
int i, n;
|
int i, n;
|
||||||
|
Loading…
Reference in New Issue
Block a user