llvm.org GIT mirror llvm / ba50355
[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 Zachary Turner 3 years ago
3 changed file(s) with 24 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
2727 template class AlignAdapter final : public FormatAdapter {
2828 AlignStyle Where;
2929 size_t Amount;
30 char Fill;
3031
3132 public:
32 AlignAdapter(T &&Item, AlignStyle Where, size_t Amount)
33 : FormatAdapter(std::forward(Item)), Where(Where), Amount(Amount) {}
33 AlignAdapter(T &&Item, AlignStyle Where, size_t Amount, char Fill)
34 : FormatAdapter(std::forward(Item)), Where(Where), Amount(Amount),
35 Fill(Fill) {}
3436
3537 void format(llvm::raw_ostream &Stream, StringRef Style) {
3638 auto Adapter = detail::build_format_adapter(std::forward(this->Item));
37 FmtAlign(Adapter, Where, Amount).format(Stream, Style);
39 FmtAlign(Adapter, Where, Amount, Fill).format(Stream, Style);
3840 }
3941 };
4042
7173 }
7274
7375 template
74 detail::AlignAdapter fmt_align(T &&Item, AlignStyle Where, size_t Amount) {
75 return detail::AlignAdapter(std::forward(Item), Where, Amount);
76 detail::AlignAdapter fmt_align(T &&Item, AlignStyle Where, size_t Amount,
77 char Fill = ' ') {
78 return detail::AlignAdapter(std::forward(Item), Where, Amount, Fill);
7679 }
7780
7881 template
2020 detail::format_adapter &Adapter;
2121 AlignStyle Where;
2222 size_t Amount;
23 char Fill;
2324
24 FmtAlign(detail::format_adapter &Adapter, AlignStyle Where, size_t Amount)
25 : Adapter(Adapter), Where(Where), Amount(Amount) {}
25 FmtAlign(detail::format_adapter &Adapter, AlignStyle Where, size_t Amount,
26 char Fill = ' ')
27 : Adapter(Adapter), Where(Where), Amount(Amount), Fill(Fill) {}
2628
2729 void format(raw_ostream &S, StringRef Options) {
2830 // If we don't need to align, we can format straight into the underlying
4749 switch (Where) {
4850 case AlignStyle::Left:
4951 S << Item;
50 S.indent(PadAmount);
52 fill(S, PadAmount);
5153 break;
5254 case AlignStyle::Center: {
5355 size_t X = PadAmount / 2;
54 S.indent(X);
56 fill(S, X);
5557 S << Item;
56 S.indent(PadAmount - X);
58 fill(S, PadAmount - X);
5759 break;
5860 }
5961 default:
60 S.indent(PadAmount);
62 fill(S, PadAmount);
6163 S << Item;
6264 break;
6365 }
66 }
67
68 private:
69 void fill(llvm::raw_ostream &S, uint32_t Count) {
70 for (uint32_t I = 0; I < Count; ++I)
71 S << Fill;
6472 }
6573 };
6674 }
541541
542542 EXPECT_EQ(" 171 ",
543543 formatv("{0}", fmt_align(N, AlignStyle::Center, 7)).str());
544 EXPECT_EQ("--171--",
545 formatv("{0}", fmt_align(N, AlignStyle::Center, 7, '-')).str());
544546 EXPECT_EQ(" 171 ", formatv("{0}", fmt_pad(N, 1, 3)).str());
545547 EXPECT_EQ("171171171171171", formatv("{0}", fmt_repeat(N, 5)).str());
546548