[llvm] r305449 - [formatv] Add the ability to specify a fill character when aligning.
Zachary Turner via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 14 20:06:39 PDT 2017
Author: zturner
Date: Wed Jun 14 22:06:38 2017
New Revision: 305449
URL: http://llvm.org/viewvc/llvm-project?rev=305449&view=rev
Log:
[formatv] Add the ability to specify a fill character when aligning.
Previously if you used fmt_align(7, Center) you would get the
output ' 7 '. It may be desirable for the user to specify
the fill character though, for example producing '---7---'. This
patch adds that.
Modified:
llvm/trunk/include/llvm/Support/FormatAdapters.h
llvm/trunk/include/llvm/Support/FormatCommon.h
llvm/trunk/unittests/Support/FormatVariadicTest.cpp
Modified: llvm/trunk/include/llvm/Support/FormatAdapters.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FormatAdapters.h?rev=305449&r1=305448&r2=305449&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/FormatAdapters.h (original)
+++ llvm/trunk/include/llvm/Support/FormatAdapters.h Wed Jun 14 22:06:38 2017
@@ -28,14 +28,16 @@ namespace detail {
template <typename T> class AlignAdapter final : public FormatAdapter<T> {
AlignStyle Where;
size_t Amount;
+ char Fill;
public:
- AlignAdapter(T &&Item, AlignStyle Where, size_t Amount)
- : FormatAdapter<T>(std::forward<T>(Item)), Where(Where), Amount(Amount) {}
+ AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
+ : FormatAdapter<T>(std::forward<T>(Item)), Where(Where), Amount(Amount),
+ Fill(Fill) {}
void format(llvm::raw_ostream &Stream, StringRef Style) {
auto Adapter = detail::build_format_adapter(std::forward<T>(this->Item));
- FmtAlign(Adapter, Where, Amount).format(Stream, Style);
+ FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
}
};
@@ -72,8 +74,9 @@ public:
}
template <typename T>
-detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where, size_t Amount) {
- return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount);
+detail::AlignAdapter<T> fmt_align(T &&Item, AlignStyle Where, size_t Amount,
+ char Fill = ' ') {
+ return detail::AlignAdapter<T>(std::forward<T>(Item), Where, Amount, Fill);
}
template <typename T>
Modified: llvm/trunk/include/llvm/Support/FormatCommon.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FormatCommon.h?rev=305449&r1=305448&r2=305449&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/FormatCommon.h (original)
+++ llvm/trunk/include/llvm/Support/FormatCommon.h Wed Jun 14 22:06:38 2017
@@ -21,9 +21,11 @@ struct FmtAlign {
detail::format_adapter &Adapter;
AlignStyle Where;
size_t Amount;
+ char Fill;
- FmtAlign(detail::format_adapter &Adapter, AlignStyle Where, size_t Amount)
- : Adapter(Adapter), Where(Where), Amount(Amount) {}
+ FmtAlign(detail::format_adapter &Adapter, AlignStyle Where, size_t Amount,
+ char Fill = ' ')
+ : Adapter(Adapter), Where(Where), Amount(Amount), Fill(Fill) {}
void format(raw_ostream &S, StringRef Options) {
// If we don't need to align, we can format straight into the underlying
@@ -48,21 +50,27 @@ struct FmtAlign {
switch (Where) {
case AlignStyle::Left:
S << Item;
- S.indent(PadAmount);
+ fill(S, PadAmount);
break;
case AlignStyle::Center: {
size_t X = PadAmount / 2;
- S.indent(X);
+ fill(S, X);
S << Item;
- S.indent(PadAmount - X);
+ fill(S, PadAmount - X);
break;
}
default:
- S.indent(PadAmount);
+ fill(S, PadAmount);
S << Item;
break;
}
}
+
+private:
+ void fill(llvm::raw_ostream &S, uint32_t Count) {
+ for (uint32_t I = 0; I < Count; ++I)
+ S << Fill;
+ }
};
}
Modified: llvm/trunk/unittests/Support/FormatVariadicTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/FormatVariadicTest.cpp?rev=305449&r1=305448&r2=305449&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/FormatVariadicTest.cpp (original)
+++ llvm/trunk/unittests/Support/FormatVariadicTest.cpp Wed Jun 14 22:06:38 2017
@@ -542,6 +542,8 @@ TEST(FormatVariadicTest, Adapter) {
EXPECT_EQ(" 171 ",
formatv("{0}", fmt_align(N, AlignStyle::Center, 7)).str());
+ EXPECT_EQ("--171--",
+ formatv("{0}", fmt_align(N, AlignStyle::Center, 7, '-')).str());
EXPECT_EQ(" 171 ", formatv("{0}", fmt_pad(N, 1, 3)).str());
EXPECT_EQ("171171171171171", formatv("{0}", fmt_repeat(N, 5)).str());
More information about the llvm-commits
mailing list