Skip to content

Commit bb1eba6

Browse files
committed
o Adding support for star modifier.
This allows %*s where the leading padding is taken from an extra argument so that this: char buf[] = "0123456789"; printf("'%*s'", 5, &buf[5]); yields '56789'
1 parent c872fc5 commit bb1eba6

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

test/all.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ void check(const char *expr, int expected_sz, int actual_sz)
2121
printf("Output %s", actual_buffer);
2222
} else {
2323
failures++;
24-
printf("FAIL %s\n", expr);
25-
26-
if (!size_pass) {
27-
printf("Size fail: expected=%d, actual=%d\n", expected_sz, actual_sz);
28-
}
24+
printf("FAIL printf(%s)\n", expr);
2925

3026
if (!content_pass) {
3127
printf("libc %s\n", expected_buffer);
@@ -85,7 +81,12 @@ void main()
8581

8682
{
8783
char buf[] = "0123456789";
88-
TPRINTF("%*s", 5, &buf[5]);
84+
TPRINTF("%*s", 5, &buf[5]); /* minimum length, too long string */
85+
TPRINTF("%.*s", 5, buf); /* maximum length, too long string */
86+
TPRINTF("%.*s", 5, &buf[8]); /* maximum length, too short string */
87+
TPRINTF("%*.*s", 5, 5, &buf[8]); /* minimum and maximum, too short string*/
88+
TPRINTF("%*.*s", 5, 1, &buf[8]);
89+
TPRINTF("%*.*s", 5, 5, buf);
8990

9091
TPRINTF("%-*d", 5, 123);
9192
TPRINTF("%*d", 5, 123);

tinyprintf.c

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
4242
#include <sys/types.h>
4343
#endif
4444

45+
#include <limits.h>
46+
4547
#ifdef PRINTF_LONG_LONG_SUPPORT
4648
# define PRINTF_LONG_SUPPORT
4749
#endif
@@ -70,15 +72,16 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7072
* Implementation
7173
*/
7274
struct param {
73-
char l:1; /**< Add leading character */
74-
char alt:1; /**< alternate form */
75-
char uc:1; /**< Upper case (for base16 only) */
76-
char align_left:1; /**< 0 == align right (default), 1 == align left */
77-
char lchr; /**< Leading character */
78-
unsigned int width; /**< field width */
79-
char sign; /**< The sign to display (if any) */
80-
unsigned int base; /**< number base (e.g.: 8, 10, 16) */
81-
char *bf; /**< Buffer to output */
75+
char l:1; /**< Add leading character */
76+
char alt:1; /**< alternate form */
77+
char uc:1; /**< Upper case (for base16 only) */
78+
char align_left:1; /**< 0 == align right (default), 1 == align left */
79+
char lchr; /**< Leading character */
80+
unsigned int width; /**< field width */
81+
int precision; /**< field precision */
82+
char sign; /**< The sign to display (if any) */
83+
unsigned int base; /**< number base (e.g.: 8, 10, 16) */
84+
char *bf; /**< Buffer to output */
8285
};
8386

8487

@@ -199,15 +202,15 @@ static char a2u(char ch, const char **src, int base, unsigned int *nump)
199202
return ch;
200203
}
201204

202-
static void putchw(void *putp, putcf putf, struct param *p)
205+
static void putchw(void *putp, putcf putf, const struct param *p)
203206
{
204207
char ch;
205208
int n = p->width;
206209
char *bf = p->bf;
210+
int precision = p->precision;
207211

208212
/* Number of filling characters */
209-
while (*bf++ && n > 0)
210-
n--;
213+
while (precision-- > 0 && *bf++ && n-- > 0);
211214
if (p->sign)
212215
n--;
213216
if (p->alt && p->base == 16)
@@ -241,7 +244,8 @@ static void putchw(void *putp, putcf putf, struct param *p)
241244

242245
/* Put actual buffer */
243246
bf = p->bf;
244-
while ((ch = *bf++))
247+
precision = p->precision;
248+
while (precision-- > 0 && (ch = *bf++))
245249
putf(putp, ch);
246250

247251
/* Fill with space to align to the left, after string */
@@ -260,6 +264,7 @@ void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
260264
char bf[12]; /* int = 32b on some architectures */
261265
#endif
262266
char ch;
267+
int w;
263268
p.bf = bf;
264269

265270
while ((ch = *(fmt++))) {
@@ -274,6 +279,7 @@ void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
274279
p.lchr = ' ';
275280
p.alt = 0;
276281
p.width = 0;
282+
p.precision = INT_MAX;
277283
p.align_left = 0;
278284
p.sign = 0;
279285

@@ -292,7 +298,13 @@ void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
292298
p.alt = 1;
293299
continue;
294300
case '*':
295-
p.width = (unsigned int) va_arg(va, int);
301+
w = va_arg(va, int);
302+
if (w < 0) {
303+
p.align_left = 1;
304+
p.width = (unsigned int) -w;
305+
} else {
306+
p.width = (unsigned int) w;
307+
}
296308
continue;
297309
default:
298310
break;
@@ -319,6 +331,12 @@ void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
319331
if (num > 1) {
320332
p.lchr = '0';
321333
}
334+
} else if (ch == '*') {
335+
ch = *(fmt++);
336+
int precision = va_arg(va, int);
337+
if (precision >= 0) {
338+
p.precision = precision;
339+
}
322340
}
323341
}
324342

0 commit comments

Comments
 (0)