aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog9
-rw-r--r--builtin.c12
2 files changed, 16 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index ebb1f1f2..05aaacd0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2014-08-13 Arnold D. Robbins <arnold@skeeve.com>
+
+ * builtin.c (do_sub): Move initial allocation of the replacement
+ string down towards code to do the replacement, with a (we hope)
+ better guesstimate of how much to initially allocate. The idea
+ is to avoid unnecessary realloc() calls by making a better guess
+ at how much to allocate. This came up in an email discussion
+ with Tom Dickey about mawk's gsub().
+
2014-08-12 Arnold D. Robbins <arnold@skeeve.com>
OFS being set should rebuild $0 using previous OFS if $0
diff --git a/builtin.c b/builtin.c
index 077575fa..0995fdf7 100644
--- a/builtin.c
+++ b/builtin.c
@@ -2786,16 +2786,11 @@ set_how_many:
text = t->stptr;
textlen = t->stlen;
- buflen = textlen + 2;
repl = s->stptr;
replend = repl + s->stlen;
repllen = replend - repl;
- emalloc(buf, char *, buflen + 2, "do_sub");
- buf[buflen] = '\0';
- buf[buflen + 1] = '\0';
-
ampersands = 0;
/*
@@ -2854,6 +2849,13 @@ set_how_many:
}
lastmatchnonzero = false;
+
+ /* guesstimate how much room to allocate; +2 forces > 0 */
+ buflen = textlen + (ampersands + 1) * repllen + 2;
+ emalloc(buf, char *, buflen + 2, "do_sub");
+ buf[buflen] = '\0';
+ buf[buflen + 1] = '\0';
+
bp = buf;
for (current = 1;; current++) {
matches++;