Skip to content

Commit 1a2a333

Browse files
committed
Use format specifiers when formatting null pointers & strings
1 parent 289885e commit 1a2a333

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

format.cc

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,17 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
425425
BasicWriter<Char> &writer() { return writer_; }
426426
FormatSpec &spec() { return spec_; }
427427

428-
void write_bool(bool value) {
428+
void write(bool value) {
429429
const char *str_value = value ? "true" : "false";
430430
Arg::StringValue<char> str = { str_value, strlen(str_value) };
431431
writer_.write_str(str, spec_);
432432
}
433433

434+
void write(const char *value) {
435+
Arg::StringValue<char> str = {value, 0};
436+
writer_.write_str(str, spec_);
437+
}
438+
434439
public:
435440
BasicArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
436441
: writer_(w), spec_(s) {}
@@ -444,7 +449,7 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
444449
void visit_bool(bool value) {
445450
if (spec_.type_)
446451
return visit_any_int(value);
447-
write_bool(value);
452+
write(value);
448453
}
449454

450455
void visit_char(int value) {
@@ -479,8 +484,7 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
479484
void visit_cstring(const char *value) {
480485
if (spec_.type_ == 'p')
481486
return write_pointer(value);
482-
Arg::StringValue<char> str = {value, 0};
483-
writer_.write_str(str, spec_);
487+
write(value);
484488
}
485489

486490
void visit_string(Arg::StringValue<char> value) {
@@ -522,9 +526,12 @@ class PrintfArgFormatter :
522526
public BasicArgFormatter<PrintfArgFormatter<Char>, Char> {
523527

524528
void write_null_pointer() {
525-
this->writer() << "(nil)";
529+
this->spec().type_ = 0;
530+
this->write("(nil)");
526531
}
527532

533+
typedef BasicArgFormatter<PrintfArgFormatter<Char>, Char> Base;
534+
528535
public:
529536
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
530537
: BasicArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
@@ -534,7 +541,7 @@ class PrintfArgFormatter :
534541
if (fmt_spec.type_ != 's')
535542
return this->visit_any_int(value);
536543
fmt_spec.type_ = 0;
537-
this->write_bool(value);
544+
this->write(value);
538545
}
539546

540547
void visit_char(int value) {
@@ -561,18 +568,18 @@ class PrintfArgFormatter :
561568

562569
void visit_cstring(const char *value) {
563570
if (value)
564-
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_cstring(value);
571+
Base::visit_cstring(value);
565572
else if (this->spec().type_ == 'p')
566573
write_null_pointer();
567574
else
568-
this->writer() << "(null)";
575+
this->write("(null)");
569576
}
570577

571578
void visit_pointer(const void *value) {
572579
if (value)
573-
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_pointer(value);
574-
else
575-
write_null_pointer();
580+
return Base::visit_pointer(value);
581+
this->spec().type_ = 0;
582+
write_null_pointer();
576583
}
577584

578585
void visit_custom(Arg::CustomValue c) {

test/printf-test.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ TEST(PrintfTest, String) {
425425
EXPECT_PRINTF("abc", "%s", "abc");
426426
const char *null_str = 0;
427427
EXPECT_PRINTF("(null)", "%s", null_str);
428+
EXPECT_PRINTF(" (null)", "%10s", null_str);
428429
// TODO: wide string
429430
}
430431

@@ -434,6 +435,7 @@ TEST(PrintfTest, Pointer) {
434435
EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
435436
p = 0;
436437
EXPECT_PRINTF("(nil)", "%p", p);
438+
EXPECT_PRINTF(" (nil)", "%10p", p);
437439
const char *s = "test";
438440
EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
439441
const char *null_str = 0;

0 commit comments

Comments
 (0)