[PATCH] D48140: Fix padding with custom character in formatv
Marcel Köppen via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 13 11:41:19 PDT 2018
mkoeppen created this revision.
mkoeppen added a reviewer: zturner.
Herald added a subscriber: llvm-commits.
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.
Repository:
rL LLVM
https://reviews.llvm.org/D48140
Files:
include/llvm/Support/FormatVariadic.h
unittests/Support/FormatVariadicTest.cpp
Index: unittests/Support/FormatVariadicTest.cpp
===================================================================
--- unittests/Support/FormatVariadicTest.cpp
+++ 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: include/llvm/Support/FormatVariadic.h
===================================================================
--- include/llvm/Support/FormatVariadic.h
+++ 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.151211.patch
Type: text/x-patch
Size: 3722 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180613/dda03eef/attachment.bin>
More information about the llvm-commits
mailing list