1 //===- FormatCommon.h - Formatters for common LLVM types --------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_SUPPORT_FORMATCOMMON_H
10 #define LLVM_SUPPORT_FORMATCOMMON_H
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/Support/FormatVariadicDetails.h"
14 #include "llvm/Support/raw_ostream.h"
17 enum class AlignStyle
{ Left
, Center
, Right
};
20 detail::format_adapter
&Adapter
;
25 FmtAlign(detail::format_adapter
&Adapter
, AlignStyle Where
, size_t Amount
,
27 : Adapter(Adapter
), Where(Where
), Amount(Amount
), Fill(Fill
) {}
29 void format(raw_ostream
&S
, StringRef Options
) {
30 // If we don't need to align, we can format straight into the underlying
31 // stream. Otherwise we have to go through an intermediate stream first
32 // in order to calculate how long the output is so we can align it.
33 // TODO: Make the format method return the number of bytes written, that
34 // way we can also skip the intermediate stream for left-aligned output.
36 Adapter
.format(S
, Options
);
40 raw_svector_ostream
Stream(Item
);
42 Adapter
.format(Stream
, Options
);
43 if (Amount
<= Item
.size()) {
48 size_t PadAmount
= Amount
- Item
.size();
50 case AlignStyle::Left
:
54 case AlignStyle::Center
: {
55 size_t X
= PadAmount
/ 2;
58 fill(S
, PadAmount
- X
);
69 void fill(llvm::raw_ostream
&S
, uint32_t Count
) {
70 for (uint32_t I
= 0; I
< Count
; ++I
)