mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-02 11:28:20 +00:00
Use format specifiers when formatting null pointers & strings
This commit is contained in:
parent
289885e8c0
commit
1a2a333a1a
29
format.cc
29
format.cc
@ -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) {}
|
||||
@ -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) {
|
||||
@ -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) {
|
||||
@ -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) {}
|
||||
@ -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<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) {
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user