/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Support/FormatCommon.h
Line | Count | Source |
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 | 157k | : Adapter(Adapter), Where(Where), Amount(Amount), Fill(Fill) {} |
28 | | |
29 | 157k | void format(raw_ostream &S, StringRef Options) { |
30 | 157k | // If we don't need to align, we can format straight into the underlying |
31 | 157k | // stream. Otherwise we have to go through an intermediate stream first |
32 | 157k | // in order to calculate how long the output is so we can align it. |
33 | 157k | // TODO: Make the format method return the number of bytes written, that |
34 | 157k | // way we can also skip the intermediate stream for left-aligned output. |
35 | 157k | if (Amount == 0) { |
36 | 150k | Adapter.format(S, Options); |
37 | 150k | return; |
38 | 150k | } |
39 | 7.24k | SmallString<64> Item; |
40 | 7.24k | raw_svector_ostream Stream(Item); |
41 | 7.24k | |
42 | 7.24k | Adapter.format(Stream, Options); |
43 | 7.24k | if (Amount <= Item.size()) { |
44 | 4.90k | S << Item; |
45 | 4.90k | return; |
46 | 4.90k | } |
47 | 2.34k | |
48 | 2.34k | size_t PadAmount = Amount - Item.size(); |
49 | 2.34k | switch (Where) { |
50 | 2.34k | case AlignStyle::Left: |
51 | 221 | S << Item; |
52 | 221 | fill(S, PadAmount); |
53 | 221 | break; |
54 | 2.34k | case AlignStyle::Center: { |
55 | 246 | size_t X = PadAmount / 2; |
56 | 246 | fill(S, X); |
57 | 246 | S << Item; |
58 | 246 | fill(S, PadAmount - X); |
59 | 246 | break; |
60 | 2.34k | } |
61 | 2.34k | default: |
62 | 1.87k | fill(S, PadAmount); |
63 | 1.87k | S << Item; |
64 | 1.87k | break; |
65 | 2.34k | } |
66 | 2.34k | } |
67 | | |
68 | | private: |
69 | 2.58k | void fill(llvm::raw_ostream &S, uint32_t Count) { |
70 | 30.3k | for (uint32_t I = 0; I < Count; ++I27.7k ) |
71 | 27.7k | S << Fill; |
72 | 2.58k | } |
73 | | }; |
74 | | } |
75 | | |
76 | | #endif |