/home/arjun/llvm-project/llvm/include/llvm/Support/FormatCommon.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- FormatCommon.h - Formatters for common LLVM types --------*- C++ -*-===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #ifndef LLVM_SUPPORT_FORMATCOMMON_H |
10 | | #define LLVM_SUPPORT_FORMATCOMMON_H |
11 | | |
12 | | #include "llvm/ADT/SmallString.h" |
13 | | #include "llvm/Support/FormatVariadicDetails.h" |
14 | | #include "llvm/Support/raw_ostream.h" |
15 | | |
16 | | namespace llvm { |
17 | | enum class AlignStyle { Left, Center, Right }; |
18 | | |
19 | | struct FmtAlign { |
20 | | detail::format_adapter &Adapter; |
21 | | AlignStyle Where; |
22 | | size_t Amount; |
23 | | char Fill; |
24 | | |
25 | | FmtAlign(detail::format_adapter &Adapter, AlignStyle Where, size_t Amount, |
26 | | char Fill = ' ') |
27 | 0 | : Adapter(Adapter), Where(Where), Amount(Amount), Fill(Fill) {} |
28 | | |
29 | 0 | void format(raw_ostream &S, StringRef Options) { |
30 | 0 | // If we don't need to align, we can format straight into the underlying |
31 | 0 | // stream. Otherwise we have to go through an intermediate stream first |
32 | 0 | // in order to calculate how long the output is so we can align it. |
33 | 0 | // TODO: Make the format method return the number of bytes written, that |
34 | 0 | // way we can also skip the intermediate stream for left-aligned output. |
35 | 0 | if (Amount == 0) { |
36 | 0 | Adapter.format(S, Options); |
37 | 0 | return; |
38 | 0 | } |
39 | 0 | SmallString<64> Item; |
40 | 0 | raw_svector_ostream Stream(Item); |
41 | 0 |
|
42 | 0 | Adapter.format(Stream, Options); |
43 | 0 | if (Amount <= Item.size()) { |
44 | 0 | S << Item; |
45 | 0 | return; |
46 | 0 | } |
47 | 0 | |
48 | 0 | size_t PadAmount = Amount - Item.size(); |
49 | 0 | switch (Where) { |
50 | 0 | case AlignStyle::Left: |
51 | 0 | S << Item; |
52 | 0 | fill(S, PadAmount); |
53 | 0 | break; |
54 | 0 | case AlignStyle::Center: { |
55 | 0 | size_t X = PadAmount / 2; |
56 | 0 | fill(S, X); |
57 | 0 | S << Item; |
58 | 0 | fill(S, PadAmount - X); |
59 | 0 | break; |
60 | 0 | } |
61 | 0 | default: |
62 | 0 | fill(S, PadAmount); |
63 | 0 | S << Item; |
64 | 0 | break; |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | | private: |
69 | 0 | void fill(llvm::raw_ostream &S, uint32_t Count) { |
70 | 0 | for (uint32_t I = 0; I < Count; ++I) |
71 | 0 | S << Fill; |
72 | 0 | } |
73 | | }; |
74 | | } |
75 | | |
76 | | #endif |