Skip to content

Commit

Permalink
Use format specifiers when formatting null pointers & strings
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 24, 2015
1 parent 289885e commit 1a2a333
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 18 additions & 11 deletions format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,17 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
BasicWriter<Char> &writer() { return writer_; }
FormatSpec &spec() { return spec_; }

void write_bool(bool value) {
void write(bool value) {
const char *str_value = value ? "true" : "false";
Arg::StringValue<char> str = { str_value, strlen(str_value) };
writer_.write_str(str, spec_);
}

void write(const char *value) {
Arg::StringValue<char> str = {value, 0};
writer_.write_str(str, spec_);
}

public:
BasicArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: writer_(w), spec_(s) {}
Expand All @@ -444,7 +449,7 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
void visit_bool(bool value) {
if (spec_.type_)
return visit_any_int(value);
write_bool(value);
write(value);
}

void visit_char(int value) {
Expand Down Expand Up @@ -479,8 +484,7 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
void visit_cstring(const char *value) {
if (spec_.type_ == 'p')
return write_pointer(value);
Arg::StringValue<char> str = {value, 0};
writer_.write_str(str, spec_);
write(value);
}

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

void write_null_pointer() {
this->writer() << "(nil)";
this->spec().type_ = 0;
this->write("(nil)");
}

typedef BasicArgFormatter<PrintfArgFormatter<Char>, Char> Base;

public:
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: BasicArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
Expand All @@ -534,7 +541,7 @@ class PrintfArgFormatter :
if (fmt_spec.type_ != 's')
return this->visit_any_int(value);
fmt_spec.type_ = 0;
this->write_bool(value);
this->write(value);
}

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

void visit_cstring(const char *value) {
if (value)
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_cstring(value);
Base::visit_cstring(value);
else if (this->spec().type_ == 'p')
write_null_pointer();
else
this->writer() << "(null)";
this->write("(null)");
}

void visit_pointer(const void *value) {
if (value)
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_pointer(value);
else
write_null_pointer();
return Base::visit_pointer(value);
this->spec().type_ = 0;
write_null_pointer();
}

void visit_custom(Arg::CustomValue c) {
Expand Down
2 changes: 2 additions & 0 deletions test/printf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ TEST(PrintfTest, String) {
EXPECT_PRINTF("abc", "%s", "abc");
const char *null_str = 0;
EXPECT_PRINTF("(null)", "%s", null_str);
EXPECT_PRINTF(" (null)", "%10s", null_str);
// TODO: wide string
}

Expand All @@ -434,6 +435,7 @@ TEST(PrintfTest, Pointer) {
EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
p = 0;
EXPECT_PRINTF("(nil)", "%p", p);
EXPECT_PRINTF(" (nil)", "%10p", p);
const char *s = "test";
EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
const char *null_str = 0;
Expand Down

0 comments on commit 1a2a333

Please sign in to comment.