Fix padding with custom character in formatv.

The format string for formatv allows to specify a custom padding
character instead of the default space.  This custom character was
parsed correctly, but not passed on to the formatter.

Patch by Marcel Köppe
Differential Revision: https://reviews.llvm.org/D48140

llvm-svn: 335915
This commit is contained in:
Zachary Turner 2018-06-28 20:09:37 +00:00
parent e87868b7e9
commit 402ab4964e
2 changed files with 63 additions and 1 deletions

View File

@ -118,7 +118,7 @@ public:
auto W = Adapters[R.Index];
FmtAlign Align(*W, R.Where, R.Align);
FmtAlign Align(*W, R.Where, R.Align, R.Pad);
Align.format(S, R.Options);
}
}

View File

@ -143,6 +143,58 @@ TEST(FormatVariadicTest, ValidReplacementSequence) {
EXPECT_EQ(0u, Replacements[0].Align);
EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
EXPECT_EQ("0:1", Replacements[0].Options);
// 9. Custom padding character
Replacements = formatv_object_base::parseFormatString("{0,p+4:foo}");
ASSERT_EQ(1u, Replacements.size());
EXPECT_EQ("0,p+4:foo", Replacements[0].Spec);
EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
EXPECT_EQ(0u, Replacements[0].Index);
EXPECT_EQ(4u, Replacements[0].Align);
EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
EXPECT_EQ('p', Replacements[0].Pad);
EXPECT_EQ("foo", Replacements[0].Options);
// Format string special characters are allowed as padding character
Replacements = formatv_object_base::parseFormatString("{0,-+4:foo}");
ASSERT_EQ(1u, Replacements.size());
EXPECT_EQ("0,-+4:foo", Replacements[0].Spec);
EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
EXPECT_EQ(0u, Replacements[0].Index);
EXPECT_EQ(4u, Replacements[0].Align);
EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
EXPECT_EQ('-', Replacements[0].Pad);
EXPECT_EQ("foo", Replacements[0].Options);
Replacements = formatv_object_base::parseFormatString("{0,+-4:foo}");
ASSERT_EQ(1u, Replacements.size());
EXPECT_EQ("0,+-4:foo", Replacements[0].Spec);
EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
EXPECT_EQ(0u, Replacements[0].Index);
EXPECT_EQ(4u, Replacements[0].Align);
EXPECT_EQ(AlignStyle::Left, Replacements[0].Where);
EXPECT_EQ('+', Replacements[0].Pad);
EXPECT_EQ("foo", Replacements[0].Options);
Replacements = formatv_object_base::parseFormatString("{0,==4:foo}");
ASSERT_EQ(1u, Replacements.size());
EXPECT_EQ("0,==4:foo", Replacements[0].Spec);
EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
EXPECT_EQ(0u, Replacements[0].Index);
EXPECT_EQ(4u, Replacements[0].Align);
EXPECT_EQ(AlignStyle::Center, Replacements[0].Where);
EXPECT_EQ('=', Replacements[0].Pad);
EXPECT_EQ("foo", Replacements[0].Options);
Replacements = formatv_object_base::parseFormatString("{0,:=4:foo}");
ASSERT_EQ(1u, Replacements.size());
EXPECT_EQ("0,:=4:foo", Replacements[0].Spec);
EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
EXPECT_EQ(0u, Replacements[0].Index);
EXPECT_EQ(4u, Replacements[0].Align);
EXPECT_EQ(AlignStyle::Center, Replacements[0].Where);
EXPECT_EQ(':', Replacements[0].Pad);
EXPECT_EQ("foo", Replacements[0].Options);
}
TEST(FormatVariadicTest, DefaultReplacementValues) {
@ -421,6 +473,16 @@ TEST(FormatVariadicTest, DoubleFormatting) {
EXPECT_EQ(" -0.001", formatv("{0,8:F3}", -.0012345678).str());
}
TEST(FormatVariadicTest, CustomPaddingCharacter) {
// 1. Padding with custom character
EXPECT_EQ("==123", formatv("{0,=+5}", 123).str());
EXPECT_EQ("=123=", formatv("{0,==5}", 123).str());
EXPECT_EQ("123==", formatv("{0,=-5}", 123).str());
// 2. Combined with zero padding
EXPECT_EQ("=00123=", formatv("{0,==7:5}", 123).str());
}
struct format_tuple {
const char *Fmt;
explicit format_tuple(const char *Fmt) : Fmt(Fmt) {}