1 | // |
2 | // NumberFormatter.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Core |
6 | // Module: NumberFormatter |
7 | // |
8 | // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/NumberFormatter.h" |
16 | #include "Poco/MemoryStream.h" |
17 | #include <iomanip> |
18 | #if !defined(POCO_NO_LOCALE) |
19 | #include <locale> |
20 | #endif |
21 | #include <cstdio> |
22 | |
23 | |
24 | #if defined(_MSC_VER) || defined(__MINGW32__) |
25 | #define I64_FMT "I64" |
26 | #elif defined(__APPLE__) |
27 | #define I64_FMT "q" |
28 | #else |
29 | #define I64_FMT "ll" |
30 | #endif |
31 | |
32 | |
33 | namespace Poco { |
34 | |
35 | |
36 | std::string NumberFormatter::format(bool value, BoolFormat format) |
37 | { |
38 | switch(format) |
39 | { |
40 | default: |
41 | case FMT_TRUE_FALSE: |
42 | if (value == true) |
43 | return "true" ; |
44 | return "false" ; |
45 | case FMT_YES_NO: |
46 | if (value == true) |
47 | return "yes" ; |
48 | return "no" ; |
49 | case FMT_ON_OFF: |
50 | if (value == true) |
51 | return "on" ; |
52 | return "off" ; |
53 | } |
54 | } |
55 | |
56 | |
57 | void NumberFormatter::append(std::string& str, int value) |
58 | { |
59 | char result[NF_MAX_INT_STRING_LEN]; |
60 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
61 | intToStr(value, 10, result, sz); |
62 | str.append(result, sz); |
63 | } |
64 | |
65 | |
66 | void NumberFormatter::append(std::string& str, int value, int width) |
67 | { |
68 | char result[NF_MAX_INT_STRING_LEN]; |
69 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
70 | intToStr(value, 10, result, sz, false, width); |
71 | str.append(result, sz); |
72 | } |
73 | |
74 | |
75 | void NumberFormatter::append0(std::string& str, int value, int width) |
76 | { |
77 | char result[NF_MAX_INT_STRING_LEN]; |
78 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
79 | intToStr(value, 10, result, sz, false, width, '0'); |
80 | str.append(result, sz); |
81 | } |
82 | |
83 | |
84 | void NumberFormatter::appendHex(std::string& str, int value) |
85 | { |
86 | char result[NF_MAX_INT_STRING_LEN]; |
87 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
88 | uIntToStr(static_cast<unsigned int>(value), 0x10, result, sz); |
89 | str.append(result, sz); |
90 | } |
91 | |
92 | |
93 | void NumberFormatter::appendHex(std::string& str, int value, int width) |
94 | { |
95 | char result[NF_MAX_INT_STRING_LEN]; |
96 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
97 | uIntToStr(static_cast<unsigned int>(value), 0x10, result, sz, false, width, '0'); |
98 | str.append(result, sz); |
99 | } |
100 | |
101 | |
102 | void NumberFormatter::append(std::string& str, unsigned value) |
103 | { |
104 | char result[NF_MAX_INT_STRING_LEN]; |
105 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
106 | uIntToStr(value, 10, result, sz); |
107 | str.append(result, sz); |
108 | } |
109 | |
110 | |
111 | void NumberFormatter::append(std::string& str, unsigned value, int width) |
112 | { |
113 | char result[NF_MAX_INT_STRING_LEN]; |
114 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
115 | uIntToStr(value, 10, result, sz, false, width); |
116 | str.append(result, sz); |
117 | } |
118 | |
119 | |
120 | void NumberFormatter::append0(std::string& str, unsigned int value, int width) |
121 | { |
122 | char result[NF_MAX_INT_STRING_LEN]; |
123 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
124 | uIntToStr(value, 10, result, sz, false, width, '0'); |
125 | str.append(result, sz); |
126 | } |
127 | |
128 | |
129 | void NumberFormatter::appendHex(std::string& str, unsigned value) |
130 | { |
131 | char result[NF_MAX_INT_STRING_LEN]; |
132 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
133 | uIntToStr(value, 0x10, result, sz); |
134 | str.append(result, sz); |
135 | } |
136 | |
137 | |
138 | void NumberFormatter::appendHex(std::string& str, unsigned value, int width) |
139 | { |
140 | char result[NF_MAX_INT_STRING_LEN]; |
141 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
142 | uIntToStr(value, 0x10, result, sz, false, width, '0'); |
143 | str.append(result, sz); |
144 | } |
145 | |
146 | |
147 | #ifndef POCO_LONG_IS_64_BIT |
148 | |
149 | |
150 | void NumberFormatter::append(std::string& str, long value) |
151 | { |
152 | char result[NF_MAX_INT_STRING_LEN]; |
153 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
154 | intToStr(value, 10, result, sz); |
155 | str.append(result, sz); |
156 | } |
157 | |
158 | |
159 | void NumberFormatter::append(std::string& str, long value, int width) |
160 | { |
161 | char result[NF_MAX_INT_STRING_LEN]; |
162 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
163 | intToStr(value, 10, result, sz, false, width); |
164 | str.append(result, sz); |
165 | } |
166 | |
167 | |
168 | void NumberFormatter::append0(std::string& str, long value, int width) |
169 | { |
170 | char result[NF_MAX_INT_STRING_LEN]; |
171 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
172 | intToStr(value, 10, result, sz, false, width, '0'); |
173 | str.append(result, sz); |
174 | } |
175 | |
176 | |
177 | void NumberFormatter::appendHex(std::string& str, long value) |
178 | { |
179 | char result[NF_MAX_INT_STRING_LEN]; |
180 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
181 | uIntToStr(static_cast<unsigned long>(value), 0x10, result, sz); |
182 | str.append(result, sz); |
183 | } |
184 | |
185 | |
186 | void NumberFormatter::appendHex(std::string& str, long value, int width) |
187 | { |
188 | char result[NF_MAX_INT_STRING_LEN]; |
189 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
190 | uIntToStr(static_cast<unsigned long>(value), 0x10, result, sz, false, width, '0'); |
191 | str.append(result, sz); |
192 | } |
193 | |
194 | |
195 | void NumberFormatter::append(std::string& str, unsigned long value) |
196 | { |
197 | char result[NF_MAX_INT_STRING_LEN]; |
198 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
199 | uIntToStr(value, 10, result, sz); |
200 | str.append(result, sz); |
201 | } |
202 | |
203 | |
204 | void NumberFormatter::append(std::string& str, unsigned long value, int width) |
205 | { |
206 | char result[NF_MAX_INT_STRING_LEN]; |
207 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
208 | uIntToStr(value, 10, result, sz, false, width, '0'); |
209 | str.append(result, sz); |
210 | } |
211 | |
212 | |
213 | void NumberFormatter::append0(std::string& str, unsigned long value, int width) |
214 | { |
215 | char result[NF_MAX_INT_STRING_LEN]; |
216 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
217 | uIntToStr(value, 10, result, sz, false, width, '0'); |
218 | str.append(result, sz); |
219 | } |
220 | |
221 | |
222 | void NumberFormatter::appendHex(std::string& str, unsigned long value) |
223 | { |
224 | char result[NF_MAX_INT_STRING_LEN]; |
225 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
226 | uIntToStr(value, 0x10, result, sz); |
227 | str.append(result, sz); |
228 | } |
229 | |
230 | |
231 | void NumberFormatter::appendHex(std::string& str, unsigned long value, int width) |
232 | { |
233 | char result[NF_MAX_INT_STRING_LEN]; |
234 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
235 | uIntToStr(value, 0x10, result, sz, false, width, '0'); |
236 | str.append(result, sz); |
237 | } |
238 | |
239 | |
240 | #endif // POCO_LONG_IS_64_BIT |
241 | |
242 | |
243 | void NumberFormatter::append(std::string& str, Int64 value) |
244 | { |
245 | char result[NF_MAX_INT_STRING_LEN]; |
246 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
247 | intToStr(value, 10, result, sz); |
248 | str.append(result, sz); |
249 | } |
250 | |
251 | |
252 | void NumberFormatter::append(std::string& str, Int64 value, int width) |
253 | { |
254 | char result[NF_MAX_INT_STRING_LEN]; |
255 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
256 | intToStr(value, 10, result, sz, false, width, '0'); |
257 | str.append(result, sz); |
258 | } |
259 | |
260 | |
261 | void NumberFormatter::append0(std::string& str, Int64 value, int width) |
262 | { |
263 | char result[NF_MAX_INT_STRING_LEN]; |
264 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
265 | intToStr(value, 10, result, sz, false, width, '0'); |
266 | str.append(result, sz); |
267 | } |
268 | |
269 | |
270 | void NumberFormatter::appendHex(std::string& str, Int64 value) |
271 | { |
272 | char result[NF_MAX_INT_STRING_LEN]; |
273 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
274 | uIntToStr(static_cast<UInt64>(value), 0x10, result, sz); |
275 | str.append(result, sz); |
276 | } |
277 | |
278 | |
279 | void NumberFormatter::appendHex(std::string& str, Int64 value, int width) |
280 | { |
281 | char result[NF_MAX_INT_STRING_LEN]; |
282 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
283 | uIntToStr(static_cast<UInt64>(value), 0x10, result, sz, false, width, '0'); |
284 | str.append(result, sz); |
285 | } |
286 | |
287 | |
288 | void NumberFormatter::append(std::string& str, UInt64 value) |
289 | { |
290 | char result[NF_MAX_INT_STRING_LEN]; |
291 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
292 | uIntToStr(value, 10, result, sz); |
293 | str.append(result, sz); |
294 | } |
295 | |
296 | |
297 | void NumberFormatter::append(std::string& str, UInt64 value, int width) |
298 | { |
299 | char result[NF_MAX_INT_STRING_LEN]; |
300 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
301 | uIntToStr(value, 10, result, sz, false, width, '0'); |
302 | str.append(result, sz); |
303 | } |
304 | |
305 | |
306 | void NumberFormatter::append0(std::string& str, UInt64 value, int width) |
307 | { |
308 | char result[NF_MAX_INT_STRING_LEN]; |
309 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
310 | uIntToStr(value, 10, result, sz, false, width, '0'); |
311 | str.append(result, sz); |
312 | } |
313 | |
314 | |
315 | void NumberFormatter::appendHex(std::string& str, UInt64 value) |
316 | { |
317 | char result[NF_MAX_INT_STRING_LEN]; |
318 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
319 | uIntToStr(value, 0x10, result, sz); |
320 | str.append(result, sz); |
321 | } |
322 | |
323 | |
324 | void NumberFormatter::appendHex(std::string& str, UInt64 value, int width) |
325 | { |
326 | char result[NF_MAX_INT_STRING_LEN]; |
327 | std::size_t sz = NF_MAX_INT_STRING_LEN; |
328 | uIntToStr(value, 0x10, result, sz, false, width, '0'); |
329 | str.append(result, sz); |
330 | } |
331 | |
332 | |
333 | void NumberFormatter::append(std::string& str, float value) |
334 | { |
335 | char buffer[NF_MAX_FLT_STRING_LEN]; |
336 | floatToStr(buffer, POCO_MAX_FLT_STRING_LEN, value); |
337 | str.append(buffer); |
338 | } |
339 | |
340 | |
341 | void NumberFormatter::append(std::string& str, float value, int precision) |
342 | { |
343 | char buffer[NF_MAX_FLT_STRING_LEN]; |
344 | floatToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision); |
345 | str.append(buffer); |
346 | } |
347 | |
348 | |
349 | void NumberFormatter::append(std::string& str, float value, int width, int precision) |
350 | { |
351 | std::string result; |
352 | str.append(floatToFixedStr(result, value, precision, width)); |
353 | } |
354 | |
355 | |
356 | void NumberFormatter::append(std::string& str, double value) |
357 | { |
358 | char buffer[NF_MAX_FLT_STRING_LEN]; |
359 | doubleToStr(buffer, POCO_MAX_FLT_STRING_LEN, value); |
360 | str.append(buffer); |
361 | } |
362 | |
363 | |
364 | void NumberFormatter::append(std::string& str, double value, int precision) |
365 | { |
366 | char buffer[NF_MAX_FLT_STRING_LEN]; |
367 | doubleToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision); |
368 | str.append(buffer); |
369 | } |
370 | |
371 | |
372 | void NumberFormatter::append(std::string& str, double value, int width, int precision) |
373 | { |
374 | std::string result; |
375 | str.append(doubleToFixedStr(result, value, precision, width)); |
376 | } |
377 | |
378 | |
379 | void NumberFormatter::append(std::string& str, const void* ptr) |
380 | { |
381 | char buffer[24]; |
382 | #if defined(POCO_PTR_IS_64_BIT) |
383 | #if defined(POCO_LONG_IS_64_BIT) |
384 | std::sprintf(buffer, "%016lX" , (long) ptr); |
385 | #else |
386 | std::sprintf(buffer, "%016" I64_FMT "X" , (UIntPtr) ptr); |
387 | #endif |
388 | #else |
389 | std::sprintf(buffer, "%08lX" , (UIntPtr) ptr); |
390 | #endif |
391 | str.append(buffer); |
392 | } |
393 | |
394 | |
395 | } // namespace Poco |
396 | |