diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-05-31 11:41:50 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-05-31 11:41:50 +0000 |
commit | 1d1bba4e99f56874f1f0da1a3e0cc39f62f0e709 (patch) | |
tree | 44044a5dfe232a3e2a8fa18935fe556d1dcf901f /coreutils/printf.c | |
parent | d12fcc20dac8415a89898fdaa9bfabaac39e5e33 (diff) | |
download | busybox-w32-1d1bba4e99f56874f1f0da1a3e0cc39f62f0e709.tar.gz busybox-w32-1d1bba4e99f56874f1f0da1a3e0cc39f62f0e709.tar.bz2 busybox-w32-1d1bba4e99f56874f1f0da1a3e0cc39f62f0e709.zip |
printf: code shrink by eliminating string alloc/copy
function old new delta
print_direc 428 382 -46
Diffstat (limited to 'coreutils/printf.c')
-rw-r--r-- | coreutils/printf.c | 55 |
1 files changed, 27 insertions, 28 deletions
diff --git a/coreutils/printf.c b/coreutils/printf.c index 7c7613152..cd184952e 100644 --- a/coreutils/printf.c +++ b/coreutils/printf.c | |||
@@ -107,28 +107,28 @@ static void print_esc_string(char *str) | |||
107 | } | 107 | } |
108 | } | 108 | } |
109 | 109 | ||
110 | static void print_direc(char *start, size_t length, int field_width, int precision, | 110 | static void print_direc(char *format, unsigned fmt_length, |
111 | int field_width, int precision, | ||
111 | const char *argument) | 112 | const char *argument) |
112 | { | 113 | { |
113 | char *p; /* Null-terminated copy of % directive. */ | 114 | char saved; |
114 | 115 | ||
115 | p = xmalloc((unsigned) (length + 1)); | 116 | saved = format[fmt_length]; |
116 | strncpy(p, start, length); | 117 | format[fmt_length] = '\0'; |
117 | p[length] = 0; | ||
118 | 118 | ||
119 | switch (p[length - 1]) { | 119 | switch (p[fmt_length - 1]) { |
120 | case 'd': | 120 | case 'd': |
121 | case 'i': | 121 | case 'i': |
122 | if (field_width < 0) { | 122 | if (field_width < 0) { |
123 | if (precision < 0) | 123 | if (precision < 0) |
124 | printf(p, my_xstrtol(argument)); | 124 | printf(format, my_xstrtol(argument)); |
125 | else | 125 | else |
126 | printf(p, precision, my_xstrtol(argument)); | 126 | printf(format, precision, my_xstrtol(argument)); |
127 | } else { | 127 | } else { |
128 | if (precision < 0) | 128 | if (precision < 0) |
129 | printf(p, field_width, my_xstrtol(argument)); | 129 | printf(format, field_width, my_xstrtol(argument)); |
130 | else | 130 | else |
131 | printf(p, field_width, precision, my_xstrtol(argument)); | 131 | printf(format, field_width, precision, my_xstrtol(argument)); |
132 | } | 132 | } |
133 | break; | 133 | break; |
134 | case 'o': | 134 | case 'o': |
@@ -137,14 +137,14 @@ static void print_direc(char *start, size_t length, int field_width, int precisi | |||
137 | case 'X': | 137 | case 'X': |
138 | if (field_width < 0) { | 138 | if (field_width < 0) { |
139 | if (precision < 0) | 139 | if (precision < 0) |
140 | printf(p, my_xstrtoul(argument)); | 140 | printf(format, my_xstrtoul(argument)); |
141 | else | 141 | else |
142 | printf(p, precision, my_xstrtoul(argument)); | 142 | printf(format, precision, my_xstrtoul(argument)); |
143 | } else { | 143 | } else { |
144 | if (precision < 0) | 144 | if (precision < 0) |
145 | printf(p, field_width, my_xstrtoul(argument)); | 145 | printf(format, field_width, my_xstrtoul(argument)); |
146 | else | 146 | else |
147 | printf(p, field_width, precision, my_xstrtoul(argument)); | 147 | printf(format, field_width, precision, my_xstrtoul(argument)); |
148 | } | 148 | } |
149 | break; | 149 | break; |
150 | case 'f': | 150 | case 'f': |
@@ -154,48 +154,47 @@ static void print_direc(char *start, size_t length, int field_width, int precisi | |||
154 | case 'G': | 154 | case 'G': |
155 | if (field_width < 0) { | 155 | if (field_width < 0) { |
156 | if (precision < 0) | 156 | if (precision < 0) |
157 | printf(p, my_xstrtod(argument)); | 157 | printf(format, my_xstrtod(argument)); |
158 | else | 158 | else |
159 | printf(p, precision, my_xstrtod(argument)); | 159 | printf(format, precision, my_xstrtod(argument)); |
160 | } else { | 160 | } else { |
161 | if (precision < 0) | 161 | if (precision < 0) |
162 | printf(p, field_width, my_xstrtod(argument)); | 162 | printf(format, field_width, my_xstrtod(argument)); |
163 | else | 163 | else |
164 | printf(p, field_width, precision, my_xstrtod(argument)); | 164 | printf(format, field_width, precision, my_xstrtod(argument)); |
165 | } | 165 | } |
166 | break; | 166 | break; |
167 | case 'c': | 167 | case 'c': |
168 | printf(p, *argument); | 168 | printf(format, *argument); |
169 | break; | 169 | break; |
170 | case 's': | 170 | case 's': |
171 | if (field_width < 0) { | 171 | if (field_width < 0) { |
172 | if (precision < 0) | 172 | if (precision < 0) |
173 | printf(p, argument); | 173 | printf(format, argument); |
174 | else | 174 | else |
175 | printf(p, precision, argument); | 175 | printf(format, precision, argument); |
176 | } else { | 176 | } else { |
177 | if (precision < 0) | 177 | if (precision < 0) |
178 | printf(p, field_width, argument); | 178 | printf(format, field_width, argument); |
179 | else | 179 | else |
180 | printf(p, field_width, precision, argument); | 180 | printf(format, field_width, precision, argument); |
181 | } | 181 | } |
182 | break; | 182 | break; |
183 | } | 183 | } |
184 | 184 | ||
185 | free(p); | 185 | format[fmt_length] = saved; |
186 | } | 186 | } |
187 | 187 | ||
188 | /* Print the text in FORMAT, using ARGV for arguments to any '%' directives. | 188 | /* Print the text in FORMAT, using ARGV for arguments to any '%' directives. |
189 | Return advanced ARGV. */ | 189 | Return advanced ARGV. */ |
190 | static char **print_formatted(char *format, char **argv) | 190 | static char **print_formatted(char *f, char **argv) |
191 | { | 191 | { |
192 | char *f; /* Pointer into 'format'. */ | ||
193 | char *direc_start; /* Start of % directive. */ | 192 | char *direc_start; /* Start of % directive. */ |
194 | size_t direc_length; /* Length of % directive. */ | 193 | unsigned direc_length; /* Length of % directive. */ |
195 | int field_width; /* Arg to first '*', or -1 if none. */ | 194 | int field_width; /* Arg to first '*', or -1 if none. */ |
196 | int precision; /* Arg to second '*', or -1 if none. */ | 195 | int precision; /* Arg to second '*', or -1 if none. */ |
197 | 196 | ||
198 | for (f = format; *f; ++f) { | 197 | for (; *f; ++f) { |
199 | switch (*f) { | 198 | switch (*f) { |
200 | case '%': | 199 | case '%': |
201 | direc_start = f++; | 200 | direc_start = f++; |