/src/poco/Foundation/include/Poco/NumberFormatter.h
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // NumberFormatter.h |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: Core |
6 | | // Module: NumberFormatter |
7 | | // |
8 | | // Definition of the NumberFormatter class. |
9 | | // |
10 | | // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef Foundation_NumberFormatter_INCLUDED |
18 | | #define Foundation_NumberFormatter_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/Foundation.h" |
22 | | #include "Poco/NumericString.h" |
23 | | |
24 | | |
25 | | namespace Poco { |
26 | | |
27 | | |
28 | | class Foundation_API NumberFormatter |
29 | | /// The NumberFormatter class provides static methods |
30 | | /// for formatting numeric values into strings. |
31 | | /// |
32 | | /// There are two kind of static member functions: |
33 | | /// * format* functions return a std::string containing |
34 | | /// the formatted value. |
35 | | /// * append* functions append the formatted value to |
36 | | /// an existing string. |
37 | | { |
38 | | public: |
39 | | enum BoolFormat |
40 | | { |
41 | | FMT_TRUE_FALSE, |
42 | | FMT_YES_NO, |
43 | | FMT_ON_OFF |
44 | | }; |
45 | | |
46 | | static const unsigned NF_MAX_INT_STRING_LEN = 32; // increase for 64-bit binary formatting support |
47 | | static const unsigned NF_MAX_FLT_STRING_LEN = POCO_MAX_FLT_STRING_LEN; |
48 | | |
49 | | static std::string format(int value); |
50 | | /// Formats an integer value in decimal notation. |
51 | | |
52 | | static std::string format(int value, int width); |
53 | | /// Formats an integer value in decimal notation, |
54 | | /// right justified in a field having at least |
55 | | /// the specified width. |
56 | | |
57 | | static std::string format0(int value, int width); |
58 | | /// Formats an integer value in decimal notation, |
59 | | /// right justified and zero-padded in a field |
60 | | /// having at least the specified width. |
61 | | |
62 | | static std::string formatHex(int value, bool prefix = false); |
63 | | /// Formats an int value in hexadecimal notation. |
64 | | /// If prefix is true, "0x" prefix is prepended to the |
65 | | /// resulting string. |
66 | | /// The value is treated as unsigned. |
67 | | |
68 | | static std::string formatHex(int value, int width, bool prefix = false); |
69 | | /// Formats a int value in hexadecimal notation, |
70 | | /// right justified and zero-padded in |
71 | | /// a field having at least the specified width. |
72 | | /// If prefix is true, "0x" prefix is prepended to the |
73 | | /// resulting string. |
74 | | /// The value is treated as unsigned. |
75 | | |
76 | | static std::string format(unsigned value); |
77 | | /// Formats an unsigned int value in decimal notation. |
78 | | |
79 | | static std::string format(unsigned value, int width); |
80 | | /// Formats an unsigned long int in decimal notation, |
81 | | /// right justified in a field having at least the |
82 | | /// specified width. |
83 | | |
84 | | static std::string format0(unsigned int value, int width); |
85 | | /// Formats an unsigned int value in decimal notation, |
86 | | /// right justified and zero-padded in a field having at |
87 | | /// least the specified width. |
88 | | |
89 | | static std::string formatHex(unsigned value, bool prefix = false); |
90 | | /// Formats an unsigned int value in hexadecimal notation. |
91 | | /// If prefix is true, "0x" prefix is prepended to the |
92 | | /// resulting string. |
93 | | |
94 | | static std::string formatHex(unsigned value, int width, bool prefix = false); |
95 | | /// Formats a int value in hexadecimal notation, |
96 | | /// right justified and zero-padded in |
97 | | /// a field having at least the specified width. |
98 | | /// If prefix is true, "0x" prefix is prepended to the |
99 | | /// resulting string. |
100 | | |
101 | | static std::string format(long value); |
102 | | /// Formats a long value in decimal notation. |
103 | | |
104 | | static std::string format(long value, int width); |
105 | | /// Formats a long value in decimal notation, |
106 | | /// right justified in a field having at least the |
107 | | /// specified width. |
108 | | |
109 | | static std::string format0(long value, int width); |
110 | | /// Formats a long value in decimal notation, |
111 | | /// right justified and zero-padded in a field |
112 | | /// having at least the specified width. |
113 | | |
114 | | static std::string formatHex(long value, bool prefix = false); |
115 | | /// Formats an unsigned long value in hexadecimal notation. |
116 | | /// If prefix is true, "0x" prefix is prepended to the |
117 | | /// resulting string. |
118 | | /// The value is treated as unsigned. |
119 | | |
120 | | static std::string formatHex(long value, int width, bool prefix = false); |
121 | | /// Formats an unsigned long value in hexadecimal notation, |
122 | | /// right justified and zero-padded in a field having at least the |
123 | | /// specified width. |
124 | | /// If prefix is true, "0x" prefix is prepended to the |
125 | | /// resulting string. |
126 | | /// The value is treated as unsigned. |
127 | | |
128 | | static std::string format(unsigned long value); |
129 | | /// Formats an unsigned long value in decimal notation. |
130 | | |
131 | | static std::string format(unsigned long value, int width); |
132 | | /// Formats an unsigned long value in decimal notation, |
133 | | /// right justified in a field having at least the specified |
134 | | /// width. |
135 | | |
136 | | static std::string format0(unsigned long value, int width); |
137 | | /// Formats an unsigned long value in decimal notation, |
138 | | /// right justified and zero-padded |
139 | | /// in a field having at least the specified width. |
140 | | |
141 | | static std::string formatHex(unsigned long value, bool prefix = false); |
142 | | /// Formats an unsigned long value in hexadecimal notation. |
143 | | /// If prefix is true, "0x" prefix is prepended to the |
144 | | /// resulting string. |
145 | | |
146 | | static std::string formatHex(unsigned long value, int width, bool prefix = false); |
147 | | /// Formats an unsigned long value in hexadecimal notation, |
148 | | /// right justified and zero-padded in a field having at least the |
149 | | /// specified width. |
150 | | /// If prefix is true, "0x" prefix is prepended to the |
151 | | /// resulting string. |
152 | | |
153 | | #ifdef POCO_HAVE_INT64 |
154 | | #ifdef POCO_INT64_IS_LONG |
155 | | |
156 | | static std::string format(long long value); |
157 | | /// Formats a 64-bit integer value in decimal notation. |
158 | | |
159 | | static std::string format(long long value, int width); |
160 | | /// Formats a 64-bit integer value in decimal notation, |
161 | | /// right justified in a field having at least the specified width. |
162 | | |
163 | | static std::string format0(long long value, int width); |
164 | | /// Formats a 64-bit integer value in decimal notation, |
165 | | /// right justified and zero-padded in a field having at least |
166 | | /// the specified width. |
167 | | |
168 | | static std::string formatHex(long long value, bool prefix = false); |
169 | | /// Formats a 64-bit integer value in hexadecimal notation. |
170 | | /// If prefix is true, "0x" prefix is prepended to the |
171 | | /// resulting string. |
172 | | /// The value is treated as unsigned. |
173 | | |
174 | | static std::string formatHex(long long value, int width, bool prefix = false); |
175 | | /// Formats a 64-bit integer value in hexadecimal notation, |
176 | | /// right justified and zero-padded in a field having at least |
177 | | /// the specified width. |
178 | | /// The value is treated as unsigned. |
179 | | /// If prefix is true, "0x" prefix is prepended to the resulting string. |
180 | | |
181 | | static std::string format(unsigned long long value); |
182 | | /// Formats an unsigned 64-bit integer value in decimal notation. |
183 | | |
184 | | static std::string format(unsigned long long value, int width); |
185 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
186 | | /// right justified in a field having at least the specified width. |
187 | | |
188 | | static std::string format0(unsigned long long value, int width); |
189 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
190 | | /// right justified and zero-padded in a field having at least the |
191 | | /// specified width. |
192 | | |
193 | | static std::string formatHex(unsigned long long value, bool prefix = false); |
194 | | /// Formats a 64-bit integer value in hexadecimal notation. |
195 | | /// If prefix is true, "0x" prefix is prepended to the |
196 | | /// resulting string. |
197 | | |
198 | | static std::string formatHex(unsigned long long value, int width, bool prefix = false); |
199 | | /// Formats a 64-bit integer value in hexadecimal notation, |
200 | | /// right justified and zero-padded in a field having at least |
201 | | /// the specified width. If prefix is true, "0x" prefix is |
202 | | /// prepended to the resulting string. |
203 | | |
204 | | #else // ifndef POCO_INT64_IS_LONG |
205 | | |
206 | | static std::string format(Int64 value); |
207 | | /// Formats a 64-bit integer value in decimal notation. |
208 | | |
209 | | static std::string format(Int64 value, int width); |
210 | | /// Formats a 64-bit integer value in decimal notation, |
211 | | /// right justified in a field having at least the specified width. |
212 | | |
213 | | static std::string format0(Int64 value, int width); |
214 | | /// Formats a 64-bit integer value in decimal notation, |
215 | | /// right justified and zero-padded in a field having at least |
216 | | /// the specified width. |
217 | | |
218 | | static std::string formatHex(Int64 value, bool prefix = false); |
219 | | /// Formats a 64-bit integer value in hexadecimal notation. |
220 | | /// If prefix is true, "0x" prefix is prepended to the |
221 | | /// resulting string. |
222 | | /// The value is treated as unsigned. |
223 | | |
224 | | static std::string formatHex(Int64 value, int width, bool prefix = false); |
225 | | /// Formats a 64-bit integer value in hexadecimal notation, |
226 | | /// right justified and zero-padded in a field having at least |
227 | | /// the specified width. |
228 | | /// The value is treated as unsigned. |
229 | | /// If prefix is true, "0x" prefix is prepended to the resulting string. |
230 | | |
231 | | static std::string format(UInt64 value); |
232 | | /// Formats an unsigned 64-bit integer value in decimal notation. |
233 | | |
234 | | static std::string format(UInt64 value, int width); |
235 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
236 | | /// right justified in a field having at least the specified width. |
237 | | |
238 | | static std::string format0(UInt64 value, int width); |
239 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
240 | | /// right justified and zero-padded in a field having at least the |
241 | | /// specified width. |
242 | | |
243 | | static std::string formatHex(UInt64 value, bool prefix = false); |
244 | | /// Formats a 64-bit integer value in hexadecimal notation. |
245 | | /// If prefix is true, "0x" prefix is prepended to the |
246 | | /// resulting string. |
247 | | |
248 | | static std::string formatHex(UInt64 value, int width, bool prefix = false); |
249 | | /// Formats a 64-bit integer value in hexadecimal notation, |
250 | | /// right justified and zero-padded in a field having at least |
251 | | /// the specified width. If prefix is true, "0x" prefix is |
252 | | /// prepended to the resulting string. |
253 | | |
254 | | #endif // ifdef POCO_INT64_IS_LONG |
255 | | #endif // ifdef POCO_HAVE_INT64 |
256 | | |
257 | | static std::string format(float value); |
258 | | /// Formats a float value in decimal floating-point notation, |
259 | | /// according to std::printf's %g format with a precision of 8 fractional digits. |
260 | | |
261 | | static std::string format(float value, int precision); |
262 | | /// Formats a double value in decimal floating-point notation, |
263 | | /// according to std::printf's %f format with the given precision. |
264 | | |
265 | | static std::string format(float value, int width, int precision); |
266 | | /// Formats a double value in decimal floating-point notation, |
267 | | /// right justified in a field of the specified width, |
268 | | /// with the number of fractional digits given in precision. |
269 | | |
270 | | static std::string format(double value); |
271 | | /// Formats a double value in decimal floating-point notation, |
272 | | /// according to std::printf's %g format with a precision of 16 fractional digits. |
273 | | |
274 | | static std::string format(double value, int precision); |
275 | | /// Formats a double value in decimal floating-point notation, |
276 | | /// according to std::printf's %f format with the given precision. |
277 | | |
278 | | static std::string format(double value, int width, int precision); |
279 | | /// Formats a double value in decimal floating-point notation, |
280 | | /// right justified in a field of the specified width, |
281 | | /// with the number of fractional digits given in precision. |
282 | | |
283 | | static std::string format(const void* ptr); |
284 | | /// Formats a pointer in an eight (32-bit architectures) or |
285 | | /// sixteen (64-bit architectures) characters wide |
286 | | /// field in hexadecimal notation. |
287 | | |
288 | | static std::string format(bool value, BoolFormat format = FMT_TRUE_FALSE); |
289 | | /// Formats a bool value in decimal/text notation, |
290 | | /// according to format parameter. |
291 | | |
292 | | static void append(std::string& str, int value); |
293 | | /// Formats an integer value in decimal notation. |
294 | | |
295 | | static void append(std::string& str, int value, int width); |
296 | | /// Formats an integer value in decimal notation, |
297 | | /// right justified in a field having at least |
298 | | /// the specified width. |
299 | | |
300 | | static void append0(std::string& str, int value, int width); |
301 | | /// Formats an integer value in decimal notation, |
302 | | /// right justified and zero-padded in a field |
303 | | /// having at least the specified width. |
304 | | |
305 | | static void appendHex(std::string& str, int value); |
306 | | /// Formats an int value in hexadecimal notation. |
307 | | /// The value is treated as unsigned. |
308 | | |
309 | | static void appendHex(std::string& str, int value, int width); |
310 | | /// Formats a int value in hexadecimal notation, |
311 | | /// right justified and zero-padded in |
312 | | /// a field having at least the specified width. |
313 | | /// The value is treated as unsigned. |
314 | | |
315 | | static void append(std::string& str, unsigned value); |
316 | | /// Formats an unsigned int value in decimal notation. |
317 | | |
318 | | static void append(std::string& str, unsigned value, int width); |
319 | | /// Formats an unsigned long int in decimal notation, |
320 | | /// right justified in a field having at least the |
321 | | /// specified width. |
322 | | |
323 | | static void append0(std::string& str, unsigned int value, int width); |
324 | | /// Formats an unsigned int value in decimal notation, |
325 | | /// right justified and zero-padded in a field having at |
326 | | /// least the specified width. |
327 | | |
328 | | static void appendHex(std::string& str, unsigned value); |
329 | | /// Formats an unsigned int value in hexadecimal notation. |
330 | | |
331 | | static void appendHex(std::string& str, unsigned value, int width); |
332 | | /// Formats a int value in hexadecimal notation, |
333 | | /// right justified and zero-padded in |
334 | | /// a field having at least the specified width. |
335 | | |
336 | | static void append(std::string& str, long value); |
337 | | /// Formats a long value in decimal notation. |
338 | | |
339 | | static void append(std::string& str, long value, int width); |
340 | | /// Formats a long value in decimal notation, |
341 | | /// right justified in a field having at least the |
342 | | /// specified width. |
343 | | |
344 | | static void append0(std::string& str, long value, int width); |
345 | | /// Formats a long value in decimal notation, |
346 | | /// right justified and zero-padded in a field |
347 | | /// having at least the specified width. |
348 | | |
349 | | static void appendHex(std::string& str, long value); |
350 | | /// Formats an unsigned long value in hexadecimal notation. |
351 | | /// The value is treated as unsigned. |
352 | | |
353 | | static void appendHex(std::string& str, long value, int width); |
354 | | /// Formats an unsigned long value in hexadecimal notation, |
355 | | /// right justified and zero-padded in a field having at least the |
356 | | /// specified width. |
357 | | /// The value is treated as unsigned. |
358 | | |
359 | | static void append(std::string& str, unsigned long value); |
360 | | /// Formats an unsigned long value in decimal notation. |
361 | | |
362 | | static void append(std::string& str, unsigned long value, int width); |
363 | | /// Formats an unsigned long value in decimal notation, |
364 | | /// right justified in a field having at least the specified |
365 | | /// width. |
366 | | |
367 | | static void append0(std::string& str, unsigned long value, int width); |
368 | | /// Formats an unsigned long value in decimal notation, |
369 | | /// right justified and zero-padded |
370 | | /// in a field having at least the specified width. |
371 | | |
372 | | static void appendHex(std::string& str, unsigned long value); |
373 | | /// Formats an unsigned long value in hexadecimal notation. |
374 | | |
375 | | static void appendHex(std::string& str, unsigned long value, int width); |
376 | | /// Formats an unsigned long value in hexadecimal notation, |
377 | | /// right justified and zero-padded in a field having at least the |
378 | | /// specified width. |
379 | | |
380 | | #ifdef POCO_HAVE_INT64 |
381 | | #ifdef POCO_INT64_IS_LONG |
382 | | |
383 | | static void append(std::string& str, long long value); |
384 | | /// Formats a 64-bit integer value in decimal notation. |
385 | | |
386 | | static void append(std::string& str, long long value, int width); |
387 | | /// Formats a 64-bit integer value in decimal notation, |
388 | | /// right justified in a field having at least the specified width. |
389 | | |
390 | | static void append0(std::string& str, long long value, int width); |
391 | | /// Formats a 64-bit integer value in decimal notation, |
392 | | /// right justified and zero-padded in a field having at least |
393 | | /// the specified width. |
394 | | |
395 | | static void appendHex(std::string& str, long long value); |
396 | | /// Formats a 64-bit integer value in hexadecimal notation. |
397 | | /// The value is treated as unsigned. |
398 | | |
399 | | static void appendHex(std::string& str, long long value, int width); |
400 | | /// Formats a 64-bit integer value in hexadecimal notation, |
401 | | /// right justified and zero-padded in a field having at least |
402 | | /// the specified width. |
403 | | /// The value is treated as unsigned. |
404 | | |
405 | | static void append(std::string& str, unsigned long long value); |
406 | | /// Formats an unsigned 64-bit integer value in decimal notation. |
407 | | |
408 | | static void append(std::string& str, unsigned long long value, int width); |
409 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
410 | | /// right justified in a field having at least the specified width. |
411 | | |
412 | | static void append0(std::string& str, unsigned long long value, int width); |
413 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
414 | | /// right justified and zero-padded in a field having at least the |
415 | | /// specified width. |
416 | | |
417 | | static void appendHex(std::string& str, unsigned long long value); |
418 | | /// Formats a 64-bit integer value in hexadecimal notation. |
419 | | |
420 | | static void appendHex(std::string& str, unsigned long long value, int width); |
421 | | /// Formats a 64-bit integer value in hexadecimal notation, |
422 | | /// right justified and zero-padded in a field having at least |
423 | | /// the specified width. |
424 | | |
425 | | #else // ifndef POCO_INT64_IS_LONG |
426 | | |
427 | | static void append(std::string& str, Int64 value); |
428 | | /// Formats a 64-bit integer value in decimal notation. |
429 | | |
430 | | static void append(std::string& str, Int64 value, int width); |
431 | | /// Formats a 64-bit integer value in decimal notation, |
432 | | /// right justified in a field having at least the specified width. |
433 | | |
434 | | static void append0(std::string& str, Int64 value, int width); |
435 | | /// Formats a 64-bit integer value in decimal notation, |
436 | | /// right justified and zero-padded in a field having at least |
437 | | /// the specified width. |
438 | | |
439 | | static void appendHex(std::string& str, Int64 value); |
440 | | /// Formats a 64-bit integer value in hexadecimal notation. |
441 | | /// The value is treated as unsigned. |
442 | | |
443 | | static void appendHex(std::string& str, Int64 value, int width); |
444 | | /// Formats a 64-bit integer value in hexadecimal notation, |
445 | | /// right justified and zero-padded in a field having at least |
446 | | /// the specified width. |
447 | | /// The value is treated as unsigned. |
448 | | |
449 | | static void append(std::string& str, UInt64 value); |
450 | | /// Formats an unsigned 64-bit integer value in decimal notation. |
451 | | |
452 | | static void append(std::string& str, UInt64 value, int width); |
453 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
454 | | /// right justified in a field having at least the specified width. |
455 | | |
456 | | static void append0(std::string& str, UInt64 value, int width); |
457 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
458 | | /// right justified and zero-padded in a field having at least the |
459 | | /// specified width. |
460 | | |
461 | | static void appendHex(std::string& str, UInt64 value); |
462 | | /// Formats a 64-bit integer value in hexadecimal notation. |
463 | | |
464 | | static void appendHex(std::string& str, UInt64 value, int width); |
465 | | /// Formats a 64-bit integer value in hexadecimal notation, |
466 | | /// right justified and zero-padded in a field having at least |
467 | | /// the specified width. |
468 | | |
469 | | #endif // ifdef POCO_INT64_IS_LONG |
470 | | #endif // ifdef POCO_HAVE_INT64 |
471 | | |
472 | | static void append(std::string& str, float value); |
473 | | /// Formats a float value in decimal floating-point notation, |
474 | | /// according to std::printf's %g format with a precision of 8 fractional digits. |
475 | | |
476 | | static void append(std::string& str, float value, int precision); |
477 | | /// Formats a double value in decimal floating-point notation, |
478 | | /// according to std::printf's %f format with the given precision. |
479 | | |
480 | | static void append(std::string& str, float value, int width, int precision); |
481 | | /// Formats a double value in decimal floating-point notation, |
482 | | /// right justified in a field of the specified width, |
483 | | /// with the number of fractional digits given in precision. |
484 | | |
485 | | static void append(std::string& str, double value); |
486 | | /// Formats a double value in decimal floating-point notation, |
487 | | /// according to std::printf's %g format with a precision of 16 fractional digits. |
488 | | |
489 | | static void append(std::string& str, double value, int precision); |
490 | | /// Formats a double value in decimal floating-point notation, |
491 | | /// according to std::printf's %f format with the given precision. |
492 | | |
493 | | static void append(std::string& str, double value, int width, int precision); |
494 | | /// Formats a double value in decimal floating-point notation, |
495 | | /// right justified in a field of the specified width, |
496 | | /// with the number of fractional digits given in precision. |
497 | | |
498 | | static void append(std::string& str, const void* ptr); |
499 | | /// Formats a pointer in an eight (32-bit architectures) or |
500 | | /// sixteen (64-bit architectures) characters wide |
501 | | /// field in hexadecimal notation. |
502 | | |
503 | | private: |
504 | | }; |
505 | | |
506 | | |
507 | | // |
508 | | // inlines |
509 | | // |
510 | | |
511 | | inline std::string NumberFormatter::format(int value) |
512 | 0 | { |
513 | 0 | std::string result; |
514 | 0 | intToStr(value, 10, result); |
515 | 0 | return result; |
516 | 0 | } |
517 | | |
518 | | |
519 | | inline std::string NumberFormatter::format(int value, int width) |
520 | 0 | { |
521 | 0 | std::string result; |
522 | 0 | intToStr(value, 10, result, false, width, ' '); |
523 | 0 | return result; |
524 | 0 | } |
525 | | |
526 | | |
527 | | inline std::string NumberFormatter::format0(int value, int width) |
528 | 0 | { |
529 | 0 | std::string result; |
530 | 0 | intToStr(value, 10, result, false, width, '0'); |
531 | 0 | return result; |
532 | 0 | } |
533 | | |
534 | | |
535 | | inline std::string NumberFormatter::formatHex(int value, bool prefix) |
536 | 0 | { |
537 | 0 | std::string result; |
538 | 0 | uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix); |
539 | 0 | return result; |
540 | 0 | } |
541 | | |
542 | | |
543 | | inline std::string NumberFormatter::formatHex(int value, int width, bool prefix) |
544 | 0 | { |
545 | 0 | std::string result; |
546 | 0 | uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix, width, '0'); |
547 | 0 | return result; |
548 | 0 | } |
549 | | |
550 | | |
551 | | inline std::string NumberFormatter::format(unsigned value) |
552 | 0 | { |
553 | 0 | std::string result; |
554 | 0 | uIntToStr(value, 10, result); |
555 | 0 | return result; |
556 | 0 | } |
557 | | |
558 | | |
559 | | inline std::string NumberFormatter::format(unsigned value, int width) |
560 | 0 | { |
561 | 0 | std::string result; |
562 | 0 | uIntToStr(value, 10, result, false, width, ' '); |
563 | 0 | return result; |
564 | 0 | } |
565 | | |
566 | | |
567 | | inline std::string NumberFormatter::format0(unsigned int value, int width) |
568 | 0 | { |
569 | 0 | std::string result; |
570 | 0 | uIntToStr(value, 10, result, false, width, '0'); |
571 | 0 | return result; |
572 | 0 | } |
573 | | |
574 | | |
575 | | inline std::string NumberFormatter::formatHex(unsigned value, bool prefix) |
576 | 0 | { |
577 | 0 | std::string result; |
578 | 0 | uIntToStr(value, 0x10, result, prefix); |
579 | 0 | return result; |
580 | 0 | } |
581 | | |
582 | | |
583 | | inline std::string NumberFormatter::formatHex(unsigned value, int width, bool prefix) |
584 | 0 | { |
585 | 0 | std::string result; |
586 | 0 | uIntToStr(value, 0x10, result, prefix, width, '0'); |
587 | 0 | return result; |
588 | 0 | } |
589 | | |
590 | | |
591 | | inline std::string NumberFormatter::format(long value) |
592 | 0 | { |
593 | 0 | std::string result; |
594 | 0 | intToStr(value, 10, result); |
595 | 0 | return result; |
596 | 0 | } |
597 | | |
598 | | |
599 | | inline std::string NumberFormatter::format(long value, int width) |
600 | 0 | { |
601 | 0 | std::string result; |
602 | 0 | intToStr(value, 10, result, false, width, ' '); |
603 | 0 | return result; |
604 | 0 | } |
605 | | |
606 | | |
607 | | inline std::string NumberFormatter::format0(long value, int width) |
608 | 0 | { |
609 | 0 | std::string result; |
610 | 0 | intToStr(value, 10, result, false, width, '0'); |
611 | 0 | return result; |
612 | 0 | } |
613 | | |
614 | | |
615 | | inline std::string NumberFormatter::formatHex(long value, bool prefix) |
616 | 0 | { |
617 | 0 | std::string result; |
618 | 0 | uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix); |
619 | 0 | return result; |
620 | 0 | } |
621 | | |
622 | | |
623 | | inline std::string NumberFormatter::formatHex(long value, int width, bool prefix) |
624 | 0 | { |
625 | 0 | std::string result; |
626 | 0 | uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix, width, '0'); |
627 | 0 | return result; |
628 | 0 | } |
629 | | |
630 | | |
631 | | inline std::string NumberFormatter::format(unsigned long value) |
632 | 0 | { |
633 | 0 | std::string result; |
634 | 0 | uIntToStr(value, 10, result); |
635 | 0 | return result; |
636 | 0 | } |
637 | | |
638 | | |
639 | | inline std::string NumberFormatter::format(unsigned long value, int width) |
640 | 0 | { |
641 | 0 | std::string result; |
642 | 0 | uIntToStr(value, 10, result, false, width, ' '); |
643 | 0 | return result; |
644 | 0 | } |
645 | | |
646 | | |
647 | | inline std::string NumberFormatter::format0(unsigned long value, int width) |
648 | 0 | { |
649 | 0 | std::string result; |
650 | 0 | uIntToStr(value, 10, result, false, width, '0'); |
651 | 0 | return result; |
652 | 0 | } |
653 | | |
654 | | |
655 | | inline std::string NumberFormatter::formatHex(unsigned long value, bool prefix) |
656 | 0 | { |
657 | 0 | std::string result; |
658 | 0 | uIntToStr(value, 0x10, result, prefix); |
659 | 0 | return result; |
660 | 0 | } |
661 | | |
662 | | |
663 | | inline std::string NumberFormatter::formatHex(unsigned long value, int width, bool prefix) |
664 | 0 | { |
665 | 0 | std::string result; |
666 | 0 | uIntToStr(value, 0x10, result, prefix, width, '0'); |
667 | 0 | return result; |
668 | 0 | } |
669 | | |
670 | | |
671 | | #ifdef POCO_HAVE_INT64 |
672 | | #ifdef POCO_INT64_IS_LONG |
673 | | |
674 | | |
675 | | inline std::string NumberFormatter::format(long long value) |
676 | 0 | { |
677 | 0 | std::string result; |
678 | 0 | intToStr(value, 10, result); |
679 | 0 | return result; |
680 | 0 | } |
681 | | |
682 | | |
683 | | inline std::string NumberFormatter::format(long long value, int width) |
684 | 0 | { |
685 | 0 | std::string result; |
686 | 0 | intToStr(value, 10, result, false, width, ' '); |
687 | 0 | return result; |
688 | 0 | } |
689 | | |
690 | | |
691 | | inline std::string NumberFormatter::format0(long long value, int width) |
692 | 0 | { |
693 | 0 | std::string result; |
694 | 0 | intToStr(value, 10, result, false, width, '0'); |
695 | 0 | return result; |
696 | 0 | } |
697 | | |
698 | | |
699 | | inline std::string NumberFormatter::formatHex(long long value, bool prefix) |
700 | 0 | { |
701 | 0 | std::string result; |
702 | 0 | uIntToStr(static_cast<unsigned long long>(value), 0x10, result, prefix); |
703 | 0 | return result; |
704 | 0 | } |
705 | | |
706 | | |
707 | | inline std::string NumberFormatter::formatHex(long long value, int width, bool prefix) |
708 | 0 | { |
709 | 0 | std::string result; |
710 | 0 | uIntToStr(static_cast<unsigned long long>(value), 0x10, result, prefix, width, '0'); |
711 | 0 | return result; |
712 | 0 | } |
713 | | |
714 | | |
715 | | inline std::string NumberFormatter::format(unsigned long long value) |
716 | 0 | { |
717 | 0 | std::string result; |
718 | 0 | uIntToStr(value, 10, result); |
719 | 0 | return result; |
720 | 0 | } |
721 | | |
722 | | |
723 | | inline std::string NumberFormatter::format(unsigned long long value, int width) |
724 | 0 | { |
725 | 0 | std::string result; |
726 | 0 | uIntToStr(value, 10, result, false, width, ' '); |
727 | 0 | return result; |
728 | 0 | } |
729 | | |
730 | | |
731 | | inline std::string NumberFormatter::format0(unsigned long long value, int width) |
732 | 0 | { |
733 | 0 | std::string result; |
734 | 0 | uIntToStr(value, 10, result, false, width, '0'); |
735 | 0 | return result; |
736 | 0 | } |
737 | | |
738 | | |
739 | | inline std::string NumberFormatter::formatHex(unsigned long long value, bool prefix) |
740 | 0 | { |
741 | 0 | std::string result; |
742 | 0 | uIntToStr(value, 0x10, result, prefix); |
743 | 0 | return result; |
744 | 0 | } |
745 | | |
746 | | |
747 | | inline std::string NumberFormatter::formatHex(unsigned long long value, int width, bool prefix) |
748 | 0 | { |
749 | 0 | std::string result; |
750 | 0 | uIntToStr(value, 0x10, result, prefix, width, '0'); |
751 | 0 | return result; |
752 | 0 | } |
753 | | |
754 | | |
755 | | #else // ifndef POCO_LONG_IS_64_BIT |
756 | | |
757 | | |
758 | | inline std::string NumberFormatter::format(Int64 value) |
759 | | { |
760 | | std::string result; |
761 | | intToStr(value, 10, result); |
762 | | return result; |
763 | | } |
764 | | |
765 | | |
766 | | inline std::string NumberFormatter::format(Int64 value, int width) |
767 | | { |
768 | | std::string result; |
769 | | intToStr(value, 10, result, false, width, ' '); |
770 | | return result; |
771 | | } |
772 | | |
773 | | |
774 | | inline std::string NumberFormatter::format0(Int64 value, int width) |
775 | | { |
776 | | std::string result; |
777 | | intToStr(value, 10, result, false, width, '0'); |
778 | | return result; |
779 | | } |
780 | | |
781 | | |
782 | | inline std::string NumberFormatter::formatHex(Int64 value, bool prefix) |
783 | | { |
784 | | std::string result; |
785 | | uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix); |
786 | | return result; |
787 | | } |
788 | | |
789 | | |
790 | | inline std::string NumberFormatter::formatHex(Int64 value, int width, bool prefix) |
791 | | { |
792 | | std::string result; |
793 | | uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix, width, '0'); |
794 | | return result; |
795 | | } |
796 | | |
797 | | |
798 | | inline std::string NumberFormatter::format(UInt64 value) |
799 | | { |
800 | | std::string result; |
801 | | uIntToStr(value, 10, result); |
802 | | return result; |
803 | | } |
804 | | |
805 | | |
806 | | inline std::string NumberFormatter::format(UInt64 value, int width) |
807 | | { |
808 | | std::string result; |
809 | | uIntToStr(value, 10, result, false, width, ' '); |
810 | | return result; |
811 | | } |
812 | | |
813 | | |
814 | | inline std::string NumberFormatter::format0(UInt64 value, int width) |
815 | | { |
816 | | std::string result; |
817 | | uIntToStr(value, 10, result, false, width, '0'); |
818 | | return result; |
819 | | } |
820 | | |
821 | | |
822 | | inline std::string NumberFormatter::formatHex(UInt64 value, bool prefix) |
823 | | { |
824 | | std::string result; |
825 | | uIntToStr(value, 0x10, result, prefix); |
826 | | return result; |
827 | | } |
828 | | |
829 | | |
830 | | inline std::string NumberFormatter::formatHex(UInt64 value, int width, bool prefix) |
831 | | { |
832 | | std::string result; |
833 | | uIntToStr(value, 0x10, result, prefix, width, '0'); |
834 | | return result; |
835 | | } |
836 | | |
837 | | |
838 | | #endif // ifdef POCO_INT64_IS_LONG |
839 | | #endif // ifdef POCO_HAVE_INT64 |
840 | | |
841 | | |
842 | | inline std::string NumberFormatter::format(float value) |
843 | 0 | { |
844 | 0 | char buffer[POCO_MAX_FLT_STRING_LEN]; |
845 | 0 | floatToStr(buffer, POCO_MAX_FLT_STRING_LEN, value); |
846 | 0 | return std::string(buffer); |
847 | 0 | } |
848 | | |
849 | | |
850 | | inline std::string NumberFormatter::format(float value, int precision) |
851 | 0 | { |
852 | 0 | if (precision < 0) |
853 | 0 | throw InvalidArgumentException("NumberFormatter::format() requires non-negative precision."); |
854 | 0 | char buffer[POCO_MAX_FLT_STRING_LEN]; |
855 | 0 | floatToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision); |
856 | 0 | return std::string(buffer); |
857 | 0 | } |
858 | | |
859 | | |
860 | | inline std::string NumberFormatter::format(float value, int width, int precision) |
861 | 0 | { |
862 | 0 | if (precision < 0) |
863 | 0 | throw InvalidArgumentException("NumberFormatter::format() requires non-negative precision."); |
864 | 0 | std::string result; |
865 | 0 | floatToFixedStr(result, value, precision, width); |
866 | 0 | return result; |
867 | 0 | } |
868 | | |
869 | | |
870 | | inline std::string NumberFormatter::format(double value) |
871 | 0 | { |
872 | 0 | char buffer[POCO_MAX_FLT_STRING_LEN]; |
873 | 0 | doubleToStr(buffer, POCO_MAX_FLT_STRING_LEN, value); |
874 | 0 | return std::string(buffer); |
875 | 0 | } |
876 | | |
877 | | |
878 | | inline std::string NumberFormatter::format(double value, int precision) |
879 | 0 | { |
880 | 0 | if (precision < 0) |
881 | 0 | throw InvalidArgumentException("NumberFormatter::format() requires non-negative precision."); |
882 | 0 | char buffer[POCO_MAX_FLT_STRING_LEN]; |
883 | 0 | doubleToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision); |
884 | 0 | return std::string(buffer); |
885 | 0 | } |
886 | | |
887 | | |
888 | | inline std::string NumberFormatter::format(double value, int width, int precision) |
889 | 0 | { |
890 | 0 | if (precision < 0) |
891 | 0 | throw InvalidArgumentException("NumberFormatter::format() requires non-negative precision."); |
892 | 0 | std::string result; |
893 | 0 | doubleToFixedStr(result, value, precision, width); |
894 | 0 | return result; |
895 | 0 | } |
896 | | |
897 | | |
898 | | inline std::string NumberFormatter::format(const void* ptr) |
899 | 0 | { |
900 | 0 | std::string result; |
901 | 0 | append(result, ptr); |
902 | 0 | return result; |
903 | 0 | } |
904 | | |
905 | | |
906 | | } // namespace Poco |
907 | | |
908 | | |
909 | | #endif // Foundation_NumberFormatter_INCLUDED |