From ba503556ac20f40b3dee239ce6f8cad9dea7e614 Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Thu, 15 Jun 2017 03:06:38 +0000 Subject: [PATCH] [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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305449 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/FormatAdapters.h | 13 ++++++++----- include/llvm/Support/FormatCommon.h | 20 ++++++++++++++------ unittests/Support/FormatVariadicTest.cpp | 2 ++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/include/llvm/Support/FormatAdapters.h b/include/llvm/Support/FormatAdapters.h index 698e134b328..197beb7363d 100644 --- a/include/llvm/Support/FormatAdapters.h +++ b/include/llvm/Support/FormatAdapters.h @@ -28,14 +28,16 @@ namespace detail { template class AlignAdapter final : public FormatAdapter { AlignStyle Where; size_t Amount; + char Fill; public: - AlignAdapter(T &&Item, AlignStyle Where, size_t Amount) - : FormatAdapter(std::forward(Item)), Where(Where), Amount(Amount) {} + AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill) + : FormatAdapter(std::forward(Item)), Where(Where), Amount(Amount), + Fill(Fill) {} void format(llvm::raw_ostream &Stream, StringRef Style) { auto Adapter = detail::build_format_adapter(std::forward(this->Item)); - FmtAlign(Adapter, Where, Amount).format(Stream, Style); + FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style); } }; @@ -72,8 +74,9 @@ public: } template -detail::AlignAdapter fmt_align(T &&Item, AlignStyle Where, size_t Amount) { - return detail::AlignAdapter(std::forward(Item), Where, Amount); +detail::AlignAdapter fmt_align(T &&Item, AlignStyle Where, size_t Amount, + char Fill = ' ') { + return detail::AlignAdapter(std::forward(Item), Where, Amount, Fill); } template diff --git a/include/llvm/Support/FormatCommon.h b/include/llvm/Support/FormatCommon.h index a8c5fdeb6bf..36fbad296c3 100644 --- a/include/llvm/Support/FormatCommon.h +++ b/include/llvm/Support/FormatCommon.h @@ -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; + } }; } diff --git a/unittests/Support/FormatVariadicTest.cpp b/unittests/Support/FormatVariadicTest.cpp index 99b90b17ae4..5387a8ae499 100644 --- a/unittests/Support/FormatVariadicTest.cpp +++ b/unittests/Support/FormatVariadicTest.cpp @@ -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()); -- 2.50.1