1 //===- Format.h - Efficient printf-style formatting for streams -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the format() function, which can be used with other
11 // LLVM subsystems to provide printf-style formatting. This gives all the power
12 // and risk of printf. This can be used like this (with raw_ostreams as an
15 // OS << "mynumber: " << format("%4.5f", 1234.412) << '\n';
19 // OS << format("mynumber: %4.5f\n", 1234.412);
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_SUPPORT_FORMAT_H
24 #define LLVM_SUPPORT_FORMAT_H
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/Support/DataTypes.h"
35 /// This is a helper class used for handling formatted output. It is the
36 /// abstract base class of a templated derived class.
37 class format_object_base {
40 ~format_object_base() {} // Disallow polymorphic deletion.
41 virtual void home(); // Out of line virtual method.
43 /// Call snprintf() for this object, on the given buffer and size.
44 virtual int snprint(char *Buffer, unsigned BufferSize) const = 0;
47 format_object_base(const char *fmt) : Fmt(fmt) {}
49 /// Format the object into the specified buffer. On success, this returns
50 /// the length of the formatted string. If the buffer is too small, this
51 /// returns a length to retry with, which will be larger than BufferSize.
52 unsigned print(char *Buffer, unsigned BufferSize) const {
53 assert(BufferSize && "Invalid buffer size!");
55 // Print the string, leaving room for the terminating null.
56 int N = snprint(Buffer, BufferSize);
58 // VC++ and old GlibC return negative on overflow, just double the size.
60 return BufferSize * 2;
62 // Other implementations yield number of bytes needed, not including the
64 if (unsigned(N) >= BufferSize)
67 // Otherwise N is the length of output (not including the final '\0').
72 /// These are templated helper classes used by the format function that
73 /// capture the object to be formated and the format string. When actually
74 /// printed, this synthesizes the string into a temporary buffer provided and
75 /// returns whether or not it is big enough.
77 template <typename... Ts>
78 class format_object final : public format_object_base {
79 std::tuple<Ts...> Vals;
81 template <std::size_t... Is>
82 int snprint_tuple(char *Buffer, unsigned BufferSize,
83 index_sequence<Is...>) const {
85 return _snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
87 return snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
92 format_object(const char *fmt, const Ts &... vals)
93 : format_object_base(fmt), Vals(vals...) {}
95 int snprint(char *Buffer, unsigned BufferSize) const override {
96 return snprint_tuple(Buffer, BufferSize, index_sequence_for<Ts...>());
100 /// These are helper functions used to produce formatted output. They use
101 /// template type deduction to construct the appropriate instance of the
102 /// format_object class to simplify their construction.
104 /// This is typically used like:
106 /// OS << format("%0.4f", myfloat) << '\n';
109 template <typename... Ts>
110 inline format_object<Ts...> format(const char *Fmt, const Ts &... Vals) {
111 return format_object<Ts...>(Fmt, Vals...);
114 /// This is a helper class used for left_justify() and right_justify().
115 class FormattedString {
119 friend class raw_ostream;
121 FormattedString(StringRef S, unsigned W, bool R)
122 : Str(S), Width(W), RightJustify(R) { }
125 /// left_justify - append spaces after string so total output is
126 /// \p Width characters. If \p Str is larger that \p Width, full string
127 /// is written with no padding.
128 inline FormattedString left_justify(StringRef Str, unsigned Width) {
129 return FormattedString(Str, Width, false);
132 /// right_justify - add spaces before string so total output is
133 /// \p Width characters. If \p Str is larger that \p Width, full string
134 /// is written with no padding.
135 inline FormattedString right_justify(StringRef Str, unsigned Width) {
136 return FormattedString(Str, Width, true);
139 /// This is a helper class used for format_hex() and format_decimal().
140 class FormattedNumber {
147 friend class raw_ostream;
149 FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U,
151 : HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U),
155 /// format_hex - Output \p N as a fixed width hexadecimal. If number will not
156 /// fit in width, full number is still printed. Examples:
157 /// OS << format_hex(255, 4) => 0xff
158 /// OS << format_hex(255, 4, true) => 0xFF
159 /// OS << format_hex(255, 6) => 0x00ff
160 /// OS << format_hex(255, 2) => 0xff
161 inline FormattedNumber format_hex(uint64_t N, unsigned Width,
162 bool Upper = false) {
163 assert(Width <= 18 && "hex width must be <= 18");
164 return FormattedNumber(N, 0, Width, true, Upper, true);
167 /// format_hex_no_prefix - Output \p N as a fixed width hexadecimal. Does not
168 /// prepend '0x' to the outputted string. If number will not fit in width,
169 /// full number is still printed. Examples:
170 /// OS << format_hex_no_prefix(255, 4) => ff
171 /// OS << format_hex_no_prefix(255, 4, true) => FF
172 /// OS << format_hex_no_prefix(255, 6) => 00ff
173 /// OS << format_hex_no_prefix(255, 2) => ff
174 inline FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width,
175 bool Upper = false) {
176 assert(Width <= 18 && "hex width must be <= 18");
177 return FormattedNumber(N, 0, Width, true, Upper, false);
180 /// format_decimal - Output \p N as a right justified, fixed-width decimal. If
181 /// number will not fit in width, full number is still printed. Examples:
182 /// OS << format_decimal(0, 5) => " 0"
183 /// OS << format_decimal(255, 5) => " 255"
184 /// OS << format_decimal(-1, 3) => " -1"
185 /// OS << format_decimal(12345, 3) => "12345"
186 inline FormattedNumber format_decimal(int64_t N, unsigned Width) {
187 return FormattedNumber(0, N, Width, false, false, false);
191 } // end namespace llvm