[PATCH] D48140: Fix padding with custom character in formatv
Zachary Turner via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 28 13:14:37 PDT 2018
This revision was not accepted when it landed; it landed in state "Needs Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rL335915: Fix padding with custom character in formatv. (authored by zturner, committed by ).
Repository:
rL LLVM
https://reviews.llvm.org/D48140
Files:
llvm/trunk/include/llvm/Support/FormatVariadic.h
llvm/trunk/unittests/Support/FormatVariadicTest.cpp
Index: llvm/trunk/unittests/Support/FormatVariadicTest.cpp
===================================================================
--- llvm/trunk/unittests/Support/FormatVariadicTest.cpp
+++ llvm/trunk/unittests/Support/FormatVariadicTest.cpp
@@ -143,6 +143,58 @@
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 @@
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) {}
Index: llvm/trunk/include/llvm/Support/FormatVariadic.h
===================================================================
--- llvm/trunk/include/llvm/Support/FormatVariadic.h
+++ llvm/trunk/include/llvm/Support/FormatVariadic.h
@@ -118,7 +118,7 @@
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);
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48140.153380.patch
Type: text/x-patch
Size: 3788 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180628/9969e44b/attachment.bin>
More information about the llvm-commits
mailing list