diff options
-rw-r--r-- | include/llvm/Support/FormatAdapters.h | 13 | ||||
-rw-r--r-- | include/llvm/Support/FormatCommon.h | 20 | ||||
-rw-r--r-- | 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 698e134b328d..197beb7363df 100644 --- a/include/llvm/Support/FormatAdapters.h +++ b/include/llvm/Support/FormatAdapters.h @@ -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> diff --git a/include/llvm/Support/FormatCommon.h b/include/llvm/Support/FormatCommon.h index a8c5fdeb6bff..36fbad296c3f 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 99b90b17ae44..5387a8ae499c 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()); |