awk: code shrink

function                                             old     new   delta
fsrealloc                                            112     107      -5
next_token                                           862     844     -18

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
This commit is contained in:
Denys Vlasenko 2010-10-05 16:49:03 +02:00
parent d527e0c81d
commit 28458c64db

View File

@ -987,7 +987,6 @@ static uint32_t next_token(uint32_t expected)
const char *tl; const char *tl;
uint32_t tc; uint32_t tc;
const uint32_t *ti; const uint32_t *ti;
int l;
if (t_rollback) { if (t_rollback) {
t_rollback = FALSE; t_rollback = FALSE;
@ -1053,7 +1052,7 @@ static uint32_t next_token(uint32_t expected)
char *pp = p; char *pp = p;
t_double = my_strtod(&pp); t_double = my_strtod(&pp);
p = pp; p = pp;
if (*pp == '.') if (*p == '.')
syntax_error(EMSG_UNEXP_TOKEN); syntax_error(EMSG_UNEXP_TOKEN);
tc = TC_NUMBER; tc = TC_NUMBER;
@ -1063,52 +1062,51 @@ static uint32_t next_token(uint32_t expected)
tc = 0x00000001; tc = 0x00000001;
ti = tokeninfo; ti = tokeninfo;
while (*tl) { while (*tl) {
l = *tl++; int l = (unsigned char) *tl++;
if (l == NTCC) { if (l == (unsigned char) NTCC) {
tc <<= 1; tc <<= 1;
continue; continue;
} }
/* if token class is expected, token /* if token class is expected,
* matches and it's not a longer word, * token matches,
* then this is what we are looking for * and it's not a longer word,
*/ */
if ((tc & (expected | TC_WORD | TC_NEWLINE)) if ((tc & (expected | TC_WORD | TC_NEWLINE))
&& *tl == *p && strncmp(p, tl, l) == 0 && strncmp(p, tl, l) == 0
&& !((tc & TC_WORD) && isalnum_(p[l])) && !((tc & TC_WORD) && isalnum_(p[l]))
) { ) {
/* then this is what we are looking for */
t_info = *ti; t_info = *ti;
p += l; p += l;
break; goto token_found;
} }
ti++; ti++;
tl += l; tl += l;
} }
/* not a known token */
if (!*tl) { /* is it a name? (var/array/function) */
/* it's a name (var/array/function), if (!isalnum_(*p))
* otherwise it's something wrong syntax_error(EMSG_UNEXP_TOKEN); /* no */
*/ /* yes */
if (!isalnum_(*p)) t_string = --p;
syntax_error(EMSG_UNEXP_TOKEN); while (isalnum_(*++p)) {
p[-1] = *p;
t_string = --p; }
while (isalnum_(*++p)) { p[-1] = '\0';
p[-1] = *p; tc = TC_VARIABLE;
} /* also consume whitespace between functionname and bracket */
p[-1] = '\0'; if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY))
tc = TC_VARIABLE; p = skip_spaces(p);
/* also consume whitespace between functionname and bracket */ if (*p == '(') {
if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY)) tc = TC_FUNCTION;
p = skip_spaces(p); } else {
if (*p == '(') { if (*p == '[') {
tc = TC_FUNCTION; p++;
} else { tc = TC_ARRAY;
if (*p == '[') {
p++;
tc = TC_ARRAY;
}
} }
} }
token_found: ;
} }
g_pos = p; g_pos = p;
@ -1186,6 +1184,7 @@ static node *parse_expr(uint32_t iexp)
xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp; xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp;
while (!((tc = next_token(xtc)) & iexp)) { while (!((tc = next_token(xtc)) & iexp)) {
if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) { if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) {
/* input redirection (<) attached to glptr node */ /* input redirection (<) attached to glptr node */
cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37)); cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37));
@ -1522,10 +1521,10 @@ static node *mk_splitter(const char *s, tsplitter *spl)
regfree(re); regfree(re);
regfree(ire); // TODO: nuke ire, use re+1? regfree(ire); // TODO: nuke ire, use re+1?
} }
if (strlen(s) > 1) { if (s[0] && s[1]) { /* strlen(s) > 1 */
mk_re_node(s, n, re); mk_re_node(s, n, re);
} else { } else {
n->info = (uint32_t) *s; n->info = (uint32_t) s[0];
} }
return n; return n;
@ -1582,24 +1581,22 @@ static void fsrealloc(int size)
if (size >= maxfields) { if (size >= maxfields) {
i = maxfields; i = maxfields;
maxfields = size + 16; maxfields = size + 16;
Fields = xrealloc(Fields, maxfields * sizeof(var)); Fields = xrealloc(Fields, maxfields * sizeof(Fields[0]));
for (; i < maxfields; i++) { for (; i < maxfields; i++) {
Fields[i].type = VF_SPECIAL; Fields[i].type = VF_SPECIAL;
Fields[i].string = NULL; Fields[i].string = NULL;
} }
} }
/* if size < nfields, clear extra field variables */
if (size < nfields) { for (i = size; i < nfields; i++) {
for (i = size; i < nfields; i++) { clrvar(Fields + i);
clrvar(Fields + i);
}
} }
nfields = size; nfields = size;
} }
static int awk_split(const char *s, node *spl, char **slist) static int awk_split(const char *s, node *spl, char **slist)
{ {
int l, n = 0; int l, n;
char c[4]; char c[4];
char *s1; char *s1;
regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough... regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough...
@ -1613,6 +1610,7 @@ static int awk_split(const char *s, node *spl, char **slist)
if (*getvar_s(intvar[RS]) == '\0') if (*getvar_s(intvar[RS]) == '\0')
c[2] = '\n'; c[2] = '\n';
n = 0;
if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */ if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
if (!*s) if (!*s)
return n; /* "": zero fields */ return n; /* "": zero fields */
@ -1658,7 +1656,7 @@ static int awk_split(const char *s, node *spl, char **slist)
} }
if (*s1) if (*s1)
n++; n++;
while ((s1 = strpbrk(s1, c))) { while ((s1 = strpbrk(s1, c)) != NULL) {
*s1++ = '\0'; *s1++ = '\0';
n++; n++;
} }