Fix padding with custom character in formatv.
authorZachary Turner <zturner@google.com>
Thu, 28 Jun 2018 20:09:37 +0000 (20:09 +0000)
committerZachary Turner <zturner@google.com>
Thu, 28 Jun 2018 20:09:37 +0000 (20:09 +0000)
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

llvm/include/llvm/Support/FormatVariadic.h
llvm/unittests/Support/FormatVariadicTest.cpp

index 70e286f..eb69761 100644 (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);
     }
   }
index ddecffd..54373da 100644 (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) {}