1
From 266bec8ba76898c5602e54fb3460c4af42f38af0 Mon Sep 17 00:00:00 2001
2
From: Denys Vlasenko <vda.linux@googlemail.com>
3
Date: Wed, 2 Jan 2019 05:03:53 +0100
4
Subject: [PATCH 11/84] bc: speed up string printing, fix print ""
8
zxc_program_print 681 683 +2
9
------------------------------------------------------------------------------
10
(add/remove: 1/0 grow/shrink: 1/0 up/down: 11/0) Total: 11 bytes
11
text data bss dec hex filename
12
979144 485 7296 986925 f0f2d busybox_old
13
979062 485 7296 986843 f0edb busybox_unstripped
15
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
17
miscutils/bc.c | 66 ++++++++++++++++++------------------------------------
18
testsuite/bc.tests | 6 +++++
19
2 files changed, 28 insertions(+), 44 deletions(-)
21
diff --git a/miscutils/bc.c b/miscutils/bc.c
22
index 1b9cdce5e..bb91216c2 100644
25
@@ -5359,7 +5359,7 @@ static char *xc_program_name(char *code, size_t *bgn)
26
static void xc_program_printString(const char *str)
30
+ if (!str[0] && IS_DC) {
31
// Example: echo '[]ap' | dc
32
// should print two bytes: 0x00, 0x0A
34
@@ -5367,46 +5367,25 @@ static void xc_program_printString(const char *str)
39
- if (c != '\\' || !*str)
44
+ static const char esc[] ALIGN1 = "nabfrt""e\\";
64
- G.prog.nchars = SIZE_MAX;
76
- // Just print the backslash and following character.
77
+ n = strchr(esc, c); // note: c can be NUL
79
+ // Just print the backslash and following character
85
+ if (n - esc == 0) // "\n" ?
86
+ G.prog.nchars = SIZE_MAX;
87
+ c = "\n\a\b\f\r\t""\\\\""\\"[n - esc];
88
+ // n a b f r t e \ \<end of line>
95
@@ -5631,16 +5610,15 @@ static BC_STATUS zxc_program_print(char inst, size_t idx)
96
str = *xc_program_str(idx);
98
if (inst == XC_INST_PRINT_STR) {
101
- if (c == '\0') break;
104
- if (c == '\n') G.prog.nchars = 0;
107
+ G.prog.nchars += printf("%s", str);
108
+ nl = strrchr(str, '\n');
110
+ G.prog.nchars = strlen(nl + 1);
112
xc_program_printString(str);
113
- if (inst == XC_INST_PRINT) bb_putchar('\n');
114
+ if (inst == XC_INST_PRINT)
119
diff --git a/testsuite/bc.tests b/testsuite/bc.tests
120
index 13525ea52..fbcfff2e4 100755
121
--- a/testsuite/bc.tests
122
+++ b/testsuite/bc.tests
123
@@ -149,6 +149,12 @@ testing "bc (!a&&b)" \
127
+# check that dc code is not messing this up (no NUL printing!)
128
+testing "bc print \"\"" \
133
testing "bc print 1,2,3" \