[git commit] awk: code shrink

Denys Vlasenko vda.linux at googlemail.com
Tue Jun 6 10:48:11 UTC 2023


commit: https://git.busybox.net/busybox/commit/?id=f4789164e0716a8b1f98cf4149a3eb2dad485b8b
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
awk_sub                                              544     548      +4
exec_builtin                                        1136    1130      -6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 4/-6)               Total: -2 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 editors/awk.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/editors/awk.c b/editors/awk.c
index f77573806..b3871ffc5 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -2494,7 +2494,7 @@ static char *awk_printf(node *n, size_t *len)
  * If src or dst is NULL, use $0.
  * If subexp != 0, enable subexpression matching (\0-\9).
  */
-static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int subexp)
+static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest /*,int subexp*/)
 {
 	char *resbuf;
 	const char *sp;
@@ -2502,6 +2502,8 @@ static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int
 	int regexec_flags;
 	regmatch_t pmatch[10];
 	regex_t sreg, *regex;
+	/* True only if called to implement gensub(): */
+	int subexp = (src != dest);
 
 	resbuf = NULL;
 	residx = 0;
@@ -2549,7 +2551,6 @@ static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int
 			}
 		}
 
-		regexec_flags = REG_NOTBOL;
 		sp += eo;
 		if (match_no == nm)
 			break;
@@ -2570,6 +2571,7 @@ static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int
 			sp++;
 			residx++;
 		}
+		regexec_flags = REG_NOTBOL;
 	}
 
 	resbuf = qrealloc(resbuf, residx + strlen(sp), &resbufsize);
@@ -2798,16 +2800,16 @@ static NOINLINE var *exec_builtin(node *op, var *res)
 		res = do_match(an[1], as[0]);
 		break;
 
-	case B_ge:
-		awk_sub(an[0], as[1], getvar_i(av[2]), av[3], res, TRUE);
+	case B_ge: /* gensub(regex, repl, matchnum, string) */
+		awk_sub(an[0], as[1], /*matchnum:*/getvar_i(av[2]), /*src:*/av[3], /*dst:*/res/*, TRUE*/);
 		break;
 
-	case B_gs:
-		setvar_i(res, awk_sub(an[0], as[1], 0, av[2], av[2], FALSE));
+	case B_gs: /* gsub(regex, repl, string) */
+		setvar_i(res, awk_sub(an[0], as[1], /*matchnum:all*/0, /*src:*/av[2], /*dst:*/av[2]/*, FALSE*/));
 		break;
 
-	case B_su:
-		setvar_i(res, awk_sub(an[0], as[1], 1, av[2], av[2], FALSE));
+	case B_su: /* sub(regex, repl, string) */
+		setvar_i(res, awk_sub(an[0], as[1], /*matchnum:first*/1, /*src:*/av[2], /*dst:*/av[2]/*, FALSE*/));
 		break;
 	}
 


More information about the busybox-cvs mailing list