mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-23 23:10:04 +00:00
Format null string as (nil)
with p
specifier
This commit is contained in:
parent
5e37698911
commit
57ba9436a0
@ -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) {
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user