Allow formatting C strings as pointers (#223)

This commit is contained in:
vitaut
2015-11-09 07:17:36 -08:00
parent 7c24973637
commit 8b86a74ad5
5 changed files with 25 additions and 9 deletions

View File

@@ -1336,7 +1336,7 @@ TEST(FormatterTest, FormatWChar) {
}
TEST(FormatterTest, FormatCString) {
check_unknown_types("test", "s", "string");
check_unknown_types("test", "sp", "string");
EXPECT_EQ("test", format("{0}", "test"));
EXPECT_EQ("test", format("{0:s}", "test"));
char nonconst[] = "nonconst";

View File

@@ -425,6 +425,8 @@ TEST(PrintfTest, Pointer) {
int n;
void *p = &n;
EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
const char *s = "test";
EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s);
}
TEST(PrintfTest, Custom) {

View File

@@ -593,6 +593,7 @@ struct TestVisitor : fmt::internal::ArgVisitor<TestVisitor, Result> {
Result visit_double(double value) { return value; }
Result visit_long_double(long double value) { return value; }
Result visit_char(int value) { return static_cast<char>(value); }
Result visit_cstring(const char *s) { return s; }
Result visit_string(Arg::StringValue<char> s) { return s.value; }
Result visit_wstring(Arg::StringValue<wchar_t> s) { return s.value; }
Result visit_pointer(const void *p) { return p; }