Format null string as (nil) with p specifier

This commit is contained in:
vitaut 2015-11-12 06:09:08 -08:00
parent 5e37698911
commit 57ba9436a0
2 changed files with 10 additions and 1 deletions

View File

@ -514,6 +514,11 @@ class ArgFormatter : public BasicArgFormatter<ArgFormatter<Char>, Char> {
template <typename Char>
class PrintfArgFormatter :
public BasicArgFormatter<PrintfArgFormatter<Char>, Char> {
void write_null_pointer() {
this->writer() << "(nil)";
}
public:
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: BasicArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
@ -543,6 +548,8 @@ class PrintfArgFormatter :
void visit_cstring(const char *value) {
if (value)
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_cstring(value);
else if (this->spec().type_ == 'p')
write_null_pointer();
else
this->writer() << "(null)";
}
@ -551,7 +558,7 @@ class PrintfArgFormatter :
if (value)
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_pointer(value);
else
this->writer() << "(nil)";
write_null_pointer();
}
void visit_custom(Arg::CustomValue c) {

View File

@ -431,6 +431,8 @@ TEST(PrintfTest, Pointer) {
EXPECT_PRINTF("(nil)", "%p", p);
const char *s = "test";
EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
const char *null_str = 0;
EXPECT_PRINTF("(nil)", "%p", null_str);
}
TEST(PrintfTest, Custom) {