Skip to content

Commit 7349b4f

Browse files
Honor field-width / precision for %c and %s (#308)
1 parent 759c5cd commit 7349b4f

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

nanoprintf.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,18 @@ static int npf_parse_format_spec(char const *format, npf_format_spec_t *out_spec
393393
case '%': out_spec->conv_spec = NPF_FMT_SPEC_CONV_PERCENT;
394394
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
395395
out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE;
396+
out_spec->prec = 0;
396397
#endif
397398
break;
398399

399400
case 'c': out_spec->conv_spec = NPF_FMT_SPEC_CONV_CHAR;
400401
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
401402
out_spec->prec_opt = NPF_FMT_SPEC_OPT_NONE;
403+
out_spec->prec = 0;
402404
#endif
403405
break;
404406

405407
case 's': out_spec->conv_spec = NPF_FMT_SPEC_CONV_STRING;
406-
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
407-
out_spec->leading_zero_pad = 0;
408-
#endif
409408
break;
410409

411410
case 'i':
@@ -990,17 +989,13 @@ int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
990989
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
991990
// Compute the field width pad character
992991
if (fs.field_width_opt != NPF_FMT_SPEC_OPT_NONE) {
993-
if (fs.leading_zero_pad) { // '0' flag is only legal with numeric types
994-
if ((fs.conv_spec != NPF_FMT_SPEC_CONV_STRING) &&
995-
(fs.conv_spec != NPF_FMT_SPEC_CONV_CHAR) &&
996-
(fs.conv_spec != NPF_FMT_SPEC_CONV_PERCENT)) {
992+
if (fs.leading_zero_pad) {
997993
#if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1
998-
if ((fs.prec_opt != NPF_FMT_SPEC_OPT_NONE) && !fs.prec && zero) {
999-
pad_c = ' ';
1000-
} else
994+
if ((fs.prec_opt != NPF_FMT_SPEC_OPT_NONE) && !fs.prec && zero) {
995+
pad_c = ' ';
996+
} else
1001997
#endif
1002-
{ pad_c = '0'; }
1003-
}
998+
{ pad_c = '0'; }
1004999
} else { pad_c = ' '; }
10051000
}
10061001
#endif

tests/unit_parse_format_spec.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ TEST_CASE("npf_parse_format_spec") {
369369
REQUIRE(spec.conv_spec == NPF_FMT_SPEC_CONV_STRING);
370370
}
371371

372-
SUBCASE("s clears leading 0") {
372+
SUBCASE("s preserves leading 0") {
373373
REQUIRE(npf_parse_format_spec("%0s", &spec) == 3);
374-
REQUIRE(!spec.leading_zero_pad);
374+
REQUIRE(spec.leading_zero_pad);
375375
}
376376

377377
SUBCASE("string negative left-justify field width") {

tests/unit_vpprintf.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,14 @@ TEST_CASE("npf_vpprintf") {
263263
REQUIRE(r.String() == std::string{"-1"});
264264
}
265265

266-
SUBCASE("leading zero-pad flag does nothing on char (undefined)") {
267-
REQUIRE(npf_pprintf(r.PutC, &r, "%010c", 'A') == 1);
268-
REQUIRE(r.String() == std::string{"A"});
266+
SUBCASE("leading zero-pad flag is honored for char (undefined)") {
267+
REQUIRE(npf_pprintf(r.PutC, &r, "%010c", 'A') == 10);
268+
REQUIRE(r.String() == std::string{"000000000A"});
269269
}
270270

271-
SUBCASE("leading zero-pad flag does nothing on string (undefined)") {
272-
REQUIRE(npf_pprintf(r.PutC, &r, "%0s", "ABCD") == 4);
273-
REQUIRE(r.String() == std::string{"ABCD"});
271+
SUBCASE("leading zero-pad flag is honored for string (undefined)") {
272+
REQUIRE(npf_pprintf(r.PutC, &r, "%05s", "ABCD") == 5);
273+
REQUIRE(r.String() == std::string{"0ABCD"});
274274
}
275275

276276
SUBCASE("alternative flag: hex doesn't prepend 0x if value is 0") {

0 commit comments

Comments
 (0)