mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-02 11:28:20 +00:00
Format null pointer as (nil) and null string as (null) in printf (#226)
This commit is contained in:
parent
e61c8d4cba
commit
b5fda1c90d
14
format.cc
14
format.cc
@ -540,6 +540,20 @@ class PrintfArgFormatter :
|
||||
*out = static_cast<Char>(value);
|
||||
}
|
||||
|
||||
void visit_cstring(const char *value) {
|
||||
if (value)
|
||||
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_cstring(value);
|
||||
else
|
||||
this->writer() << "(null)";
|
||||
}
|
||||
|
||||
void visit_pointer(const void *value) {
|
||||
if (value)
|
||||
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_pointer(value);
|
||||
else
|
||||
this->writer() << "(nil)";
|
||||
}
|
||||
|
||||
void visit_custom(Arg::CustomValue c) {
|
||||
BasicFormatter<Char> formatter(ArgList(), this->writer());
|
||||
const Char format_str[] = {'}', 0};
|
||||
|
@ -418,6 +418,8 @@ TEST(PrintfTest, Char) {
|
||||
|
||||
TEST(PrintfTest, String) {
|
||||
EXPECT_PRINTF("abc", "%s", "abc");
|
||||
const char *null_str = 0;
|
||||
EXPECT_PRINTF("(null)", "%s", null_str);
|
||||
// TODO: wide string
|
||||
}
|
||||
|
||||
@ -425,6 +427,8 @@ TEST(PrintfTest, Pointer) {
|
||||
int n;
|
||||
void *p = &n;
|
||||
EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
|
||||
p = 0;
|
||||
EXPECT_PRINTF("(nil)", "%p", p);
|
||||
const char *s = "test";
|
||||
EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user