Optimize string concatenation.

This commit is contained in:
Arnold D. Robbins 2019-10-24 10:06:10 -04:00
parent 8b92a4abcb
commit 1d6ddfd9c0
2 changed files with 7 additions and 4 deletions

2
FIXES
View File

@ -28,6 +28,8 @@ was sent to the printers in August, 1987.
October 24, 2019:
Import second round of code cleanups from NetBSD. Much thanks
to Christos Zoulas (Github user zoulasc). Merges PR 53.
Add an optimization for string concatenation, also from
Christos.
October 17, 2019:
Import code cleanups from NetBSD. Much thanks to Christos

9
run.c
View File

@ -1196,13 +1196,14 @@ Cell *cat(Node **a, int q) /* a[0] cat a[1] */
x = execute(a[0]);
n1 = strlen(getsval(x));
adjbuf(&s, &ssz, n1 + 1, recsize, 0, "cat1");
(void) strncpy(s, x->sval, ssz);
y = execute(a[1]);
n2 = strlen(getsval(y));
adjbuf(&s, &ssz, n1 + n2 + 1, recsize, 0, "cat2");
(void) strncpy(s + n1, y->sval, ssz - n1);
adjbuf(&s, &ssz, n1 + n2 + 1, recsize, 0, "cat");
memcpy(s, x->sval, n1);
memcpy(s + n1, y->sval, n2);
s[n1 + n2] = '\0';
tempfree(x);
tempfree(y);