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_; }
|
BasicWriter<Char> &writer() { return writer_; }
|
||||||
FormatSpec &spec() { return spec_; }
|
FormatSpec &spec() { return spec_; }
|
||||||
|
|
||||||
void write_bool(bool value) {
|
void write(bool value) {
|
||||||
const char *str_value = value ? "true" : "false";
|
const char *str_value = value ? "true" : "false";
|
||||||
Arg::StringValue<char> str = { str_value, strlen(str_value) };
|
Arg::StringValue<char> str = { str_value, strlen(str_value) };
|
||||||
writer_.write_str(str, spec_);
|
writer_.write_str(str, spec_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void write(const char *value) {
|
||||||
|
Arg::StringValue<char> str = {value, 0};
|
||||||
|
writer_.write_str(str, spec_);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BasicArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
|
BasicArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
|
||||||
: writer_(w), spec_(s) {}
|
: writer_(w), spec_(s) {}
|
||||||
@ -444,7 +449,7 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
|
|||||||
void visit_bool(bool value) {
|
void visit_bool(bool value) {
|
||||||
if (spec_.type_)
|
if (spec_.type_)
|
||||||
return visit_any_int(value);
|
return visit_any_int(value);
|
||||||
write_bool(value);
|
write(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void visit_char(int value) {
|
void visit_char(int value) {
|
||||||
@ -479,8 +484,7 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
|
|||||||
void visit_cstring(const char *value) {
|
void visit_cstring(const char *value) {
|
||||||
if (spec_.type_ == 'p')
|
if (spec_.type_ == 'p')
|
||||||
return write_pointer(value);
|
return write_pointer(value);
|
||||||
Arg::StringValue<char> str = {value, 0};
|
write(value);
|
||||||
writer_.write_str(str, spec_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void visit_string(Arg::StringValue<char> value) {
|
void visit_string(Arg::StringValue<char> value) {
|
||||||
@ -522,9 +526,12 @@ class PrintfArgFormatter :
|
|||||||
public BasicArgFormatter<PrintfArgFormatter<Char>, Char> {
|
public BasicArgFormatter<PrintfArgFormatter<Char>, Char> {
|
||||||
|
|
||||||
void write_null_pointer() {
|
void write_null_pointer() {
|
||||||
this->writer() << "(nil)";
|
this->spec().type_ = 0;
|
||||||
|
this->write("(nil)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef BasicArgFormatter<PrintfArgFormatter<Char>, Char> Base;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
|
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
|
||||||
: BasicArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
|
: BasicArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
|
||||||
@ -534,7 +541,7 @@ class PrintfArgFormatter :
|
|||||||
if (fmt_spec.type_ != 's')
|
if (fmt_spec.type_ != 's')
|
||||||
return this->visit_any_int(value);
|
return this->visit_any_int(value);
|
||||||
fmt_spec.type_ = 0;
|
fmt_spec.type_ = 0;
|
||||||
this->write_bool(value);
|
this->write(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void visit_char(int value) {
|
void visit_char(int value) {
|
||||||
@ -561,18 +568,18 @@ class PrintfArgFormatter :
|
|||||||
|
|
||||||
void visit_cstring(const char *value) {
|
void visit_cstring(const char *value) {
|
||||||
if (value)
|
if (value)
|
||||||
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_cstring(value);
|
Base::visit_cstring(value);
|
||||||
else if (this->spec().type_ == 'p')
|
else if (this->spec().type_ == 'p')
|
||||||
write_null_pointer();
|
write_null_pointer();
|
||||||
else
|
else
|
||||||
this->writer() << "(null)";
|
this->write("(null)");
|
||||||
}
|
}
|
||||||
|
|
||||||
void visit_pointer(const void *value) {
|
void visit_pointer(const void *value) {
|
||||||
if (value)
|
if (value)
|
||||||
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_pointer(value);
|
return Base::visit_pointer(value);
|
||||||
else
|
this->spec().type_ = 0;
|
||||||
write_null_pointer();
|
write_null_pointer();
|
||||||
}
|
}
|
||||||
|
|
||||||
void visit_custom(Arg::CustomValue c) {
|
void visit_custom(Arg::CustomValue c) {
|
||||||
|
@ -425,6 +425,7 @@ TEST(PrintfTest, String) {
|
|||||||
EXPECT_PRINTF("abc", "%s", "abc");
|
EXPECT_PRINTF("abc", "%s", "abc");
|
||||||
const char *null_str = 0;
|
const char *null_str = 0;
|
||||||
EXPECT_PRINTF("(null)", "%s", null_str);
|
EXPECT_PRINTF("(null)", "%s", null_str);
|
||||||
|
EXPECT_PRINTF(" (null)", "%10s", null_str);
|
||||||
// TODO: wide string
|
// TODO: wide string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -434,6 +435,7 @@ TEST(PrintfTest, Pointer) {
|
|||||||
EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
|
EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
|
||||||
p = 0;
|
p = 0;
|
||||||
EXPECT_PRINTF("(nil)", "%p", p);
|
EXPECT_PRINTF("(nil)", "%p", p);
|
||||||
|
EXPECT_PRINTF(" (nil)", "%10p", p);
|
||||||
const char *s = "test";
|
const char *s = "test";
|
||||||
EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
|
EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
|
||||||
const char *null_str = 0;
|
const char *null_str = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user