llvm.org GIT mirror llvm / b256052
Split format() out to its own file, add support for formatting up to three values in one call. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@55251 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
2 changed file(s) with 156 addition(s) and 59 deletion(s). Raw diff Collapse all Expand all
0 //===--- Format.h - Efficient printf-style formatting for streams etc -----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the format() function, which can be used with other
10 // LLVM subsystems to provide printf-style formatting. This gives all the power
11 // and risk of printf. This can be used like this (with raw_ostreams as an
12 // example):
13 //
14 // OS << "mynumber: " << format("%4.5f", 1234.412) << '\n';
15 //
16 // Or if you prefer:
17 //
18 // OS << format("mynumber: %4.5f\n", 1234.412);
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_SUPPORT_FORMAT_H
23 #define LLVM_SUPPORT_FORMAT_H
24
25 #include
26 #ifdef WIN32
27 #define snprintf _snprintf
28 #endif
29
30 namespace llvm {
31
32 /// format_object_base - This is a helper class used for handling formatted
33 /// output. It is the abstract base class of a templated derived class.
34 class format_object_base {
35 protected:
36 const char *Fmt;
37 virtual void home(); // Out of line virtual method.
38 public:
39 format_object_base(const char *fmt) : Fmt(fmt) {}
40 virtual ~format_object_base() {}
41
42 /// print - Format the object into the specified buffer. On success, this
43 /// returns the length of the formatted string. If the buffer is too small,
44 /// this returns a length to retry with, which will be larger than BufferSize.
45 virtual unsigned print(char *Buffer, unsigned BufferSize) const = 0;
46 };
47
48 /// format_object1 - This is a templated helper class used by the format
49 /// function that captures the object to be formated and the format string. When
50 /// actually printed, this synthesizes the string into a temporary buffer
51 /// provided and returns whether or not it is big enough.
52 template
53 class format_object1 : public format_object_base {
54 T Val;
55 public:
56 format_object1(const char *fmt, const T &val)
57 : format_object_base(fmt), Val(val) {
58 }
59
60 /// print - Format the object into the specified buffer. On success, this
61 /// returns the length of the formatted string. If the buffer is too small,
62 /// this returns a length to retry with, which will be larger than BufferSize.
63 virtual unsigned print(char *Buffer, unsigned BufferSize) const {
64 int N = snprintf(Buffer, BufferSize-1, Fmt, Val);
65 if (N < 0) // VC++ and old GlibC return negative on overflow.
66 return BufferSize*2;
67 if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
68 return N+1;
69 // If N is positive and <= BufferSize-1, then the string fit, yay.
70 return N;
71 }
72 };
73
74 /// format_object2 - This is a templated helper class used by the format
75 /// function that captures the object to be formated and the format string. When
76 /// actually printed, this synthesizes the string into a temporary buffer
77 /// provided and returns whether or not it is big enough.
78 template
79 class format_object2 : public format_object_base {
80 T1 Val1;
81 T2 Val2;
82 public:
83 format_object2(const char *fmt, const T1 &val1, const T2 &val2)
84 : format_object_base(fmt), Val1(val1), Val2(val2) {
85 }
86
87 /// print - Format the object into the specified buffer. On success, this
88 /// returns the length of the formatted string. If the buffer is too small,
89 /// this returns a length to retry with, which will be larger than BufferSize.
90 virtual unsigned print(char *Buffer, unsigned BufferSize) const {
91 int N = snprintf(Buffer, BufferSize-1, Fmt, Val1, Val2);
92 if (N < 0) // VC++ and old GlibC return negative on overflow.
93 return BufferSize*2;
94 if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
95 return N+1;
96 // If N is positive and <= BufferSize-1, then the string fit, yay.
97 return N;
98 }
99 };
100
101 /// format_object3 - This is a templated helper class used by the format
102 /// function that captures the object to be formated and the format string. When
103 /// actually printed, this synthesizes the string into a temporary buffer
104 /// provided and returns whether or not it is big enough.
105 template
106 class format_object3 : public format_object_base {
107 T1 Val1;
108 T2 Val2;
109 T3 Val3;
110 public:
111 format_object3(const char *fmt, const T1 &val1, const T2 &val2,const T3 &val3)
112 : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3) {
113 }
114
115 /// print - Format the object into the specified buffer. On success, this
116 /// returns the length of the formatted string. If the buffer is too small,
117 /// this returns a length to retry with, which will be larger than BufferSize.
118 virtual unsigned print(char *Buffer, unsigned BufferSize) const {
119 int N = snprintf(Buffer, BufferSize-1, Fmt, Val1, Val2, Val3);
120 if (N < 0) // VC++ and old GlibC return negative on overflow.
121 return BufferSize*2;
122 if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
123 return N+1;
124 // If N is positive and <= BufferSize-1, then the string fit, yay.
125 return N;
126 }
127 };
128
129 /// format - This is a helper function that is used to produce formatted output.
130 /// This is typically used like: OS << format("%0.4f", myfloat) << '\n';
131 template
132 inline format_object1 format(const char *Fmt, const T &Val) {
133 return format_object1(Fmt, Val);
134 }
135
136 /// format - This is a helper function that is used to produce formatted output.
137 /// This is typically used like: OS << format("%0.4f", myfloat) << '\n';
138 template
139 inline format_object2 format(const char *Fmt, const T1 &Val1,
140 const T2 &Val2) {
141 return format_object2(Fmt, Val1, Val2);
142 }
143
144 /// format - This is a helper function that is used to produce formatted output.
145 /// This is typically used like: OS << format("%0.4f", myfloat) << '\n';
146 template
147 inline format_object3 format(const char *Fmt, const T1 &Val1,
148 const T2 &Val2, const T3 &Val3) {
149 return format_object3(Fmt, Val1, Val2, Val3);
150 }
151
152 } // end namespace llvm
153
154 #endif
1616 #include "llvm/ADT/StringExtras.h"
1717 #include
1818 #include
19 #include
2019 #include
2120 #include
2221
111110
112111 raw_ostream &write(const char *Ptr, unsigned Size);
113112
114 // Formatted output, see the format() function below.
113 // Formatted output, see the format() function in Support/Format.h.
115114 raw_ostream &operator<<(const format_object_base &Fmt);
116115
117116 //===--------------------------------------------------------------------===//
138137 };
139138
140139 //===----------------------------------------------------------------------===//
141 // Formatted Output
142 //===----------------------------------------------------------------------===//
143
144 /// format_object_base - This is a helper class used for handling formatted
145 /// output. It is the abstract base class of a templated derived class.
146 class format_object_base {
147 protected:
148 const char *Fmt;
149 virtual void home(); // Out of line virtual method.
150 public:
151 format_object_base(const char *fmt) : Fmt(fmt) {}
152 virtual ~format_object_base() {}
153
154 /// print - Format the object into the specified buffer. On success, this
155 /// returns the length of the formatted string. If the buffer is too small,
156 /// this returns a length to retry with, which will be larger than BufferSize.
157 virtual unsigned print(char *Buffer, unsigned BufferSize) const = 0;
158 };
159
160 /// format_object - This is a templated helper class used by the format function
161 /// that captures the object to be formated and the format string. When
162 /// actually printed, this synthesizes the string into a temporary buffer
163 /// provided and returns whether or not it is big enough.
164 template
165 class format_object : public format_object_base {
166 T Val;
167 public:
168 format_object(const char *fmt, const T &val)
169 : format_object_base(fmt), Val(val) {
170 }
171
172 /// print - Format the object into the specified buffer. On success, this
173 /// returns the length of the formatted string. If the buffer is too small,
174 /// this returns a length to retry with, which will be larger than BufferSize.
175 virtual unsigned print(char *Buffer, unsigned BufferSize) const {
176 #ifdef WIN32
177 int N = _snprintf(Buffer, BufferSize-1, Fmt, Val);
178 #else
179 int N = snprintf(Buffer, BufferSize-1, Fmt, Val);
180 #endif
181 if (N < 0) // VC++ and old GlibC return negative on overflow.
182 return BufferSize*2;
183 if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
184 return N+1;
185 // If N is positive and <= BufferSize-1, then the string fit, yay.
186 return N;
187 }
188 };
189
190 /// format - This is a helper function that is used to produce formatted output.
191 /// This is typically used like: OS << format("%0.4f", myfloat) << '\n';
192 template
193 inline format_object format(const char *Fmt, const T &Val) {
194 return format_object(Fmt, Val);
195 }
196
197 //===----------------------------------------------------------------------===//
198140 // File Output Streams
199141 //===----------------------------------------------------------------------===//
200142