diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2024-07-09 17:50:58 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2024-07-09 17:50:58 +0200 |
commit | 45d471d435a335b172724c53fff41957adb22885 (patch) | |
tree | bd65fe5edab0b437a6fd731e9b58236dc571d7e4 | |
parent | 38335df9e9f45378c3407defd38b5b610578bdda (diff) | |
download | busybox-w32-45d471d435a335b172724c53fff41957adb22885.tar.gz busybox-w32-45d471d435a335b172724c53fff41957adb22885.tar.bz2 busybox-w32-45d471d435a335b172724c53fff41957adb22885.zip |
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>
-rw-r--r-- | editors/awk.c | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/editors/awk.c b/editors/awk.c index 697a44c8c..cf5173938 100644 --- a/editors/awk.c +++ b/editors/awk.c | |||
@@ -180,11 +180,11 @@ typedef struct node_s { | |||
180 | var *v; | 180 | var *v; |
181 | int aidx; | 181 | int aidx; |
182 | const char *new_progname; | 182 | const char *new_progname; |
183 | /* if TI_REGEXP node, points to regex_t[2] array (case sensitive and insensitive) */ | ||
183 | regex_t *re; | 184 | regex_t *re; |
184 | } l; | 185 | } l; |
185 | union { | 186 | union { |
186 | struct node_s *n; | 187 | struct node_s *n; |
187 | regex_t *ire; | ||
188 | func *f; | 188 | func *f; |
189 | } r; | 189 | } r; |
190 | union { | 190 | union { |
@@ -1399,7 +1399,6 @@ static void mk_re_node(const char *s, node *n, regex_t *re) | |||
1399 | { | 1399 | { |
1400 | n->info = TI_REGEXP; | 1400 | n->info = TI_REGEXP; |
1401 | n->l.re = re; | 1401 | n->l.re = re; |
1402 | n->r.ire = re + 1; | ||
1403 | xregcomp(re, s, REG_EXTENDED); | 1402 | xregcomp(re, s, REG_EXTENDED); |
1404 | xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE); | 1403 | xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE); |
1405 | } | 1404 | } |
@@ -1412,13 +1411,13 @@ static node *parse_lrparen_list(void) | |||
1412 | return parse_expr(TC_RPAREN); | 1411 | return parse_expr(TC_RPAREN); |
1413 | } | 1412 | } |
1414 | 1413 | ||
1415 | /* parse expression terminated by given argument, return ptr | 1414 | /* Parse expression terminated by given token, return ptr |
1416 | * to built subtree. Terminator is eaten by parse_expr */ | 1415 | * to built subtree. Terminator is eaten by parse_expr */ |
1417 | static node *parse_expr(uint32_t term_tc) | 1416 | static node *parse_expr(uint32_t term_tc) |
1418 | { | 1417 | { |
1419 | node sn; | 1418 | node sn; |
1420 | node *cn = &sn; | 1419 | node *cn = &sn; |
1421 | node *glptr; | 1420 | node *getline_node; |
1422 | uint32_t tc, expected_tc; | 1421 | uint32_t tc, expected_tc; |
1423 | 1422 | ||
1424 | debug_printf_parse("%s() term_tc(%x):", __func__, term_tc); | 1423 | debug_printf_parse("%s() term_tc(%x):", __func__, term_tc); |
@@ -1426,19 +1425,19 @@ static node *parse_expr(uint32_t term_tc) | |||
1426 | debug_printf_parse("\n"); | 1425 | debug_printf_parse("\n"); |
1427 | 1426 | ||
1428 | sn.info = PRIMASK; | 1427 | sn.info = PRIMASK; |
1429 | sn.r.n = sn.a.n = glptr = NULL; | 1428 | sn.r.n = sn.a.n = getline_node = NULL; |
1430 | expected_tc = TS_OPERAND | TS_UOPPRE | TC_REGEXP | term_tc; | 1429 | expected_tc = TS_OPERAND | TS_UOPPRE | TC_REGEXP | term_tc; |
1431 | 1430 | ||
1432 | while (!((tc = next_token(expected_tc)) & term_tc)) { | 1431 | while (!((tc = next_token(expected_tc)) & term_tc)) { |
1433 | node *vn; | 1432 | node *vn; |
1434 | 1433 | ||
1435 | if (glptr && (t_info == TI_LESS)) { | 1434 | if (getline_node && (t_info == TI_LESS)) { |
1436 | /* input redirection (<) attached to glptr node */ | 1435 | /* Attach input redirection (<) to getline node */ |
1437 | debug_printf_parse("%s: input redir\n", __func__); | 1436 | debug_printf_parse("%s: input redir\n", __func__); |
1438 | cn = glptr->l.n = new_node(OC_CONCAT | SS | PRECEDENCE(37)); | 1437 | cn = getline_node->l.n = new_node(OC_CONCAT | SS | PRECEDENCE(37)); |
1439 | cn->a.n = glptr; | 1438 | cn->a.n = getline_node; |
1440 | expected_tc = TS_OPERAND | TS_UOPPRE; | 1439 | expected_tc = TS_OPERAND | TS_UOPPRE; |
1441 | glptr = NULL; | 1440 | getline_node = NULL; |
1442 | continue; | 1441 | continue; |
1443 | } | 1442 | } |
1444 | if (tc & (TS_BINOP | TC_UOPPOST)) { | 1443 | if (tc & (TS_BINOP | TC_UOPPOST)) { |
@@ -1485,19 +1484,21 @@ static node *parse_expr(uint32_t term_tc) | |||
1485 | 1484 | ||
1486 | expected_tc = TS_OPERAND | TS_UOPPRE | TC_REGEXP; | 1485 | expected_tc = TS_OPERAND | TS_UOPPRE | TC_REGEXP; |
1487 | if (t_info == TI_PGETLINE) { | 1486 | if (t_info == TI_PGETLINE) { |
1488 | /* it's a pipe */ | 1487 | /* it's a pipe token "|" */ |
1489 | next_token(TC_GETLINE); | 1488 | next_token(TC_GETLINE); |
1490 | /* give maximum priority to this pipe */ | 1489 | /* give maximum priority to this pipe */ |
1491 | cn->info &= ~PRIMASK; | 1490 | cn->info &= ~PRIMASK; |
1492 | expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc; | 1491 | expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc; |
1493 | } | 1492 | } |
1494 | } else { | 1493 | } else { |
1494 | /* It was an unary postfix operator */ | ||
1495 | cn->r.n = vn; | 1495 | cn->r.n = vn; |
1496 | expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc; | 1496 | expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc; |
1497 | } | 1497 | } |
1498 | vn->a.n = cn; | 1498 | vn->a.n = cn; |
1499 | continue; | 1499 | continue; |
1500 | } | 1500 | } |
1501 | /* It wasn't a binary or unary_postfix operator */ | ||
1501 | 1502 | ||
1502 | debug_printf_parse("%s: other, t_info:%x\n", __func__, t_info); | 1503 | debug_printf_parse("%s: other, t_info:%x\n", __func__, t_info); |
1503 | /* for operands and prefix-unary operators, attach them | 1504 | /* for operands and prefix-unary operators, attach them |
@@ -1572,7 +1573,7 @@ static node *parse_expr(uint32_t term_tc) | |||
1572 | 1573 | ||
1573 | case TC_GETLINE: | 1574 | case TC_GETLINE: |
1574 | debug_printf_parse("%s: TC_GETLINE\n", __func__); | 1575 | debug_printf_parse("%s: TC_GETLINE\n", __func__); |
1575 | glptr = cn; | 1576 | getline_node = cn; |
1576 | expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc; | 1577 | expected_tc = TS_OPERAND | TS_UOPPRE | TS_BINOP | term_tc; |
1577 | break; | 1578 | break; |
1578 | 1579 | ||
@@ -1944,15 +1945,14 @@ static void nvfree(var *v, int sz) | |||
1944 | 1945 | ||
1945 | static node *mk_splitter(const char *s, tsplitter *spl) | 1946 | static node *mk_splitter(const char *s, tsplitter *spl) |
1946 | { | 1947 | { |
1947 | regex_t *re, *ire; | 1948 | regex_t *re; |
1948 | node *n; | 1949 | node *n; |
1949 | 1950 | ||
1950 | re = &spl->re[0]; | 1951 | re = spl->re; |
1951 | ire = &spl->re[1]; | ||
1952 | n = &spl->n; | 1952 | n = &spl->n; |
1953 | if (n->info == TI_REGEXP) { | 1953 | if (n->info == TI_REGEXP) { |
1954 | regfree(re); | 1954 | regfree(re); |
1955 | regfree(ire); // TODO: nuke ire, use re+1? | 1955 | regfree(re + 1); |
1956 | } | 1956 | } |
1957 | if (s[0] && s[1]) { /* strlen(s) > 1 */ | 1957 | if (s[0] && s[1]) { /* strlen(s) > 1 */ |
1958 | mk_re_node(s, n, re); | 1958 | mk_re_node(s, n, re); |
@@ -1975,7 +1975,7 @@ static regex_t *as_regex(node *op, regex_t *preg) | |||
1975 | const char *s; | 1975 | const char *s; |
1976 | 1976 | ||
1977 | if (op->info == TI_REGEXP) { | 1977 | if (op->info == TI_REGEXP) { |
1978 | return icase ? op->r.ire : op->l.re; | 1978 | return &op->l.re[icase]; |
1979 | } | 1979 | } |
1980 | 1980 | ||
1981 | //tmpvar = nvalloc(1); | 1981 | //tmpvar = nvalloc(1); |
@@ -2093,7 +2093,7 @@ static int awk_split(const char *s, node *spl, char **slist) | |||
2093 | regmatch_t pmatch[1]; | 2093 | regmatch_t pmatch[1]; |
2094 | 2094 | ||
2095 | l = strcspn(s, c+2); /* len till next NUL or \n */ | 2095 | l = strcspn(s, c+2); /* len till next NUL or \n */ |
2096 | if (regexec1_nonempty(icase ? spl->r.ire : spl->l.re, s, pmatch) == 0 | 2096 | if (regexec1_nonempty(&spl->l.re[icase], s, pmatch) == 0 |
2097 | && pmatch[0].rm_so <= l | 2097 | && pmatch[0].rm_so <= l |
2098 | ) { | 2098 | ) { |
2099 | /* if (pmatch[0].rm_eo == 0) ... - impossible */ | 2099 | /* if (pmatch[0].rm_eo == 0) ... - impossible */ |
@@ -2348,7 +2348,7 @@ static int awk_getline(rstream *rsm, var *v) | |||
2348 | if (p > 0) { | 2348 | if (p > 0) { |
2349 | char c = (char) rsplitter.n.info; | 2349 | char c = (char) rsplitter.n.info; |
2350 | if (rsplitter.n.info == TI_REGEXP) { | 2350 | if (rsplitter.n.info == TI_REGEXP) { |
2351 | if (regexec(icase ? rsplitter.n.r.ire : rsplitter.n.l.re, | 2351 | if (regexec(&rsplitter.n.l.re[icase], |
2352 | b, 1, pmatch, 0) == 0 | 2352 | b, 1, pmatch, 0) == 0 |
2353 | ) { | 2353 | ) { |
2354 | so = pmatch[0].rm_so; | 2354 | so = pmatch[0].rm_so; |