/src/poco/Foundation/include/Poco/NumberFormatter.h
Line | Count | Source |
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 | | enum class Options |
47 | | /// Options to control the format of the generated string. |
48 | | { |
49 | | DEFAULT = 0, |
50 | | /// formatHex defaults: No 0x prefix, uppercase hexadecimal values |
51 | | HEX_PREFIX = (1 << 0), |
52 | | /// formatHex: Prepend prefix 0x |
53 | | HEX_LOWERCASE = (1 << 1) |
54 | | /// formatHex: Use lowercase letters for hexadecimal values |
55 | | }; |
56 | | |
57 | | static const unsigned NF_MAX_INT_STRING_LEN = 32; // increase for 64-bit binary formatting support |
58 | | static const unsigned NF_MAX_FLT_STRING_LEN = POCO_MAX_FLT_STRING_LEN; |
59 | | |
60 | | static std::string format(int value); |
61 | | /// Formats an integer value in decimal notation. |
62 | | |
63 | | static std::string format(int value, int width); |
64 | | /// Formats an integer value in decimal notation, |
65 | | /// right justified in a field having at least |
66 | | /// the specified width. |
67 | | |
68 | | static std::string format0(int value, int width); |
69 | | /// Formats an integer value in decimal notation, |
70 | | /// right justified and zero-padded in a field |
71 | | /// having at least the specified width. |
72 | | |
73 | | static std::string formatHex(int value, Options options = Options::DEFAULT); |
74 | | /// Formats an int value in hexadecimal notation. |
75 | | /// Options (see NumberFormatter::Options) define the format of the |
76 | | /// generated string. |
77 | | /// The value is treated as unsigned. |
78 | | |
79 | | static std::string formatHex(int value, int width, Options options = Options::DEFAULT); |
80 | | /// Formats an int value in hexadecimal notation, |
81 | | /// right justified and zero-padded in |
82 | | /// a field having at least the specified width. |
83 | | /// Options (see NumberFormatter::Options) define the format of the |
84 | | /// generated string. |
85 | | /// The value is treated as unsigned. |
86 | | |
87 | | static std::string format(unsigned value); |
88 | | /// Formats an unsigned int value in decimal notation. |
89 | | |
90 | | static std::string format(unsigned value, int width); |
91 | | /// Formats an unsigned long int in decimal notation, |
92 | | /// right justified in a field having at least the |
93 | | /// specified width. |
94 | | |
95 | | static std::string format0(unsigned int value, int width); |
96 | | /// Formats an unsigned int value in decimal notation, |
97 | | /// right justified and zero-padded in a field having at |
98 | | /// least the specified width. |
99 | | |
100 | | static std::string formatHex(unsigned value, Options options = Options::DEFAULT); |
101 | | /// Formats an unsigned int value in hexadecimal notation. |
102 | | /// Options (see NumberFormatter::Options) define the format of the |
103 | | /// generated string. |
104 | | |
105 | | static std::string formatHex(unsigned value, int width, Options options = Options::DEFAULT); |
106 | | /// Formats an unsigned value in hexadecimal notation, |
107 | | /// right justified and zero-padded in |
108 | | /// a field having at least the specified width. |
109 | | /// Options (see NumberFormatter::Options) define the format of the |
110 | | /// generated string. |
111 | | |
112 | | static std::string format(long value); |
113 | | /// Formats a long value in decimal notation. |
114 | | |
115 | | static std::string format(long value, int width); |
116 | | /// Formats a long value in decimal notation, |
117 | | /// right justified in a field having at least the |
118 | | /// specified width. |
119 | | |
120 | | static std::string format0(long value, int width); |
121 | | /// Formats a long value in decimal notation, |
122 | | /// right justified and zero-padded in a field |
123 | | /// having at least the specified width. |
124 | | |
125 | | static std::string formatHex(long value, Options options = Options::DEFAULT); |
126 | | /// Formats a long value in hexadecimal notation. |
127 | | /// Options (see NumberFormatter::Options) define the format of the |
128 | | /// generated string. |
129 | | |
130 | | static std::string formatHex(long value, int width, Options options = Options::DEFAULT); |
131 | | /// Formats a long value in hexadecimal notation, |
132 | | /// right justified and zero-padded in |
133 | | /// a field having at least the specified width. |
134 | | /// Options (see NumberFormatter::Options) define the format of the |
135 | | /// generated string. |
136 | | |
137 | | static std::string format(unsigned long value); |
138 | | /// Formats an unsigned long value in decimal notation. |
139 | | |
140 | | static std::string format(unsigned long value, int width); |
141 | | /// Formats an unsigned long value in decimal notation, |
142 | | /// right justified in a field having at least the specified |
143 | | /// width. |
144 | | |
145 | | static std::string format0(unsigned long value, int width); |
146 | | /// Formats an unsigned long value in decimal notation, |
147 | | /// right justified and zero-padded |
148 | | /// in a field having at least the specified width. |
149 | | |
150 | | static std::string formatHex(unsigned long value, Options options = Options::DEFAULT); |
151 | | /// Formats an unsigned long value in hexadecimal notation. |
152 | | /// Options (see NumberFormatter::Options) define the format of the |
153 | | /// generated string. |
154 | | |
155 | | static std::string formatHex(unsigned long value, int width, Options options = Options::DEFAULT); |
156 | | /// Formats an unsigned long value in hexadecimal notation, |
157 | | /// right justified and zero-padded in |
158 | | /// a field having at least the specified width. |
159 | | /// Options (see NumberFormatter::Options) define the format of the |
160 | | /// generated string. |
161 | | |
162 | | #ifdef POCO_HAVE_INT64 |
163 | | #ifdef POCO_INT64_IS_LONG |
164 | | |
165 | | static std::string format(long long value); |
166 | | /// Formats a 64-bit integer value in decimal notation. |
167 | | |
168 | | static std::string format(long long value, int width); |
169 | | /// Formats a 64-bit integer value in decimal notation, |
170 | | /// right justified in a field having at least the specified width. |
171 | | |
172 | | static std::string format0(long long value, int width); |
173 | | /// Formats a 64-bit integer value in decimal notation, |
174 | | /// right justified and zero-padded in a field having at least |
175 | | /// the specified width. |
176 | | |
177 | | static std::string formatHex(long long value, Options options = Options::DEFAULT); |
178 | | /// Formats a 64-bit integer value in hexadecimal notation. |
179 | | /// Options (see NumberFormatter::Options) define the format of the |
180 | | /// generated string. |
181 | | |
182 | | static std::string formatHex(long long value, int width, Options options = Options::DEFAULT); |
183 | | /// Formats a 64-bit integer value in hexadecimal notation, |
184 | | /// right justified and zero-padded in a field having at least |
185 | | /// the specified width. |
186 | | /// The value is treated as unsigned. |
187 | | /// Options (see NumberFormatter::Options) define the format of the |
188 | | /// generated string. |
189 | | |
190 | | static std::string format(unsigned long long value); |
191 | | /// Formats an unsigned 64-bit integer value in decimal notation. |
192 | | |
193 | | static std::string format(unsigned long long value, int width); |
194 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
195 | | /// right justified in a field having at least the specified width. |
196 | | |
197 | | static std::string format0(unsigned long long value, int width); |
198 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
199 | | /// right justified and zero-padded in a field having at least the |
200 | | /// specified width. |
201 | | |
202 | | static std::string formatHex(unsigned long long value, Options options = Options::DEFAULT); |
203 | | /// Formats an unsigned 64-bit value in hexadecimal notation. |
204 | | /// Options (see NumberFormatter::Options) define the format of the |
205 | | /// generated string. |
206 | | |
207 | | static std::string formatHex(unsigned long long value, int width, Options options = Options::DEFAULT); |
208 | | /// Formats an unsigned 64-bit value in hexadecimal notation, |
209 | | /// right justified and zero-padded in a field having at least |
210 | | /// the specified width. |
211 | | /// The value is treated as unsigned. |
212 | | /// Options (see NumberFormatter::Options) define the format of the |
213 | | /// generated string. |
214 | | |
215 | | #else // ifndef POCO_INT64_IS_LONG |
216 | | |
217 | | static std::string format(Int64 value); |
218 | | /// Formats a 64-bit integer value in decimal notation. |
219 | | |
220 | | static std::string format(Int64 value, int width); |
221 | | /// Formats a 64-bit integer value in decimal notation, |
222 | | /// right justified in a field having at least the specified width. |
223 | | |
224 | | static std::string format0(Int64 value, int width); |
225 | | /// Formats a 64-bit integer value in decimal notation, |
226 | | /// right justified and zero-padded in a field having at least |
227 | | /// the specified width. |
228 | | |
229 | | static std::string formatHex(Int64 value, Options options = Options::DEFAULT); |
230 | | /// Formats a 64-bit integer value in hexadecimal notation. |
231 | | /// Options (see NumberFormatter::Options) define the format of the |
232 | | /// generated string. |
233 | | |
234 | | static std::string formatHex(Int64 value, int width, Options options = Options::DEFAULT); |
235 | | /// Formats a 64-bit integer value in hexadecimal notation, |
236 | | /// right justified and zero-padded in a field having at least |
237 | | /// the specified width. |
238 | | /// The value is treated as unsigned. |
239 | | /// Options (see NumberFormatter::Options) define the format of the |
240 | | /// generated string. |
241 | | |
242 | | static std::string format(UInt64 value); |
243 | | /// Formats an unsigned 64-bit integer value in decimal notation. |
244 | | |
245 | | static std::string format(UInt64 value, int width); |
246 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
247 | | /// right justified in a field having at least the specified width. |
248 | | |
249 | | static std::string format0(UInt64 value, int width); |
250 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
251 | | /// right justified and zero-padded in a field having at least the |
252 | | /// specified width. |
253 | | |
254 | | static std::string formatHex(UInt64 value, Options options = Options::DEFAULT); |
255 | | /// Formats an unsigned 64-bit integer in hexadecimal notation. |
256 | | /// Options (see NumberFormatter::Options) define the format of the |
257 | | /// generated string. |
258 | | |
259 | | static std::string formatHex(UInt64 value, int width, Options options = Options::DEFAULT); |
260 | | /// Formats an unsigned 64-bit integer in hexadecimal notation, |
261 | | /// right justified and zero-padded in a field having at least |
262 | | /// the specified width. |
263 | | /// The value is treated as unsigned. |
264 | | /// Options (see NumberFormatter::Options) define the format of the |
265 | | /// generated string. |
266 | | |
267 | | #endif // ifdef POCO_INT64_IS_LONG |
268 | | #endif // ifdef POCO_HAVE_INT64 |
269 | | |
270 | | static std::string format(float value); |
271 | | /// Formats a float value in decimal floating-point notation, |
272 | | /// according to std::printf's %g format with a precision of 8 fractional digits. |
273 | | |
274 | | static std::string format(float 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(float 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(double value); |
284 | | /// Formats a double value in decimal floating-point notation, |
285 | | /// according to std::printf's %g format with a precision of 16 fractional digits. |
286 | | |
287 | | static std::string format(double value, int precision); |
288 | | /// Formats a double value in decimal floating-point notation, |
289 | | /// according to std::printf's %f format with the given precision. |
290 | | |
291 | | static std::string format(double value, int width, int precision); |
292 | | /// Formats a double value in decimal floating-point notation, |
293 | | /// right justified in a field of the specified width, |
294 | | /// with the number of fractional digits given in precision. |
295 | | |
296 | | static std::string format(const void* ptr); |
297 | | /// Formats a pointer in an eight (32-bit architectures) or |
298 | | /// sixteen (64-bit architectures) characters wide |
299 | | /// field in hexadecimal notation. |
300 | | |
301 | | static std::string format(bool value, BoolFormat format = FMT_TRUE_FALSE); |
302 | | /// Formats a bool value in decimal/text notation, |
303 | | /// according to format parameter. |
304 | | |
305 | | static void append(std::string& str, int value); |
306 | | /// Formats an integer value in decimal notation. |
307 | | |
308 | | static void append(std::string& str, int value, int width); |
309 | | /// Formats an integer value in decimal notation, |
310 | | /// right justified in a field having at least |
311 | | /// the specified width. |
312 | | |
313 | | static void append0(std::string& str, int value, int width); |
314 | | /// Formats an integer value in decimal notation, |
315 | | /// right justified and zero-padded in a field |
316 | | /// having at least the specified width. |
317 | | |
318 | | static void appendHex(std::string& str, int value, bool lowercase = false); |
319 | | /// Formats an int value in hexadecimal notation. |
320 | | /// The value is treated as unsigned. |
321 | | |
322 | | static void appendHex(std::string& str, int value, int width, bool lowercase = false); |
323 | | /// Formats a int value in hexadecimal notation, |
324 | | /// right justified and zero-padded in |
325 | | /// a field having at least the specified width. |
326 | | /// The value is treated as unsigned. |
327 | | |
328 | | static void append(std::string& str, unsigned value); |
329 | | /// Formats an unsigned int value in decimal notation. |
330 | | |
331 | | static void append(std::string& str, unsigned value, int width); |
332 | | /// Formats an unsigned long int in decimal notation, |
333 | | /// right justified in a field having at least the |
334 | | /// specified width. |
335 | | |
336 | | static void append0(std::string& str, unsigned int value, int width); |
337 | | /// Formats an unsigned int value in decimal notation, |
338 | | /// right justified and zero-padded in a field having at |
339 | | /// least the specified width. |
340 | | |
341 | | static void appendHex(std::string& str, unsigned value, bool lowercase = false); |
342 | | /// Formats an unsigned int value in hexadecimal notation. |
343 | | |
344 | | static void appendHex(std::string& str, unsigned value, int width, bool lowercase = false); |
345 | | /// Formats a int value in hexadecimal notation, |
346 | | /// right justified and zero-padded in |
347 | | /// a field having at least the specified width. |
348 | | |
349 | | static void append(std::string& str, long value); |
350 | | /// Formats a long value in decimal notation. |
351 | | |
352 | | static void append(std::string& str, long value, int width); |
353 | | /// Formats a long value in decimal notation, |
354 | | /// right justified in a field having at least the |
355 | | /// specified width. |
356 | | |
357 | | static void append0(std::string& str, long value, int width); |
358 | | /// Formats a long value in decimal notation, |
359 | | /// right justified and zero-padded in a field |
360 | | /// having at least the specified width. |
361 | | |
362 | | static void appendHex(std::string& str, long value, bool lowercase = false); |
363 | | /// Formats an unsigned long value in hexadecimal notation. |
364 | | /// The value is treated as unsigned. |
365 | | |
366 | | static void appendHex(std::string& str, long value, int width, bool lowercase = false); |
367 | | /// Formats an unsigned long value in hexadecimal notation, |
368 | | /// right justified and zero-padded in a field having at least the |
369 | | /// specified width. |
370 | | /// The value is treated as unsigned. |
371 | | |
372 | | static void append(std::string& str, unsigned long value); |
373 | | /// Formats an unsigned long value in decimal notation. |
374 | | |
375 | | static void append(std::string& str, unsigned long value, int width); |
376 | | /// Formats an unsigned long value in decimal notation, |
377 | | /// right justified in a field having at least the specified |
378 | | /// width. |
379 | | |
380 | | static void append0(std::string& str, unsigned long value, int width); |
381 | | /// Formats an unsigned long value in decimal notation, |
382 | | /// right justified and zero-padded |
383 | | /// in a field having at least the specified width. |
384 | | |
385 | | static void appendHex(std::string& str, unsigned long value, bool lowercase = false); |
386 | | /// Formats an unsigned long value in hexadecimal notation. |
387 | | |
388 | | static void appendHex(std::string& str, unsigned long value, int width, bool lowercase = false); |
389 | | /// Formats an unsigned long value in hexadecimal notation, |
390 | | /// right justified and zero-padded in a field having at least the |
391 | | /// specified width. |
392 | | |
393 | | #ifdef POCO_HAVE_INT64 |
394 | | #ifdef POCO_INT64_IS_LONG |
395 | | |
396 | | static void append(std::string& str, long long value); |
397 | | /// Formats a 64-bit integer value in decimal notation. |
398 | | |
399 | | static void append(std::string& str, long long value, int width); |
400 | | /// Formats a 64-bit integer value in decimal notation, |
401 | | /// right justified in a field having at least the specified width. |
402 | | |
403 | | static void append0(std::string& str, long long value, int width); |
404 | | /// Formats a 64-bit integer value in decimal notation, |
405 | | /// right justified and zero-padded in a field having at least |
406 | | /// the specified width. |
407 | | |
408 | | static void appendHex(std::string& str, long long value, bool lowercase = false); |
409 | | /// Formats a 64-bit integer value in hexadecimal notation. |
410 | | /// The value is treated as unsigned. |
411 | | |
412 | | static void appendHex(std::string& str, long long value, int width, bool lowercase = false); |
413 | | /// Formats a 64-bit integer value in hexadecimal notation, |
414 | | /// right justified and zero-padded in a field having at least |
415 | | /// the specified width. |
416 | | /// The value is treated as unsigned. |
417 | | |
418 | | static void append(std::string& str, unsigned long long value); |
419 | | /// Formats an unsigned 64-bit integer value in decimal notation. |
420 | | |
421 | | static void append(std::string& str, unsigned long long value, int width); |
422 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
423 | | /// right justified in a field having at least the specified width. |
424 | | |
425 | | static void append0(std::string& str, unsigned long long value, int width); |
426 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
427 | | /// right justified and zero-padded in a field having at least the |
428 | | /// specified width. |
429 | | |
430 | | static void appendHex(std::string& str, unsigned long long value, bool lowercase = false); |
431 | | /// Formats a 64-bit integer value in hexadecimal notation. |
432 | | |
433 | | static void appendHex(std::string& str, unsigned long long value, int width, bool lowercase = false); |
434 | | /// Formats a 64-bit integer value in hexadecimal notation, |
435 | | /// right justified and zero-padded in a field having at least |
436 | | /// the specified width. |
437 | | |
438 | | #else // ifndef POCO_INT64_IS_LONG |
439 | | |
440 | | static void append(std::string& str, Int64 value); |
441 | | /// Formats a 64-bit integer value in decimal notation. |
442 | | |
443 | | static void append(std::string& str, Int64 value, int width); |
444 | | /// Formats a 64-bit integer value in decimal notation, |
445 | | /// right justified in a field having at least the specified width. |
446 | | |
447 | | static void append0(std::string& str, Int64 value, int width); |
448 | | /// Formats a 64-bit integer value in decimal notation, |
449 | | /// right justified and zero-padded in a field having at least |
450 | | /// the specified width. |
451 | | |
452 | | static void appendHex(std::string& str, Int64 value, bool lowercase = false); |
453 | | /// Formats a 64-bit integer value in hexadecimal notation. |
454 | | /// The value is treated as unsigned. |
455 | | |
456 | | static void appendHex(std::string& str, Int64 value, int width, bool lowercase = false); |
457 | | /// Formats a 64-bit integer value in hexadecimal notation, |
458 | | /// right justified and zero-padded in a field having at least |
459 | | /// the specified width. |
460 | | /// The value is treated as unsigned. |
461 | | |
462 | | static void append(std::string& str, UInt64 value); |
463 | | /// Formats an unsigned 64-bit integer value in decimal notation. |
464 | | |
465 | | static void append(std::string& str, UInt64 value, int width); |
466 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
467 | | /// right justified in a field having at least the specified width. |
468 | | |
469 | | static void append0(std::string& str, UInt64 value, int width); |
470 | | /// Formats an unsigned 64-bit integer value in decimal notation, |
471 | | /// right justified and zero-padded in a field having at least the |
472 | | /// specified width. |
473 | | |
474 | | static void appendHex(std::string& str, UInt64 value, bool lowercase = false); |
475 | | /// Formats a 64-bit integer value in hexadecimal notation. |
476 | | |
477 | | static void appendHex(std::string& str, UInt64 value, int width, bool lowercase = false); |
478 | | /// Formats a 64-bit integer value in hexadecimal notation, |
479 | | /// right justified and zero-padded in a field having at least |
480 | | /// the specified width. |
481 | | |
482 | | #endif // ifdef POCO_INT64_IS_LONG |
483 | | #endif // ifdef POCO_HAVE_INT64 |
484 | | |
485 | | static void append(std::string& str, float value); |
486 | | /// Formats a float value in decimal floating-point notation, |
487 | | /// according to std::printf's %g format with a precision of 8 fractional digits. |
488 | | |
489 | | static void append(std::string& str, float 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, float 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, double value); |
499 | | /// Formats a double value in decimal floating-point notation, |
500 | | /// according to std::printf's %g format with a precision of 16 fractional digits. |
501 | | |
502 | | static void append(std::string& str, double value, int precision); |
503 | | /// Formats a double value in decimal floating-point notation, |
504 | | /// according to std::printf's %f format with the given precision. |
505 | | |
506 | | static void append(std::string& str, double value, int width, int precision); |
507 | | /// Formats a double value in decimal floating-point notation, |
508 | | /// right justified in a field of the specified width, |
509 | | /// with the number of fractional digits given in precision. |
510 | | |
511 | | static void append(std::string& str, const void* ptr); |
512 | | /// Formats a pointer in an eight (32-bit architectures) or |
513 | | /// sixteen (64-bit architectures) characters wide |
514 | | /// field in hexadecimal notation. |
515 | | |
516 | | // |
517 | | // Deprecated functions |
518 | | // |
519 | | |
520 | | POCO_DEPRECATED("use formatHex with options instead") |
521 | | static std::string formatHex(int value, bool prefix); |
522 | | /// Formats an int value in hexadecimal notation. |
523 | | /// If prefix is true, "0x" prefix is prepended to the |
524 | | /// resulting string. |
525 | | /// The value is treated as unsigned. |
526 | | |
527 | | POCO_DEPRECATED("use formatHex with options instead") |
528 | | static std::string formatHex(int value, int width, bool prefix); |
529 | | /// Formats an int value in hexadecimal notation, |
530 | | /// right justified and zero-padded in |
531 | | /// a field having at least the specified width. |
532 | | /// If prefix is true, "0x" prefix is prepended to the |
533 | | /// resulting string. |
534 | | /// The value is treated as unsigned. |
535 | | |
536 | | POCO_DEPRECATED("use formatHex with options instead") |
537 | | static std::string formatHex(unsigned value, bool prefix); |
538 | | /// Formats an unsigned int value in hexadecimal notation. |
539 | | /// If prefix is true, "0x" prefix is prepended to the |
540 | | /// resulting string. |
541 | | |
542 | | POCO_DEPRECATED("use formatHex with options instead") |
543 | | static std::string formatHex(unsigned value, int width, bool prefix); |
544 | | /// Formats an unsigned value in hexadecimal notation, |
545 | | /// right justified and zero-padded in |
546 | | /// a field having at least the specified width. |
547 | | /// If prefix is true, "0x" prefix is prepended to the |
548 | | /// resulting string. |
549 | | |
550 | | POCO_DEPRECATED("use formatHex with options instead") |
551 | | static std::string formatHex(long value, bool prefix); |
552 | | /// Formats a long value in hexadecimal notation. |
553 | | /// If prefix is true, "0x" prefix is prepended to the |
554 | | /// resulting string. |
555 | | /// The value is treated as unsigned. |
556 | | |
557 | | POCO_DEPRECATED("use formatHex with options instead") |
558 | | static std::string formatHex(long value, int width, bool prefix); |
559 | | /// Formats a long value in hexadecimal notation, |
560 | | /// right justified and zero-padded in a field having at least the |
561 | | /// specified width. |
562 | | /// If prefix is true, "0x" prefix is prepended to the |
563 | | /// resulting string. |
564 | | /// The value is treated as unsigned. |
565 | | |
566 | | POCO_DEPRECATED("use formatHex with options instead") |
567 | | static std::string formatHex(unsigned long value, bool prefix); |
568 | | /// Formats an unsigned long value in hexadecimal notation. |
569 | | /// If prefix is true, "0x" prefix is prepended to the |
570 | | /// resulting string. |
571 | | |
572 | | POCO_DEPRECATED("use formatHex with options instead") |
573 | | static std::string formatHex(unsigned long value, int width, bool prefix); |
574 | | /// Formats an unsigned long value in hexadecimal notation, |
575 | | /// right justified and zero-padded in a field having at least the |
576 | | /// specified width. |
577 | | /// If prefix is true, "0x" prefix is prepended to the |
578 | | /// resulting string. |
579 | | |
580 | | #ifdef POCO_HAVE_INT64 |
581 | | #ifdef POCO_INT64_IS_LONG |
582 | | |
583 | | POCO_DEPRECATED("use formatHex with options instead") |
584 | | static std::string formatHex(long long value, bool prefix); |
585 | | /// Formats a 64-bit integer value in hexadecimal notation. |
586 | | /// If prefix is true, "0x" prefix is prepended to the |
587 | | /// resulting string. |
588 | | /// The value is treated as unsigned. |
589 | | |
590 | | POCO_DEPRECATED("use formatHex with options instead") |
591 | | static std::string formatHex(long long value, int width, bool prefix); |
592 | | /// Formats a 64-bit integer value in hexadecimal notation, |
593 | | /// right justified and zero-padded in a field having at least |
594 | | /// the specified width. |
595 | | /// The value is treated as unsigned. |
596 | | /// If prefix is true, "0x" prefix is prepended to the resulting string. |
597 | | |
598 | | POCO_DEPRECATED("use formatHex with options instead") |
599 | | static std::string formatHex(unsigned long long value, bool prefix); |
600 | | /// Formats an unsigned 64-bit integer value in hexadecimal notation. |
601 | | /// If prefix is true, "0x" prefix is prepended to the |
602 | | /// resulting string. |
603 | | |
604 | | POCO_DEPRECATED("use formatHex with options instead") |
605 | | static std::string formatHex(unsigned long long value, int width, bool prefix); |
606 | | /// Formats an unsigned 64-bit integer value in hexadecimal notation, |
607 | | /// right justified and zero-padded in a field having at least |
608 | | /// the specified width. If prefix is true, "0x" prefix is |
609 | | /// prepended to the resulting string. |
610 | | |
611 | | #else // ifndef POCO_INT64_IS_LONG |
612 | | |
613 | | POCO_DEPRECATED("use formatHex with options instead") |
614 | | static std::string formatHex(Int64 value, bool prefix); |
615 | | /// Formats a 64-bit integer value in hexadecimal notation. |
616 | | /// If prefix is true, "0x" prefix is prepended to the |
617 | | /// resulting string. |
618 | | /// The value is treated as unsigned. |
619 | | |
620 | | POCO_DEPRECATED("use formatHex with options instead") |
621 | | static std::string formatHex(Int64 value, int width, bool prefix); |
622 | | /// Formats a 64-bit integer value in hexadecimal notation, |
623 | | /// right justified and zero-padded in a field having at least |
624 | | /// the specified width. |
625 | | /// The value is treated as unsigned. |
626 | | /// If prefix is true, "0x" prefix is prepended to the resulting string. |
627 | | |
628 | | POCO_DEPRECATED("use formatHex with options instead") |
629 | | static std::string formatHex(UInt64 value, bool prefix); |
630 | | /// Formats an unsigned 64-bit integer value in hexadecimal notation. |
631 | | /// If prefix is true, "0x" prefix is prepended to the |
632 | | /// resulting string. |
633 | | |
634 | | POCO_DEPRECATED("use formatHex with options instead") |
635 | | static std::string formatHex(UInt64 value, int width, bool prefix); |
636 | | /// Formats an unsigned 64-bit integer value in hexadecimal notation, |
637 | | /// right justified and zero-padded in a field having at least |
638 | | /// the specified width. If prefix is true, "0x" prefix is |
639 | | /// prepended to the resulting string. |
640 | | |
641 | | #endif // ifdef POCO_INT64_IS_LONG |
642 | | #endif // ifdef POCO_HAVE_INT64 |
643 | | |
644 | | private: |
645 | | |
646 | | static bool isEnabled(NumberFormatter::Options options, NumberFormatter::Options opt); |
647 | | }; |
648 | | |
649 | | |
650 | | // |
651 | | // inlines |
652 | | // |
653 | | |
654 | | |
655 | | inline NumberFormatter::Options operator | (NumberFormatter::Options lhs, NumberFormatter::Options rhs) |
656 | 0 | { |
657 | 0 | using T = std::underlying_type_t<NumberFormatter::Options>; |
658 | 0 | return static_cast<NumberFormatter::Options>(static_cast<T>(lhs) | static_cast<T>(rhs)); |
659 | 0 | } |
660 | | |
661 | | |
662 | | inline NumberFormatter::Options& operator |= (NumberFormatter::Options& lhs, NumberFormatter::Options rhs) |
663 | 0 | { |
664 | 0 | lhs = lhs | rhs; |
665 | 0 | return lhs; |
666 | 0 | } |
667 | | |
668 | | |
669 | | inline NumberFormatter::Options operator & (NumberFormatter::Options lhs, NumberFormatter::Options rhs) |
670 | 779M | { |
671 | 779M | using T = std::underlying_type_t<NumberFormatter::Options>; |
672 | 779M | return static_cast<NumberFormatter::Options>(static_cast<T>(lhs) & static_cast<T>(rhs)); |
673 | 779M | } |
674 | | |
675 | | |
676 | | inline NumberFormatter::Options& operator &= (NumberFormatter::Options& lhs, NumberFormatter::Options rhs) |
677 | 0 | { |
678 | 0 | lhs = lhs & rhs; |
679 | 0 | return lhs; |
680 | 0 | } |
681 | | |
682 | | |
683 | | inline bool NumberFormatter::isEnabled(Options options, Options opt) |
684 | 779M | { |
685 | 779M | using T = std::underlying_type_t<Options>; |
686 | 779M | return static_cast<T>(options & opt) != 0; |
687 | 779M | } |
688 | | |
689 | | |
690 | | inline std::string NumberFormatter::format(int value) |
691 | 0 | { |
692 | 0 | std::string result; |
693 | 0 | intToStr(value, 10, result); |
694 | 0 | return result; |
695 | 0 | } |
696 | | |
697 | | |
698 | | inline std::string NumberFormatter::format(int value, int width) |
699 | 0 | { |
700 | 0 | std::string result; |
701 | 0 | intToStr(value, 10, result, false, width, ' '); |
702 | 0 | return result; |
703 | 0 | } |
704 | | |
705 | | |
706 | | inline std::string NumberFormatter::format0(int value, int width) |
707 | 0 | { |
708 | 0 | std::string result; |
709 | 0 | intToStr(value, 10, result, false, width, '0'); |
710 | 0 | return result; |
711 | 0 | } |
712 | | |
713 | | |
714 | | inline std::string NumberFormatter::formatHex(int value, Options options) |
715 | 0 | { |
716 | 0 | return formatHex(static_cast<unsigned int>(value), options); |
717 | 0 | } |
718 | | |
719 | | |
720 | | inline std::string NumberFormatter::formatHex(int value, int width, Options options) |
721 | 494 | { |
722 | 494 | return formatHex(static_cast<unsigned int>(value), width, options); |
723 | 494 | } |
724 | | |
725 | | |
726 | | inline std::string NumberFormatter::format(unsigned value) |
727 | 0 | { |
728 | 0 | std::string result; |
729 | 0 | intToStr(value, 10, result); |
730 | 0 | return result; |
731 | 0 | } |
732 | | |
733 | | |
734 | | inline std::string NumberFormatter::format(unsigned value, int width) |
735 | 0 | { |
736 | 0 | std::string result; |
737 | 0 | intToStr(value, 10, result, false, width, ' '); |
738 | 0 | return result; |
739 | 0 | } |
740 | | |
741 | | |
742 | | inline std::string NumberFormatter::format0(unsigned int value, int width) |
743 | 0 | { |
744 | 0 | std::string result; |
745 | 0 | intToStr(value, 10, result, false, width, '0'); |
746 | 0 | return result; |
747 | 0 | } |
748 | | |
749 | | |
750 | | inline std::string NumberFormatter::formatHex(unsigned value, Options options) |
751 | 0 | { |
752 | 0 | std::string result; |
753 | 0 | intToStr(value, 0x10, result, isEnabled(options, Options::HEX_PREFIX), -1, ' ', 0, isEnabled(options, Options::HEX_LOWERCASE)); |
754 | 0 | return result; |
755 | 0 | } |
756 | | |
757 | | |
758 | | inline std::string NumberFormatter::formatHex(unsigned value, int width, Options options) |
759 | 389M | { |
760 | 389M | std::string result; |
761 | 389M | intToStr(value, 0x10, result, isEnabled(options, Options::HEX_PREFIX), width, '0', 0, isEnabled(options, Options::HEX_LOWERCASE)); |
762 | 389M | return result; |
763 | 389M | } |
764 | | |
765 | | |
766 | | inline std::string NumberFormatter::format(long value) |
767 | 329k | { |
768 | 329k | std::string result; |
769 | 329k | intToStr(value, 10, result); |
770 | 329k | return result; |
771 | 329k | } |
772 | | |
773 | | |
774 | | inline std::string NumberFormatter::format(long value, int width) |
775 | 0 | { |
776 | 0 | std::string result; |
777 | 0 | intToStr(value, 10, result, false, width, ' '); |
778 | 0 | return result; |
779 | 0 | } |
780 | | |
781 | | |
782 | | inline std::string NumberFormatter::format0(long value, int width) |
783 | 0 | { |
784 | 0 | std::string result; |
785 | 0 | intToStr(value, 10, result, false, width, '0'); |
786 | 0 | return result; |
787 | 0 | } |
788 | | |
789 | | |
790 | | inline std::string NumberFormatter::formatHex(long value, Options options) |
791 | 0 | { |
792 | 0 | return formatHex(static_cast<unsigned long>(value), options); |
793 | 0 | } |
794 | | |
795 | | |
796 | | inline std::string NumberFormatter::formatHex(long value, int width, Options options) |
797 | 0 | { |
798 | 0 | return formatHex(static_cast<unsigned long>(value), width, options); |
799 | 0 | } |
800 | | |
801 | | |
802 | | inline std::string NumberFormatter::format(unsigned long value) |
803 | 5.79k | { |
804 | 5.79k | std::string result; |
805 | 5.79k | intToStr(value, 10, result); |
806 | 5.79k | return result; |
807 | 5.79k | } |
808 | | |
809 | | |
810 | | inline std::string NumberFormatter::format(unsigned long value, int width) |
811 | 0 | { |
812 | 0 | std::string result; |
813 | 0 | intToStr(value, 10, result, false, width, ' '); |
814 | 0 | return result; |
815 | 0 | } |
816 | | |
817 | | |
818 | | inline std::string NumberFormatter::format0(unsigned long value, int width) |
819 | 0 | { |
820 | 0 | std::string result; |
821 | 0 | intToStr(value, 10, result, false, width, '0'); |
822 | 0 | return result; |
823 | 0 | } |
824 | | |
825 | | |
826 | | inline std::string NumberFormatter::formatHex(unsigned long value, Options options) |
827 | 0 | { |
828 | 0 | std::string result; |
829 | 0 | intToStr(value, 0x10, result, isEnabled(options, Options::HEX_PREFIX), -1, ' ', 0, isEnabled(options, Options::HEX_LOWERCASE)); |
830 | 0 | return result; |
831 | 0 | } |
832 | | |
833 | | |
834 | | inline std::string NumberFormatter::formatHex(unsigned long value, int width, Options options) |
835 | 0 | { |
836 | 0 | std::string result; |
837 | 0 | intToStr(value, 0x10, result, isEnabled(options, Options::HEX_PREFIX), width, '0', 0, isEnabled(options, Options::HEX_LOWERCASE)); |
838 | 0 | return result; |
839 | 0 | } |
840 | | |
841 | | #ifdef POCO_HAVE_INT64 |
842 | | #ifdef POCO_INT64_IS_LONG |
843 | | |
844 | | |
845 | | inline std::string NumberFormatter::format(long long value) |
846 | 0 | { |
847 | 0 | std::string result; |
848 | 0 | intToStr(value, 10, result); |
849 | 0 | return result; |
850 | 0 | } |
851 | | |
852 | | |
853 | | inline std::string NumberFormatter::format(long long value, int width) |
854 | 0 | { |
855 | 0 | std::string result; |
856 | 0 | intToStr(value, 10, result, false, width, ' '); |
857 | 0 | return result; |
858 | 0 | } |
859 | | |
860 | | |
861 | | inline std::string NumberFormatter::format0(long long value, int width) |
862 | 0 | { |
863 | 0 | std::string result; |
864 | 0 | intToStr(value, 10, result, false, width, '0'); |
865 | 0 | return result; |
866 | 0 | } |
867 | | |
868 | | |
869 | | inline std::string NumberFormatter::formatHex(long long value, Options options) |
870 | 0 | { |
871 | 0 | return formatHex(static_cast<unsigned long long>(value), options); |
872 | 0 | } |
873 | | |
874 | | |
875 | | inline std::string NumberFormatter::formatHex(long long value, int width, Options options) |
876 | 0 | { |
877 | 0 | return formatHex(static_cast<unsigned long long>(value), width, options); |
878 | 0 | } |
879 | | |
880 | | |
881 | | inline std::string NumberFormatter::format(unsigned long long value) |
882 | 0 | { |
883 | 0 | std::string result; |
884 | 0 | intToStr(value, 10, result); |
885 | 0 | return result; |
886 | 0 | } |
887 | | |
888 | | |
889 | | inline std::string NumberFormatter::format(unsigned long long value, int width) |
890 | 0 | { |
891 | 0 | std::string result; |
892 | 0 | intToStr(value, 10, result, false, width, ' '); |
893 | 0 | return result; |
894 | 0 | } |
895 | | |
896 | | |
897 | | inline std::string NumberFormatter::format0(unsigned long long value, int width) |
898 | 0 | { |
899 | 0 | std::string result; |
900 | 0 | intToStr(value, 10, result, false, width, '0'); |
901 | 0 | return result; |
902 | 0 | } |
903 | | |
904 | | |
905 | | inline std::string NumberFormatter::formatHex(unsigned long long value, Options options) |
906 | 0 | { |
907 | 0 | std::string result; |
908 | 0 | intToStr(value, 0x10, result, isEnabled(options, Options::HEX_PREFIX), -1, ' ', 0, isEnabled(options, Options::HEX_LOWERCASE)); |
909 | 0 | return result; |
910 | 0 | } |
911 | | |
912 | | |
913 | | inline std::string NumberFormatter::formatHex(unsigned long long value, int width, Options options) |
914 | 0 | { |
915 | 0 | std::string result; |
916 | 0 | intToStr(value, 0x10, result, isEnabled(options, Options::HEX_PREFIX), width, '0', 0, isEnabled(options, Options::HEX_LOWERCASE)); |
917 | 0 | return result; |
918 | 0 | } |
919 | | |
920 | | |
921 | | #else // ifndef POCO_LONG_IS_64_BIT |
922 | | |
923 | | |
924 | | inline std::string NumberFormatter::format(Int64 value) |
925 | | { |
926 | | std::string result; |
927 | | intToStr(value, 10, result); |
928 | | return result; |
929 | | } |
930 | | |
931 | | |
932 | | inline std::string NumberFormatter::format(Int64 value, int width) |
933 | | { |
934 | | std::string result; |
935 | | intToStr(value, 10, result, false, width, ' '); |
936 | | return result; |
937 | | } |
938 | | |
939 | | |
940 | | inline std::string NumberFormatter::format0(Int64 value, int width) |
941 | | { |
942 | | std::string result; |
943 | | intToStr(value, 10, result, false, width, '0'); |
944 | | return result; |
945 | | } |
946 | | |
947 | | |
948 | | inline std::string NumberFormatter::formatHex(Int64 value, Options options) |
949 | | { |
950 | | return formatHex(static_cast<UInt64>(value), options); |
951 | | } |
952 | | |
953 | | |
954 | | inline std::string NumberFormatter::formatHex(long long value, int width, Options options) |
955 | | { |
956 | | return formatHex(static_cast<UInt64>(value), width, options); |
957 | | } |
958 | | |
959 | | |
960 | | inline std::string NumberFormatter::format(UInt64 value) |
961 | | { |
962 | | std::string result; |
963 | | intToStr(value, 10, result); |
964 | | return result; |
965 | | } |
966 | | |
967 | | |
968 | | inline std::string NumberFormatter::format(UInt64 value, int width) |
969 | | { |
970 | | std::string result; |
971 | | intToStr(value, 10, result, false, width, ' '); |
972 | | return result; |
973 | | } |
974 | | |
975 | | |
976 | | inline std::string NumberFormatter::format0(UInt64 value, int width) |
977 | | { |
978 | | std::string result; |
979 | | intToStr(value, 10, result, false, width, '0'); |
980 | | return result; |
981 | | } |
982 | | |
983 | | |
984 | | inline std::string NumberFormatter::formatHex(UInt64 value, Options options) |
985 | | { |
986 | | std::string result; |
987 | | intToStr(value, 0x10, result, isEnabled(options, Options::HEX_PREFIX), -1, ' ', 0, isEnabled(options, Options::HEX_LOWERCASE)); |
988 | | return result; |
989 | | } |
990 | | |
991 | | |
992 | | inline std::string NumberFormatter::formatHex(UInt64 value, int width, Options options) |
993 | | { |
994 | | std::string result; |
995 | | intToStr(value, 0x10, result, isEnabled(options, Options::HEX_PREFIX), width, '0', 0, isEnabled(options, Options::HEX_LOWERCASE)); |
996 | | return result; |
997 | | } |
998 | | |
999 | | |
1000 | | #endif // ifdef POCO_INT64_IS_LONG |
1001 | | #endif // ifdef POCO_HAVE_INT64 |
1002 | | |
1003 | | |
1004 | | inline std::string NumberFormatter::format(float value) |
1005 | 0 | { |
1006 | 0 | char buffer[POCO_MAX_FLT_STRING_LEN]; |
1007 | 0 | floatToStr(buffer, POCO_MAX_FLT_STRING_LEN, value); |
1008 | 0 | return std::string(buffer); |
1009 | 0 | } |
1010 | | |
1011 | | |
1012 | | inline std::string NumberFormatter::format(float value, int precision) |
1013 | 0 | { |
1014 | 0 | if (precision < 0) |
1015 | 0 | throw InvalidArgumentException("NumberFormatter::format() requires non-negative precision."); |
1016 | 0 | char buffer[POCO_MAX_FLT_STRING_LEN]; |
1017 | 0 | floatToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision); |
1018 | 0 | return std::string(buffer); |
1019 | 0 | } |
1020 | | |
1021 | | |
1022 | | inline std::string NumberFormatter::format(float value, int width, int precision) |
1023 | 0 | { |
1024 | 0 | if (precision < 0) |
1025 | 0 | throw InvalidArgumentException("NumberFormatter::format() requires non-negative precision."); |
1026 | 0 | std::string result; |
1027 | 0 | floatToFixedStr(result, value, precision, width); |
1028 | 0 | return result; |
1029 | 0 | } |
1030 | | |
1031 | | |
1032 | | inline std::string NumberFormatter::format(double value) |
1033 | 533k | { |
1034 | 533k | char buffer[POCO_MAX_FLT_STRING_LEN]; |
1035 | 533k | doubleToStr(buffer, POCO_MAX_FLT_STRING_LEN, value); |
1036 | 533k | return std::string(buffer); |
1037 | 533k | } |
1038 | | |
1039 | | |
1040 | | inline std::string NumberFormatter::format(double value, int precision) |
1041 | 0 | { |
1042 | 0 | if (precision < 0) |
1043 | 0 | throw InvalidArgumentException("NumberFormatter::format() requires non-negative precision."); |
1044 | 0 | char buffer[POCO_MAX_FLT_STRING_LEN]; |
1045 | 0 | doubleToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision); |
1046 | 0 | return std::string(buffer); |
1047 | 0 | } |
1048 | | |
1049 | | |
1050 | | inline std::string NumberFormatter::format(double value, int width, int precision) |
1051 | 0 | { |
1052 | 0 | if (precision < 0) |
1053 | 0 | throw InvalidArgumentException("NumberFormatter::format() requires non-negative precision."); |
1054 | 0 | std::string result; |
1055 | 0 | doubleToFixedStr(result, value, precision, width); |
1056 | 0 | return result; |
1057 | 0 | } |
1058 | | |
1059 | | |
1060 | | inline std::string NumberFormatter::format(const void* ptr) |
1061 | 0 | { |
1062 | 0 | std::string result; |
1063 | 0 | append(result, ptr); |
1064 | 0 | return result; |
1065 | 0 | } |
1066 | | |
1067 | | |
1068 | | } // namespace Poco |
1069 | | |
1070 | | |
1071 | | #endif // Foundation_NumberFormatter_INCLUDED |