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
28 #define snprintf _snprintf
33 /// format_object_base - This is a helper class used for handling formatted
34 /// output. It is the abstract base class of a templated derived class.
35 class format_object_base {
38 virtual void home(); // Out of line virtual method.
40 format_object_base(const char *fmt) : Fmt(fmt) {}
41 virtual ~format_object_base() {}
43 /// print - Format the object into the specified buffer. On success, this
44 /// returns the length of the formatted string. If the buffer is too small,
45 /// this returns a length to retry with, which will be larger than BufferSize.
46 virtual unsigned print(char *Buffer, unsigned BufferSize) const = 0;
49 /// format_object1 - This is a templated helper class used by the format
50 /// function that captures the object to be formated and the format string. When
51 /// actually printed, this synthesizes the string into a temporary buffer
52 /// provided and returns whether or not it is big enough.
54 class format_object1 : public format_object_base {
57 format_object1(const char *fmt, const T &val)
58 : format_object_base(fmt), Val(val) {
61 /// print - Format the object into the specified buffer. On success, this
62 /// returns the length of the formatted string. If the buffer is too small,
63 /// this returns a length to retry with, which will be larger than BufferSize.
64 virtual unsigned print(char *Buffer, unsigned BufferSize) const {
65 int N = snprintf(Buffer, BufferSize-1, Fmt, Val);
66 if (N < 0) // VC++ and old GlibC return negative on overflow.
68 if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
70 // If N is positive and <= BufferSize-1, then the string fit, yay.
75 /// format_object2 - This is a templated helper class used by the format
76 /// function that captures the object to be formated and the format string. When
77 /// actually printed, this synthesizes the string into a temporary buffer
78 /// provided and returns whether or not it is big enough.
79 template <typename T1, typename T2>
80 class format_object2 : public format_object_base {
84 format_object2(const char *fmt, const T1 &val1, const T2 &val2)
85 : format_object_base(fmt), Val1(val1), Val2(val2) {
88 /// print - Format the object into the specified buffer. On success, this
89 /// returns the length of the formatted string. If the buffer is too small,
90 /// this returns a length to retry with, which will be larger than BufferSize.
91 virtual unsigned print(char *Buffer, unsigned BufferSize) const {
92 int N = snprintf(Buffer, BufferSize-1, Fmt, Val1, Val2);
93 if (N < 0) // VC++ and old GlibC return negative on overflow.
95 if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
97 // If N is positive and <= BufferSize-1, then the string fit, yay.
102 /// format_object3 - This is a templated helper class used by the format
103 /// function that captures the object to be formated and the format string. When
104 /// actually printed, this synthesizes the string into a temporary buffer
105 /// provided and returns whether or not it is big enough.
106 template <typename T1, typename T2, typename T3>
107 class format_object3 : public format_object_base {
112 format_object3(const char *fmt, const T1 &val1, const T2 &val2,const T3 &val3)
113 : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3) {
116 /// print - Format the object into the specified buffer. On success, this
117 /// returns the length of the formatted string. If the buffer is too small,
118 /// this returns a length to retry with, which will be larger than BufferSize.
119 virtual unsigned print(char *Buffer, unsigned BufferSize) const {
120 int N = snprintf(Buffer, BufferSize-1, Fmt, Val1, Val2, Val3);
121 if (N < 0) // VC++ and old GlibC return negative on overflow.
123 if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
125 // If N is positive and <= BufferSize-1, then the string fit, yay.
130 /// format - This is a helper function that is used to produce formatted output.
131 /// This is typically used like: OS << format("%0.4f", myfloat) << '\n';
132 template <typename T>
133 inline format_object1<T> format(const char *Fmt, const T &Val) {
134 return format_object1<T>(Fmt, Val);
137 /// format - This is a helper function that is used to produce formatted output.
138 /// This is typically used like: OS << format("%0.4f", myfloat) << '\n';
139 template <typename T1, typename T2>
140 inline format_object2<T1, T2> format(const char *Fmt, const T1 &Val1,
142 return format_object2<T1, T2>(Fmt, Val1, Val2);
145 /// format - This is a helper function that is used to produce formatted output.
146 /// This is typically used like: OS << format("%0.4f", myfloat) << '\n';
147 template <typename T1, typename T2, typename T3>
148 inline format_object3<T1, T2, T3> format(const char *Fmt, const T1 &Val1,
149 const T2 &Val2, const T3 &Val3) {
150 return format_object3<T1, T2, T3>(Fmt, Val1, Val2, Val3);
153 } // end namespace llvm