[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
This commit is contained in:
Zachary Turner 2017-06-15 03:06:38 +00:00
parent 46d1553fcd
commit ba503556ac
3 changed files with 24 additions and 11 deletions

View File

@ -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>

View File

@ -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;
}
};
}

View File

@ -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());