qwk: code shrink

function                                             old     new   delta
mk_splitter                                          100      96      -4
as_regex                                             103      99      -4
parse_expr                                           991     986      -5
awk_split                                            544     538      -6
awk_getline                                          559     552      -7
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/5 up/down: 0/-26)             Total: -26 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2024-07-09 17:50:58 +02:00
parent 38335df9e9
commit 45d471d435

View file

@ -180,11 +180,11 @@ typedef struct node_s {
var *v; var *v;
int aidx; int aidx;
const char *new_progname; const char *new_progname;
/* if TI_REGEXP node, points to regex_t[2] array (case sensitive and insensitive) */
regex_t *re; regex_t *re;
} l; } l;
union { union {
struct node_s *n; struct node_s *n;
regex_t *ire;
func *f; func *f;
} r; } r;
union { union {
@ -1399,7 +1399,6 @@ static void mk_re_node(const char *s, node *n, regex_t *re)
{ {
n->info = TI_REGEXP; n->info = TI_REGEXP;
n->l.re = re; n->l.re = re;
n->r.ire = re + 1;
xregcomp(re, s, REG_EXTENDED); xregcomp(re, s, REG_EXTENDED);
xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE); xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE);
} }
@ -1412,13 +1411,13 @@ static node *parse_lrparen_list(void)
return parse_expr(TC_RPAREN); return parse_expr(TC_RPAREN);
} }
/* parse expression terminated by given argument, return ptr /* Parse expression terminated by given token, return ptr
* to built subtree. Terminator is eaten by parse_expr */ * to built subtree. Terminator is eaten by parse_expr */
static node *parse_expr(uint32_t term_tc) static node *parse_expr(uint32_t term_tc)
{ {
node sn; node sn;
node *cn = &sn; node *cn = &sn;
node *glptr; node *getline_node;
uint32_t tc, expected_tc; uint32_t tc, expected_tc;
debug_printf_parse("%s() term_tc(%x):", __func__, term_tc); debug_printf_parse("%s() term_tc(%x):", __func__, term_tc);
@ -1426,19 +1425,19 @@ static node *parse_expr(uint32_t term_tc)
debug_printf_parse("\n"); debug_printf_parse("\n");
sn.info = PRIMASK; sn.info = PRIMASK;
sn.r.n = sn.a.n = glptr = NULL; sn.r.n = sn.a.n = getline_node = NULL;
expected_tc = TS_OPERAND | TS_UOPPRE | TC_REGEXP | term_tc; expected_tc = TS_OPERAND | TS_UOPPRE | TC_REGEXP | term_tc;
while (!((tc = next_token(expected_tc)) & term_tc)) { while (!((tc = next_token(expected_tc)) & term_tc)) {
node *vn; node *vn;
if (glptr && (t_info == TI_LESS)) { if (getline_node && (t_info == TI_LESS)) {
/* input redirection (<) attached to glptr node */ /* Attach input redirection (<) to getline node */
debug_printf_parse("%s: input redir\n", __func__); debug_printf_parse("%s: input redir\n", __func__);
cn = glptr->l.n = new_node(OC_CONCAT | SS | PRECEDENCE(37)); cn = getline_node->l.n = new_node(OC_CONCAT | SS | PRECEDENCE(37));
cn->a.n = glptr; cn->a.n = getline_node;
expected_tc = TS_OPERAND | TS_UOPPRE; expected_tc = TS_OPERAND | TS_UOPPRE;
glptr = NULL; getline_node = NULL;
continue; continue;
} }
if (tc & (TS_BINOP | TC_UOPPOST)) { if (tc & (TS_BINOP | TC_UOPPOST)) {
@ -1485,19 +1484,21 @@ static node *parse_expr(uint32_t term_tc)
expected_tc = TS_OPERAND | TS_UOPPRE | TC_REGEXP; expected_tc = TS_OPERAND | TS_UOPPRE | TC_REGEXP;
if (t_info == TI_PGETLINE) { if (t_info == TI_PGETLINE) {
/* it's a pipe */ /* it's a pipe token "|" */
next_token(TC_GETLINE); next_token(TC_GETLINE);
/* give maximum priority to this pipe */ /* give maximum priority to this pipe */
cn->info &= ~PRIMASK; cn->info &= ~PRIMASK;
expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc; expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc;
} }
} else { } else {
/* It was an unary postfix operator */
cn->r.n = vn; cn->r.n = vn;
expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc; expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc;
} }
vn->a.n = cn; vn->a.n = cn;
continue; continue;
} }
/* It wasn't a binary or unary_postfix operator */
debug_printf_parse("%s: other, t_info:%x\n", __func__, t_info); debug_printf_parse("%s: other, t_info:%x\n", __func__, t_info);
/* for operands and prefix-unary operators, attach them /* for operands and prefix-unary operators, attach them
@ -1572,7 +1573,7 @@ static node *parse_expr(uint32_t term_tc)
case TC_GETLINE: case TC_GETLINE:
debug_printf_parse("%s: TC_GETLINE\n", __func__); debug_printf_parse("%s: TC_GETLINE\n", __func__);
glptr = cn; getline_node = cn;
expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc; expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc;
break; break;
@ -1944,15 +1945,14 @@ static void nvfree(var *v, int sz)
static node *mk_splitter(const char *s, tsplitter *spl) static node *mk_splitter(const char *s, tsplitter *spl)
{ {
regex_t *re, *ire; regex_t *re;
node *n; node *n;
re = &spl->re[0]; re = spl->re;
ire = &spl->re[1];
n = &spl->n; n = &spl->n;
if (n->info == TI_REGEXP) { if (n->info == TI_REGEXP) {
regfree(re); regfree(re);
regfree(ire); // TODO: nuke ire, use re+1? regfree(re + 1);
} }
if (s[0] && s[1]) { /* strlen(s) > 1 */ if (s[0] && s[1]) { /* strlen(s) > 1 */
mk_re_node(s, n, re); mk_re_node(s, n, re);
@ -1975,7 +1975,7 @@ static regex_t *as_regex(node *op, regex_t *preg)
const char *s; const char *s;
if (op->info == TI_REGEXP) { if (op->info == TI_REGEXP) {
return icase ? op->r.ire : op->l.re; return &op->l.re[icase];
} }
//tmpvar = nvalloc(1); //tmpvar = nvalloc(1);
@ -2093,7 +2093,7 @@ static int awk_split(const char *s, node *spl, char **slist)
regmatch_t pmatch[1]; regmatch_t pmatch[1];
l = strcspn(s, c+2); /* len till next NUL or \n */ l = strcspn(s, c+2); /* len till next NUL or \n */
if (regexec1_nonempty(icase ? spl->r.ire : spl->l.re, s, pmatch) == 0 if (regexec1_nonempty(&spl->l.re[icase], s, pmatch) == 0
&& pmatch[0].rm_so <= l && pmatch[0].rm_so <= l
) { ) {
/* if (pmatch[0].rm_eo == 0) ... - impossible */ /* if (pmatch[0].rm_eo == 0) ... - impossible */
@ -2348,7 +2348,7 @@ static int awk_getline(rstream *rsm, var *v)
if (p > 0) { if (p > 0) {
char c = (char) rsplitter.n.info; char c = (char) rsplitter.n.info;
if (rsplitter.n.info == TI_REGEXP) { if (rsplitter.n.info == TI_REGEXP) {
if (regexec(icase ? rsplitter.n.r.ire : rsplitter.n.l.re, if (regexec(&rsplitter.n.l.re[icase],
b, 1, pmatch, 0) == 0 b, 1, pmatch, 0) == 0
) { ) {
so = pmatch[0].rm_so; so = pmatch[0].rm_so;