Convert integer argument like printf does.

This commit is contained in:
Victor Zverovich 2014-08-19 08:49:10 -07:00
parent 406c6123fb
commit be00d8b272
2 changed files with 14 additions and 2 deletions

View File

@ -974,6 +974,7 @@ void fmt::internal::PrintfFormatter<Char>::format(
break; break;
default: default:
--s; --s;
ArgConverter<int>(arg, *s).visit(arg);
} }
// Parse type. // Parse type.

View File

@ -165,12 +165,13 @@ TEST(PrintfTest, SpaceFlag) {
TEST(PrintfTest, HashFlag) { TEST(PrintfTest, HashFlag) {
EXPECT_PRINTF("042", "%#o", 042); EXPECT_PRINTF("042", "%#o", 042);
EXPECT_PRINTF("-042", "%#o", -042); EXPECT_PRINTF(fmt::format("0{:o}", static_cast<unsigned>(-042)), "%#o", -042);
EXPECT_PRINTF("0", "%#o", 0); EXPECT_PRINTF("0", "%#o", 0);
EXPECT_PRINTF("0x42", "%#x", 0x42); EXPECT_PRINTF("0x42", "%#x", 0x42);
EXPECT_PRINTF("0X42", "%#X", 0x42); EXPECT_PRINTF("0X42", "%#X", 0x42);
EXPECT_PRINTF("-0x42", "%#x", -0x42); EXPECT_PRINTF(
fmt::format("0x{:x}", static_cast<unsigned>(-0x42)), "%#x", -0x42);
EXPECT_PRINTF("0", "%#x", 0); EXPECT_PRINTF("0", "%#x", 0);
EXPECT_PRINTF("0x0042", "%#06x", 0x42); EXPECT_PRINTF("0x0042", "%#06x", 0x42);
@ -370,6 +371,16 @@ TEST(PrintfTest, Length) {
EXPECT_PRINTF(fmt::format("{}", max), "%Lg", max); EXPECT_PRINTF(fmt::format("{}", max), "%Lg", max);
} }
TEST(PrintfTest, Int) {
EXPECT_PRINTF("-42", "%d", -42);
EXPECT_PRINTF("-42", "%i", -42);
unsigned u = -42;
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);
}
TEST(PrintfTest, Char) { TEST(PrintfTest, Char) {
EXPECT_PRINTF("x", "%c", 'x'); EXPECT_PRINTF("x", "%c", 'x');
int max = std::numeric_limits<int>::max(); int max = std::numeric_limits<int>::max();