aboutsummaryrefslogtreecommitdiff
path: root/editors/awk.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-03-10 19:21:54 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-03-10 19:21:54 +0100
commitc9955f23a2a57c9822fc1dba5f3d1487dd8aacbc (patch)
tree9838e6e3d36472c4ffa8fdcb857ab63b364076c1 /editors/awk.c
parent3cb60c39737208ca073842f4f0386d5e5c278705 (diff)
downloadbusybox-w32-c9955f23a2a57c9822fc1dba5f3d1487dd8aacbc.tar.gz
busybox-w32-c9955f23a2a57c9822fc1dba5f3d1487dd8aacbc.tar.bz2
busybox-w32-c9955f23a2a57c9822fc1dba5f3d1487dd8aacbc.zip
awk: simple code shrink
function old new delta awk_getline 710 724 +14 awk_printf 455 457 +2 hash_find 234 233 -1 qrealloc 33 31 -2 handle_special 499 482 -17 awk_sub 641 601 -40 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/4 up/down: 16/-60) Total: -44 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'editors/awk.c')
-rw-r--r--editors/awk.c24
1 files changed, 13 insertions, 11 deletions
diff --git a/editors/awk.c b/editors/awk.c
index 5cc4adce7..1d704e87c 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -1513,12 +1513,13 @@ static regex_t *as_regex(node *op, regex_t *preg)
1513} 1513}
1514 1514
1515/* gradually increasing buffer */ 1515/* gradually increasing buffer */
1516static void qrealloc(char **b, int n, int *size) 1516static char* qrealloc(char *b, int n, int *size)
1517{ 1517{
1518 if (!*b || n >= *size) { 1518 if (!b || n >= *size) {
1519 *size = n + (n>>1) + 80; 1519 *size = n + (n>>1) + 80;
1520 *b = xrealloc(*b, *size); 1520 b = xrealloc(b, *size);
1521 } 1521 }
1522 return b;
1522} 1523}
1523 1524
1524/* resize field storage space */ 1525/* resize field storage space */
@@ -1678,7 +1679,7 @@ static void handle_special(var *v)
1678 memcpy(b+len, sep, sl); 1679 memcpy(b+len, sep, sl);
1679 len += sl; 1680 len += sl;
1680 } 1681 }
1681 qrealloc(&b, len+l+sl, &bsize); 1682 b = qrealloc(b, len+l+sl, &bsize);
1682 memcpy(b+len, s, l); 1683 memcpy(b+len, s, l);
1683 len += l; 1684 len += l;
1684 } 1685 }
@@ -1790,7 +1791,8 @@ static int awk_getline(rstream *rsm, var *v)
1790 c = (char) rsplitter.n.info; 1791 c = (char) rsplitter.n.info;
1791 rp = 0; 1792 rp = 0;
1792 1793
1793 if (!m) qrealloc(&m, 256, &size); 1794 if (!m)
1795 m = qrealloc(m, 256, &size);
1794 do { 1796 do {
1795 b = m + a; 1797 b = m + a;
1796 so = eo = p; 1798 so = eo = p;
@@ -1831,7 +1833,7 @@ static int awk_getline(rstream *rsm, var *v)
1831 a = 0; 1833 a = 0;
1832 } 1834 }
1833 1835
1834 qrealloc(&m, a+p+128, &size); 1836 m = qrealloc(m, a+p+128, &size);
1835 b = m + a; 1837 b = m + a;
1836 pp = p; 1838 pp = p;
1837 p += safe_read(fd, b+p, size-p-1); 1839 p += safe_read(fd, b+p, size-p-1);
@@ -1910,7 +1912,7 @@ static char *awk_printf(node *n)
1910 } 1912 }
1911 1913
1912 incr = (f - s) + MAXVARFMT; 1914 incr = (f - s) + MAXVARFMT;
1913 qrealloc(&b, incr + i, &bsize); 1915 b = qrealloc(b, incr + i, &bsize);
1914 c = *f; 1916 c = *f;
1915 if (c != '\0') f++; 1917 if (c != '\0') f++;
1916 c1 = *f; 1918 c1 = *f;
@@ -1923,7 +1925,7 @@ static char *awk_printf(node *n)
1923 (char)getvar_i(arg) : *getvar_s(arg)); 1925 (char)getvar_i(arg) : *getvar_s(arg));
1924 } else if (c == 's') { 1926 } else if (c == 's') {
1925 s1 = getvar_s(arg); 1927 s1 = getvar_s(arg);
1926 qrealloc(&b, incr+i+strlen(s1), &bsize); 1928 b = qrealloc(b, incr+i+strlen(s1), &bsize);
1927 i += sprintf(b+i, s, s1); 1929 i += sprintf(b+i, s, s1);
1928 } else { 1930 } else {
1929 i += fmt_num(b+i, incr, s, getvar_i(arg), FALSE); 1931 i += fmt_num(b+i, incr, s, getvar_i(arg), FALSE);
@@ -1967,7 +1969,7 @@ static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int
1967 so = pmatch[0].rm_so; 1969 so = pmatch[0].rm_so;
1968 eo = pmatch[0].rm_eo; 1970 eo = pmatch[0].rm_eo;
1969 1971
1970 qrealloc(&ds, di + eo + rl, &dssize); 1972 ds = qrealloc(ds, di + eo + rl, &dssize);
1971 memcpy(ds + di, sp, eo); 1973 memcpy(ds + di, sp, eo);
1972 di += eo; 1974 di += eo;
1973 if (++i >= nm) { 1975 if (++i >= nm) {
@@ -1991,7 +1993,7 @@ static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int
1991 ds[di++] = c; 1993 ds[di++] = c;
1992 } else { 1994 } else {
1993 n = pmatch[j].rm_eo - pmatch[j].rm_so; 1995 n = pmatch[j].rm_eo - pmatch[j].rm_so;
1994 qrealloc(&ds, di + rl + n, &dssize); 1996 ds = qrealloc(ds, di + rl + n, &dssize);
1995 memcpy(ds + di, sp + pmatch[j].rm_so, n); 1997 memcpy(ds + di, sp + pmatch[j].rm_so, n);
1996 di += n; 1998 di += n;
1997 } 1999 }
@@ -2010,7 +2012,7 @@ static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int
2010 } 2012 }
2011 } 2013 }
2012 2014
2013 qrealloc(&ds, di + strlen(sp), &dssize); 2015 ds = qrealloc(ds, di + strlen(sp), &dssize);
2014 strcpy(ds + di, sp); 2016 strcpy(ds + di, sp);
2015 setvar_p(dest, ds); 2017 setvar_p(dest, ds);
2016 if (re == &sreg) 2018 if (re == &sreg)