WPILibC++  unspecified
Format.h
1 //===- Format.h - Efficient printf-style formatting for streams -*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
13 // example):
14 //
15 // OS << "mynumber: " << format("%4.5f", 1234.412) << '\n';
16 //
17 // Or if you prefer:
18 //
19 // OS << format("mynumber: %4.5f\n", 1234.412);
20 //
21 //===----------------------------------------------------------------------===//
22 
23 #ifndef LLVM_SUPPORT_FORMAT_H
24 #define LLVM_SUPPORT_FORMAT_H
25 
26 #include "llvm/STLExtras.h"
27 #include "llvm/StringRef.h"
28 #include <cassert>
29 #include <cstdint>
30 #include <cstdio>
31 #include <tuple>
32 
33 namespace llvm {
34 
38 protected:
39  const char *Fmt;
40  ~format_object_base() = default; // Disallow polymorphic deletion.
41  format_object_base(const format_object_base &) = default;
42  virtual void home(); // Out of line virtual method.
43 
45  virtual int snprint(char *Buffer, unsigned BufferSize) const = 0;
46 
47 public:
48  format_object_base(const char *fmt) : Fmt(fmt) {}
49 
53  unsigned print(char *Buffer, unsigned BufferSize) const {
54  assert(BufferSize && "Invalid buffer size!");
55 
56  // Print the string, leaving room for the terminating null.
57  int N = snprint(Buffer, BufferSize);
58 
59  // VC++ and old GlibC return negative on overflow, just double the size.
60  if (N < 0)
61  return BufferSize * 2;
62 
63  // Other implementations yield number of bytes needed, not including the
64  // final '\0'.
65  if (unsigned(N) >= BufferSize)
66  return N + 1;
67 
68  // Otherwise N is the length of output (not including the final '\0').
69  return N;
70  }
71 };
72 
77 
78 template <typename... Ts>
79 class format_object final : public format_object_base {
80  std::tuple<Ts...> Vals;
81 
82  template <std::size_t... Is>
83  int snprint_tuple(char *Buffer, unsigned BufferSize,
84  index_sequence<Is...>) const {
85 #ifdef _MSC_VER
86  return _snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
87 #else
88 #ifdef __GNUC__
89 #pragma GCC diagnostic push
90 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
91 #endif
92  return snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
93 #ifdef __GNUC__
94 #pragma GCC diagnostic pop
95 #endif
96 #endif
97  }
98 
99 public:
100  format_object(const char *fmt, const Ts &... vals)
101  : format_object_base(fmt), Vals(vals...) {}
102 
103  int snprint(char *Buffer, unsigned BufferSize) const override {
104  return snprint_tuple(Buffer, BufferSize, index_sequence_for<Ts...>());
105  }
106 };
107 
116 
117 template <typename... Ts>
118 inline format_object<Ts...> format(const char *Fmt, const Ts &... Vals) {
119  return format_object<Ts...>(Fmt, Vals...);
120 }
121 
124  StringRef Str;
125  unsigned Width;
126  bool RightJustify;
127  friend class raw_ostream;
128 
129 public:
130  FormattedString(StringRef S, unsigned W, bool R)
131  : Str(S), Width(W), RightJustify(R) { }
132 };
133 
137 inline FormattedString left_justify(StringRef Str, unsigned Width) {
138  return FormattedString(Str, Width, false);
139 }
140 
144 inline FormattedString right_justify(StringRef Str, unsigned Width) {
145  return FormattedString(Str, Width, true);
146 }
147 
150  uint64_t HexValue;
151  int64_t DecValue;
152  unsigned Width;
153  bool Hex;
154  bool Upper;
155  bool HexPrefix;
156  friend class raw_ostream;
157 
158 public:
159  FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U,
160  bool Prefix)
161  : HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U),
162  HexPrefix(Prefix) {}
163 };
164 
171 inline FormattedNumber format_hex(uint64_t N, unsigned Width,
172  bool Upper = false) {
173  assert(Width <= 18 && "hex width must be <= 18");
174  return FormattedNumber(N, 0, Width, true, Upper, true);
175 }
176 
184 inline FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width,
185  bool Upper = false) {
186  assert(Width <= 16 && "hex width must be <= 16");
187  return FormattedNumber(N, 0, Width, true, Upper, false);
188 }
189 
196 inline FormattedNumber format_decimal(int64_t N, unsigned Width) {
197  return FormattedNumber(0, N, Width, false, false, false);
198 }
199 
200 } // end namespace llvm
201 
202 #endif
Definition: Path.inc:27
virtual int snprint(char *Buffer, unsigned BufferSize) const =0
Call snprintf() for this object, on the given buffer and size.
This is a helper class used for left_justify() and right_justify().
Definition: Format.h:123
Alias for the common case of a sequence of size_ts.
Definition: STLExtras.h:269
This is a helper class used for handling formatted output.
Definition: Format.h:37
unsigned print(char *Buffer, unsigned BufferSize) const
Format the object into the specified buffer.
Definition: Format.h:53
This is a helper class used for format_hex() and format_decimal().
Definition: Format.h:149
Creates a compile-time integer sequence for a parameter pack.
Definition: STLExtras.h:278
These are templated helper classes used by the format function that capture the object to be formated...
Definition: Format.h:79
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:33
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
int snprint(char *Buffer, unsigned BufferSize) const override
Call snprintf() for this object, on the given buffer and size.
Definition: Format.h:103