From 1a2a333a1aacff4311b52232e29daa3b64f6def6 Mon Sep 17 00:00:00 2001 From: vitaut Date: Mon, 23 Nov 2015 21:01:28 -0800 Subject: [PATCH] Use format specifiers when formatting null pointers & strings --- format.cc | 29 ++++++++++++++++++----------- test/printf-test.cc | 2 ++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/format.cc b/format.cc index 270d4591..6858058f 100644 --- a/format.cc +++ b/format.cc @@ -425,12 +425,17 @@ class BasicArgFormatter : public ArgVisitor { BasicWriter &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 str = { str_value, strlen(str_value) }; writer_.write_str(str, spec_); } + void write(const char *value) { + Arg::StringValue str = {value, 0}; + writer_.write_str(str, spec_); + } + public: BasicArgFormatter(BasicWriter &w, FormatSpec &s) : writer_(w), spec_(s) {} @@ -444,7 +449,7 @@ class BasicArgFormatter : public ArgVisitor { void visit_bool(bool value) { if (spec_.type_) return visit_any_int(value); - write_bool(value); + write(value); } void visit_char(int value) { @@ -479,8 +484,7 @@ class BasicArgFormatter : public ArgVisitor { void visit_cstring(const char *value) { if (spec_.type_ == 'p') return write_pointer(value); - Arg::StringValue str = {value, 0}; - writer_.write_str(str, spec_); + write(value); } void visit_string(Arg::StringValue value) { @@ -522,9 +526,12 @@ class PrintfArgFormatter : public BasicArgFormatter, Char> { void write_null_pointer() { - this->writer() << "(nil)"; + this->spec().type_ = 0; + this->write("(nil)"); } + typedef BasicArgFormatter, Char> Base; + public: PrintfArgFormatter(BasicWriter &w, FormatSpec &s) : BasicArgFormatter, Char>(w, s) {} @@ -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) { @@ -561,18 +568,18 @@ class PrintfArgFormatter : void visit_cstring(const char *value) { if (value) - BasicArgFormatter, 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, 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) { diff --git a/test/printf-test.cc b/test/printf-test.cc index c985d026..4c35edb0 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -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 } @@ -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;