~mmach/netext73/busybox

« back to all changes in this revision

Viewing changes to 0011-bc-speed-up-string-printing-fix-print.patch

  • Committer: mmach
  • Date: 2022-08-22 15:28:31 UTC
  • Revision ID: netbit73@gmail.com-20220822152831-vrsxgw6c75b03ujx
1.35.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 ""
5
 
 
6
 
function                                             old     new   delta
7
 
static.esc                                             -       9      +9
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
14
 
 
15
 
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
16
 
---
17
 
 miscutils/bc.c     | 66 ++++++++++++++++++------------------------------------
18
 
 testsuite/bc.tests |  6 +++++
19
 
 2 files changed, 28 insertions(+), 44 deletions(-)
20
 
 
21
 
diff --git a/miscutils/bc.c b/miscutils/bc.c
22
 
index 1b9cdce5e..bb91216c2 100644
23
 
--- a/miscutils/bc.c
24
 
+++ b/miscutils/bc.c
25
 
@@ -5359,7 +5359,7 @@ static char *xc_program_name(char *code, size_t *bgn)
26
 
 static void xc_program_printString(const char *str)
27
 
 {
28
 
 #if ENABLE_DC
29
 
-       if (!str[0]) {
30
 
+       if (!str[0] && IS_DC) {
31
 
                // Example: echo '[]ap' | dc
32
 
                // should print two bytes: 0x00, 0x0A
33
 
                bb_putchar('\0');
34
 
@@ -5367,46 +5367,25 @@ static void xc_program_printString(const char *str)
35
 
        }
36
 
 #endif
37
 
        while (*str) {
38
 
-               int c = *str++;
39
 
-               if (c != '\\' || !*str)
40
 
-                       bb_putchar(c);
41
 
-               else {
42
 
+               char c = *str++;
43
 
+               if (c == '\\') {
44
 
+                       static const char esc[] ALIGN1 = "nabfrt""e\\";
45
 
+                       char *n;
46
 
+
47
 
                        c = *str++;
48
 
-                       switch (c) {
49
 
-                       case 'a':
50
 
-                               bb_putchar('\a');
51
 
-                               break;
52
 
-                       case 'b':
53
 
-                               bb_putchar('\b');
54
 
-                               break;
55
 
-                       case '\\':
56
 
-                       case 'e':
57
 
-                               bb_putchar('\\');
58
 
-                               break;
59
 
-                       case 'f':
60
 
-                               bb_putchar('\f');
61
 
-                               break;
62
 
-                       case 'n':
63
 
-                               bb_putchar('\n');
64
 
-                               G.prog.nchars = SIZE_MAX;
65
 
-                               break;
66
 
-                       case 'r':
67
 
-                               bb_putchar('\r');
68
 
-                               break;
69
 
-                       case 'q':
70
 
-                               bb_putchar('"');
71
 
-                               break;
72
 
-                       case 't':
73
 
-                               bb_putchar('\t');
74
 
-                               break;
75
 
-                       default:
76
 
-                               // Just print the backslash and following character.
77
 
+                       n = strchr(esc, c); // note: c can be NUL
78
 
+                       if (!n) {
79
 
+                               // Just print the backslash and following character
80
 
                                bb_putchar('\\');
81
 
                                ++G.prog.nchars;
82
 
-                               bb_putchar(c);
83
 
-                               break;
84
 
+                       } else {
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>
89
 
                        }
90
 
                }
91
 
+               putchar(c);
92
 
                ++G.prog.nchars;
93
 
        }
94
 
 }
95
 
@@ -5631,16 +5610,15 @@ static BC_STATUS zxc_program_print(char inst, size_t idx)
96
 
                str = *xc_program_str(idx);
97
 
 
98
 
                if (inst == XC_INST_PRINT_STR) {
99
 
-                       for (;;) {
100
 
-                               char c = *str++;
101
 
-                               if (c == '\0') break;
102
 
-                               bb_putchar(c);
103
 
-                               ++G.prog.nchars;
104
 
-                               if (c == '\n') G.prog.nchars = 0;
105
 
-                       }
106
 
+                       char *nl;
107
 
+                       G.prog.nchars += printf("%s", str);
108
 
+                       nl = strrchr(str, '\n');
109
 
+                       if (nl)
110
 
+                               G.prog.nchars = strlen(nl + 1);
111
 
                } else {
112
 
                        xc_program_printString(str);
113
 
-                       if (inst == XC_INST_PRINT) bb_putchar('\n');
114
 
+                       if (inst == XC_INST_PRINT)
115
 
+                               bb_putchar('\n');
116
 
                }
117
 
        }
118
 
 
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)" \
124
 
        "0\n" \
125
 
        "" "(!a&&b)"
126
 
 
127
 
+# check that dc code is not messing this up (no NUL printing!)
128
 
+testing "bc print \"\"" \
129
 
+       "bc" \
130
 
+       "" \
131
 
+       "" "print \"\""
132
 
+
133
 
 testing "bc print 1,2,3" \
134
 
        "bc" \
135
 
        "123" \
136
 
2.16.2
137