diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2019-01-02 05:03:53 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2019-02-14 14:40:57 +0100 |
commit | 7dd6ba12f76d2affafac8e6173d0c995b3279e51 (patch) | |
tree | d13be59fcc4cd7fa3a91ecc6e7872219831b1b87 | |
parent | f23c04af2d4575836cb5d9d1ea9176310ed633b5 (diff) | |
download | busybox-w32-7dd6ba12f76d2affafac8e6173d0c995b3279e51.tar.gz busybox-w32-7dd6ba12f76d2affafac8e6173d0c995b3279e51.tar.bz2 busybox-w32-7dd6ba12f76d2affafac8e6173d0c995b3279e51.zip |
bc: speed up string printing, fix print ""
function old new delta
static.esc - 9 +9
zxc_program_print 681 683 +2
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/0 up/down: 11/0) Total: 11 bytes
text data bss dec hex filename
979144 485 7296 986925 f0f2d busybox_old
979062 485 7296 986843 f0edb busybox_unstripped
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/bc.c | 66 | ||||
-rwxr-xr-x | testsuite/bc.tests | 6 |
2 files changed, 28 insertions, 44 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 1b9cdce5e..bb91216c2 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
@@ -5359,7 +5359,7 @@ static char *xc_program_name(char *code, size_t *bgn) | |||
5359 | static void xc_program_printString(const char *str) | 5359 | static void xc_program_printString(const char *str) |
5360 | { | 5360 | { |
5361 | #if ENABLE_DC | 5361 | #if ENABLE_DC |
5362 | if (!str[0]) { | 5362 | if (!str[0] && IS_DC) { |
5363 | // Example: echo '[]ap' | dc | 5363 | // Example: echo '[]ap' | dc |
5364 | // should print two bytes: 0x00, 0x0A | 5364 | // should print two bytes: 0x00, 0x0A |
5365 | bb_putchar('\0'); | 5365 | bb_putchar('\0'); |
@@ -5367,46 +5367,25 @@ static void xc_program_printString(const char *str) | |||
5367 | } | 5367 | } |
5368 | #endif | 5368 | #endif |
5369 | while (*str) { | 5369 | while (*str) { |
5370 | int c = *str++; | 5370 | char c = *str++; |
5371 | if (c != '\\' || !*str) | 5371 | if (c == '\\') { |
5372 | bb_putchar(c); | 5372 | static const char esc[] ALIGN1 = "nabfrt""e\\"; |
5373 | else { | 5373 | char *n; |
5374 | |||
5374 | c = *str++; | 5375 | c = *str++; |
5375 | switch (c) { | 5376 | n = strchr(esc, c); // note: c can be NUL |
5376 | case 'a': | 5377 | if (!n) { |
5377 | bb_putchar('\a'); | 5378 | // Just print the backslash and following character |
5378 | break; | ||
5379 | case 'b': | ||
5380 | bb_putchar('\b'); | ||
5381 | break; | ||
5382 | case '\\': | ||
5383 | case 'e': | ||
5384 | bb_putchar('\\'); | ||
5385 | break; | ||
5386 | case 'f': | ||
5387 | bb_putchar('\f'); | ||
5388 | break; | ||
5389 | case 'n': | ||
5390 | bb_putchar('\n'); | ||
5391 | G.prog.nchars = SIZE_MAX; | ||
5392 | break; | ||
5393 | case 'r': | ||
5394 | bb_putchar('\r'); | ||
5395 | break; | ||
5396 | case 'q': | ||
5397 | bb_putchar('"'); | ||
5398 | break; | ||
5399 | case 't': | ||
5400 | bb_putchar('\t'); | ||
5401 | break; | ||
5402 | default: | ||
5403 | // Just print the backslash and following character. | ||
5404 | bb_putchar('\\'); | 5379 | bb_putchar('\\'); |
5405 | ++G.prog.nchars; | 5380 | ++G.prog.nchars; |
5406 | bb_putchar(c); | 5381 | } else { |
5407 | break; | 5382 | if (n - esc == 0) // "\n" ? |
5383 | G.prog.nchars = SIZE_MAX; | ||
5384 | c = "\n\a\b\f\r\t""\\\\""\\"[n - esc]; | ||
5385 | // n a b f r t e \ \<end of line> | ||
5408 | } | 5386 | } |
5409 | } | 5387 | } |
5388 | putchar(c); | ||
5410 | ++G.prog.nchars; | 5389 | ++G.prog.nchars; |
5411 | } | 5390 | } |
5412 | } | 5391 | } |
@@ -5631,16 +5610,15 @@ static BC_STATUS zxc_program_print(char inst, size_t idx) | |||
5631 | str = *xc_program_str(idx); | 5610 | str = *xc_program_str(idx); |
5632 | 5611 | ||
5633 | if (inst == XC_INST_PRINT_STR) { | 5612 | if (inst == XC_INST_PRINT_STR) { |
5634 | for (;;) { | 5613 | char *nl; |
5635 | char c = *str++; | 5614 | G.prog.nchars += printf("%s", str); |
5636 | if (c == '\0') break; | 5615 | nl = strrchr(str, '\n'); |
5637 | bb_putchar(c); | 5616 | if (nl) |
5638 | ++G.prog.nchars; | 5617 | G.prog.nchars = strlen(nl + 1); |
5639 | if (c == '\n') G.prog.nchars = 0; | ||
5640 | } | ||
5641 | } else { | 5618 | } else { |
5642 | xc_program_printString(str); | 5619 | xc_program_printString(str); |
5643 | if (inst == XC_INST_PRINT) bb_putchar('\n'); | 5620 | if (inst == XC_INST_PRINT) |
5621 | bb_putchar('\n'); | ||
5644 | } | 5622 | } |
5645 | } | 5623 | } |
5646 | 5624 | ||
diff --git a/testsuite/bc.tests b/testsuite/bc.tests index 13525ea52..fbcfff2e4 100755 --- a/testsuite/bc.tests +++ b/testsuite/bc.tests | |||
@@ -149,6 +149,12 @@ testing "bc (!a&&b)" \ | |||
149 | "0\n" \ | 149 | "0\n" \ |
150 | "" "(!a&&b)" | 150 | "" "(!a&&b)" |
151 | 151 | ||
152 | # check that dc code is not messing this up (no NUL printing!) | ||
153 | testing "bc print \"\"" \ | ||
154 | "bc" \ | ||
155 | "" \ | ||
156 | "" "print \"\"" | ||
157 | |||
152 | testing "bc print 1,2,3" \ | 158 | testing "bc print 1,2,3" \ |
153 | "bc" \ | 159 | "bc" \ |
154 | "123" \ | 160 | "123" \ |