From b059a5e64714f2bd727ac11643803f1e6f986838 Mon Sep 17 00:00:00 2001 From: Brendan McCarthy Date: Tue, 10 Jan 2017 20:07:41 +1000 Subject: [PATCH] Allow %s as generic format specifier in printf Signed integers are formatted as %d Unsigned integers are formatted as %u Doubles are formatted as %f Chars are formatted as %c Void Pointers are formatted as %p --- fmt/printf.h | 26 ++++++++++++++++++++++++++ test/printf-test.cc | 12 ++++++++++++ 2 files changed, 38 insertions(+) diff --git a/fmt/printf.h b/fmt/printf.h index 28f32f8e..6ee63a74 100644 --- a/fmt/printf.h +++ b/fmt/printf.h @@ -92,9 +92,18 @@ class ArgConverter : public ArgVisitor, void> { visit_any_int(value); } + void visit_char(char value) { + if (type_ != 's' && type_ != 'S') + visit_any_int(value); + } + template void visit_any_int(U value) { bool is_signed = type_ == 'd' || type_ == 'i'; + if (type_ == 's' || type_ == 'S') { + is_signed = std::numeric_limits::is_signed; + } + using internal::Arg; typedef typename internal::Conditional< is_same::value, U, T>::type TargetType; @@ -463,6 +472,23 @@ void PrintfFormatter::format(BasicCStringRef format_str) { if (!*s) FMT_THROW(FormatError("invalid format string")); spec.type_ = static_cast(*s++); + + if (spec.type_ == 's' || spec.type_ == 'S') { + if (arg.type == Arg::POINTER) { + spec.type_ = 'p'; + } else if (arg.type == Arg::CHAR) { + spec.type_ = 'c'; + } else if (arg.type == Arg::BOOL) { + // leave bool alone + } else if (arg.type <= Arg::LAST_INTEGER_TYPE) { + // all other integer types + spec.type_ = 'd'; + } else if (arg.type <= Arg::LAST_NUMERIC_TYPE) { + // all floating point types + spec.type_ = 'f'; + } + } + if (arg.type <= Arg::LAST_INTEGER_TYPE) { // Normalize type. switch (spec.type_) { diff --git a/test/printf-test.cc b/test/printf-test.cc index 6bba02e1..bbbb9f5b 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -202,6 +202,8 @@ TEST(PrintfTest, HashFlag) { TEST(PrintfTest, Width) { EXPECT_PRINTF(" abc", "%5s", "abc"); + EXPECT_PRINTF(" -42", "%5s", "-42"); + EXPECT_PRINTF(" 0.123456", "%10s", 0.123456); // Width cannot be specified twice. EXPECT_THROW_MSG(fmt::sprintf("%5-5d", 42), FormatError, @@ -381,11 +383,14 @@ TEST(PrintfTest, Bool) { TEST(PrintfTest, Int) { EXPECT_PRINTF("-42", "%d", -42); EXPECT_PRINTF("-42", "%i", -42); + EXPECT_PRINTF("-42", "%s", -42); unsigned u = 0 - 42u; EXPECT_PRINTF(fmt::format("{}", u), "%u", -42); EXPECT_PRINTF(fmt::format("{:o}", u), "%o", -42); EXPECT_PRINTF(fmt::format("{:x}", u), "%x", -42); EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42); + EXPECT_PRINTF(fmt::format("{}", u), "%s", u); + EXPECT_PRINTF(fmt::format("{}", u), "%S", u); } TEST(PrintfTest, LongLong) { @@ -398,6 +403,8 @@ TEST(PrintfTest, LongLong) { TEST(PrintfTest, Float) { EXPECT_PRINTF("392.650000", "%f", 392.65); EXPECT_PRINTF("392.650000", "%F", 392.65); + EXPECT_PRINTF("392.650000", "%s", 392.65); + EXPECT_PRINTF("392.650000", "%S", 392.65); char buffer[BUFFER_SIZE]; safe_sprintf(buffer, "%e", 392.65); EXPECT_PRINTF(buffer, "%e", 392.65); @@ -422,6 +429,7 @@ TEST(PrintfTest, Inf) { TEST(PrintfTest, Char) { EXPECT_PRINTF("x", "%c", 'x'); + EXPECT_PRINTF("x", "%s", 'x'); int max = std::numeric_limits::max(); EXPECT_PRINTF(fmt::format("{}", static_cast(max)), "%c", max); //EXPECT_PRINTF("x", "%lc", L'x'); @@ -440,13 +448,17 @@ TEST(PrintfTest, Pointer) { int n; void *p = &n; EXPECT_PRINTF(fmt::format("{}", p), "%p", p); + EXPECT_PRINTF(fmt::format("{}", p), "%s", p); p = 0; EXPECT_PRINTF("(nil)", "%p", p); EXPECT_PRINTF(" (nil)", "%10p", p); + EXPECT_PRINTF("(nil)", "%s", p); + EXPECT_PRINTF(" (nil)", "%10s", p); const char *s = "test"; EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s); const char *null_str = 0; EXPECT_PRINTF("(nil)", "%p", null_str); + EXPECT_PRINTF("(null)", "%s", null_str); } TEST(PrintfTest, Location) {