@@ -37,77 +37,72 @@ typedef uint32_t uint_value_type;
37
37
BUILD_ASSERT (sizeof (uint_value_type ) <= 8U ,
38
38
"DIGITS_BUFLEN formula may be incorrect" );
39
39
40
+ #define OUTC (_c ) do { \
41
+ out((int)(_c), ctx); \
42
+ if (IS_ENABLED(CONFIG_CBPRINTF_LIBC_SUBSTS)) { \
43
+ ++count; \
44
+ } \
45
+ } while (false)
46
+
40
47
static void print_digits (cbprintf_cb out , void * ctx , uint_value_type num ,
41
48
unsigned int base , bool pad_before , char pad_char ,
42
49
int min_width , size_t * countp )
43
50
{
44
51
size_t count = 0 ;
45
52
char buf [DIGITS_BUFLEN ];
46
- unsigned int i ;
53
+ int i = 0 ;
47
54
48
- /* Print it backwards into the end of the buffer, low digits first */
49
- for (i = DIGITS_BUFLEN - 1U ; num != 0U ; i -- ) {
50
- buf [i ] = "0123456789abcdef" [num % base ];
55
+ /* Print it backwards, low digits first */
56
+ do {
57
+ char c = num % base ;
58
+ if (c >= 10 ) {
59
+ c += 'a' - '0' - 10 ;
60
+ }
61
+ buf [i ++ ] = c + '0' ;
51
62
num /= base ;
52
- }
53
-
54
- if (i == DIGITS_BUFLEN - 1U ) {
55
- buf [i ] = '0' ;
56
- } else {
57
- i ++ ;
58
- }
63
+ } while (num );
59
64
60
- int pad = MAX (min_width - ( int )( DIGITS_BUFLEN - i ) , 0 );
65
+ int pad = MAX (min_width - i , 0 );
61
66
62
67
for (/**/ ; pad > 0 && pad_before ; pad -- ) {
63
- out (pad_char , ctx );
64
- if (IS_ENABLED (CONFIG_CBPRINTF_LIBC_SUBSTS )) {
65
- ++ count ;
66
- }
67
- }
68
- for (/**/ ; i < DIGITS_BUFLEN ; i ++ ) {
69
- out (buf [i ], ctx );
70
- if (IS_ENABLED (CONFIG_CBPRINTF_LIBC_SUBSTS )) {
71
- ++ count ;
72
- }
68
+ OUTC (pad_char );
73
69
}
70
+ do {
71
+ OUTC (buf [-- i ]);
72
+ } while (i > 0 );
74
73
for (/**/ ; pad > 0 ; pad -- ) {
75
- out (pad_char , ctx );
76
- if (IS_ENABLED (CONFIG_CBPRINTF_LIBC_SUBSTS )) {
77
- ++ count ;
78
- }
74
+ OUTC (pad_char );
79
75
}
80
76
81
- if (IS_ENABLED (CONFIG_CBPRINTF_LIBC_SUBSTS )) {
82
- * countp += count ;
83
- }
77
+ * countp += count ;
84
78
}
85
79
86
80
static void print_hex (cbprintf_cb out , void * ctx , uint_value_type num ,
87
- enum pad_type padding , int min_width , size_t * count )
81
+ enum pad_type padding , int min_width , size_t * countp )
88
82
{
89
83
print_digits (out , ctx , num , 16U , padding != PAD_SPACE_AFTER ,
90
84
padding == PAD_ZERO_BEFORE ? '0' : ' ' , min_width ,
91
- count );
85
+ countp );
92
86
}
93
87
94
88
static void print_dec (cbprintf_cb out , void * ctx , uint_value_type num ,
95
- enum pad_type padding , int min_width , size_t * count )
89
+ enum pad_type padding , int min_width , size_t * countp )
96
90
{
97
91
print_digits (out , ctx , num , 10U , padding != PAD_SPACE_AFTER ,
98
92
padding == PAD_ZERO_BEFORE ? '0' : ' ' , min_width ,
99
- count );
93
+ countp );
100
94
}
101
95
102
- static bool ok64 (cbprintf_cb out , void * ctx , long long val , size_t * count )
96
+ static bool ok64 (cbprintf_cb out , void * ctx , long long val , size_t * countp )
103
97
{
98
+ size_t count = 0 ;
99
+
104
100
if (sizeof (int_value_type ) < 8U && val != (int_value_type ) val ) {
105
- out ('E' , ctx );
106
- out ('R' , ctx );
107
- out ('R' , ctx );
108
- if (IS_ENABLED (CONFIG_CBPRINTF_LIBC_SUBSTS )) {
109
- * count += 3 ;
110
- }
101
+ const char * cp = "ERR" ;
102
+ do {
103
+ OUTC (* cp ++ );
104
+ } while (* cp );
105
+ * countp += count ;
111
106
return false;
112
107
}
113
108
return true;
@@ -134,16 +129,9 @@ int cbvprintf(cbprintf_cb out, void *ctx, const char *fmt, va_list ap)
134
129
size_t count = 0 ;
135
130
int might_format = 0 ; /* 1 if encountered a '%' */
136
131
enum pad_type padding = PAD_NONE ;
137
- int min_width = -1 ;
132
+ int padlen , min_width = -1 ;
138
133
char length_mod = 0 ;
139
134
140
- #define OUTC (_c ) do { \
141
- out((int)(_c), ctx); \
142
- if (IS_ENABLED(CONFIG_CBPRINTF_LIBC_SUBSTS)) { \
143
- ++count; \
144
- } \
145
- } while (false)
146
-
147
135
/* fmt has already been adjusted if needed */
148
136
while (* fmt ) {
149
137
if (!might_format ) {
@@ -176,10 +164,9 @@ int cbvprintf(cbprintf_cb out, void *ctx, const char *fmt, va_list ap)
176
164
case '8' :
177
165
case '9' :
178
166
if (min_width < 0 ) {
179
- min_width = * fmt - '0' ;
180
- } else {
181
- min_width = 10 * min_width + * fmt - '0' ;
167
+ min_width = 0 ;
182
168
}
169
+ min_width = 10 * min_width + * fmt - '0' ;
183
170
184
171
if (padding == PAD_NONE ) {
185
172
padding = PAD_SPACE_BEFORE ;
@@ -247,12 +234,14 @@ int cbvprintf(cbprintf_cb out, void *ctx, const char *fmt, va_list ap)
247
234
cp = "0x" ;
248
235
}
249
236
250
- while ( * cp ) {
237
+ do {
251
238
OUTC (* cp ++ );
252
- }
239
+ } while ( * cp );
253
240
if (x == 0 ) {
254
- break ;
241
+ padlen = min_width - 5 ;
242
+ goto pad_string ;
255
243
}
244
+ min_width -= 2 ;
256
245
} else if (length_mod == 'l' ) {
257
246
x = va_arg (ap , unsigned long );
258
247
} else if (length_mod == 'L' ) {
@@ -272,11 +261,11 @@ int cbvprintf(cbprintf_cb out, void *ctx, const char *fmt, va_list ap)
272
261
while (* s ) {
273
262
OUTC (* s ++ );
274
263
}
264
+ padlen = min_width - (s - start );
275
265
266
+ pad_string :
276
267
if (padding == PAD_SPACE_AFTER ) {
277
- int remaining = min_width - (s - start );
278
-
279
- while (remaining -- > 0 ) {
268
+ while (padlen -- > 0 ) {
280
269
OUTC (' ' );
281
270
}
282
271
}
0 commit comments