Coverage Report

Created: 2025-08-28 07:02

/src/poco/Foundation/src/NumberFormatter.cpp
Line
Count
Source (jump to first uncovered line)
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
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
16
#define _CRT_SECURE_NO_WARNINGS
17
#endif
18
19
20
#include "Poco/NumberFormatter.h"
21
#include "Poco/MemoryStream.h"
22
#include <iomanip>
23
#if !defined(POCO_NO_LOCALE)
24
#include <locale>
25
#endif
26
#include <cstdio>
27
#include <cinttypes>
28
29
30
#if defined(_MSC_VER) || defined(__MINGW32__)
31
  #define I64_FMT "I64"
32
#elif defined(__APPLE__)
33
  #define I64_FMT "q"
34
#else
35
  #define I64_FMT "ll"
36
#endif
37
38
39
40
namespace Poco {
41
42
43
std::string NumberFormatter::format(bool value, BoolFormat format)
44
0
{
45
0
  switch (format)
46
0
  {
47
0
  case FMT_YES_NO:
48
0
    return value ? "yes" : "no";
49
50
0
  case FMT_ON_OFF:
51
0
    return value ? "on" : "off";
52
53
0
  default: // including FMT_TRUE_FALSE:
54
0
    return value ? "true" : "false";
55
0
  }
56
0
}
57
58
59
void NumberFormatter::append(std::string& str, int value)
60
18.4k
{
61
18.4k
  char result[NF_MAX_INT_STRING_LEN];
62
18.4k
  std::size_t sz = NF_MAX_INT_STRING_LEN;
63
18.4k
  intToStr(value, 10, result, sz);
64
18.4k
  str.append(result, sz);
65
18.4k
}
66
67
68
void NumberFormatter::append(std::string& str, int value, int width)
69
1.95k
{
70
1.95k
  char result[NF_MAX_INT_STRING_LEN];
71
1.95k
  std::size_t sz = NF_MAX_INT_STRING_LEN;
72
1.95k
  intToStr(value, 10, result, sz, false, width);
73
1.95k
  str.append(result, sz);
74
1.95k
}
75
76
77
void NumberFormatter::append0(std::string& str, int value, int width)
78
159k
{
79
159k
  char result[NF_MAX_INT_STRING_LEN];
80
159k
  std::size_t sz = NF_MAX_INT_STRING_LEN;
81
159k
  intToStr(value, 10, result, sz, false, width, '0');
82
159k
  str.append(result, sz);
83
159k
}
84
85
86
void NumberFormatter::appendHex(std::string& str, int value, bool lowercase)
87
0
{
88
0
  char result[NF_MAX_INT_STRING_LEN];
89
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
90
0
  intToStr(static_cast<unsigned int>(value), 0x10, result, sz, false, -1, ' ', 0, lowercase);
91
0
  str.append(result, sz);
92
0
}
93
94
95
void NumberFormatter::appendHex(std::string& str, int value, int width, bool lowercase)
96
3.09M
{
97
3.09M
  char result[NF_MAX_INT_STRING_LEN];
98
3.09M
  std::size_t sz = NF_MAX_INT_STRING_LEN;
99
3.09M
  intToStr(static_cast<unsigned int>(value), 0x10, result, sz, false, width, '0', 0, lowercase);
100
3.09M
  str.append(result, sz);
101
3.09M
}
102
103
104
void NumberFormatter::append(std::string& str, unsigned value)
105
0
{
106
0
  char result[NF_MAX_INT_STRING_LEN];
107
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
108
0
  intToStr(value, 10, result, sz);
109
0
  str.append(result, sz);
110
0
}
111
112
113
void NumberFormatter::append(std::string& str, unsigned value, int width)
114
0
{
115
0
  char result[NF_MAX_INT_STRING_LEN];
116
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
117
0
  intToStr(value, 10, result, sz, false, width);
118
0
  str.append(result, sz);
119
0
}
120
121
122
void NumberFormatter::append0(std::string& str, unsigned int value, int width)
123
0
{
124
0
  char result[NF_MAX_INT_STRING_LEN];
125
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
126
0
  intToStr(value, 10, result, sz, false, width, '0');
127
0
  str.append(result, sz);
128
0
}
129
130
131
void NumberFormatter::appendHex(std::string& str, unsigned value, bool lowercase)
132
0
{
133
0
  char result[NF_MAX_INT_STRING_LEN];
134
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
135
0
  intToStr(value, 0x10, result, sz, false, -1, ' ', 0, lowercase);
136
0
  str.append(result, sz);
137
0
}
138
139
140
void NumberFormatter::appendHex(std::string& str, unsigned value, int width, bool lowercase)
141
882k
{
142
882k
  char result[NF_MAX_INT_STRING_LEN];
143
882k
  std::size_t sz = NF_MAX_INT_STRING_LEN;
144
882k
  intToStr(value, 0x10, result, sz, false, width, '0', 0, lowercase);
145
882k
  str.append(result, sz);
146
882k
}
147
148
149
void NumberFormatter::append(std::string& str, long value)
150
0
{
151
0
  char result[NF_MAX_INT_STRING_LEN];
152
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
153
0
  intToStr(value, 10, result, sz);
154
0
  str.append(result, sz);
155
0
}
156
157
158
void NumberFormatter::append(std::string& str, long value, int width)
159
0
{
160
0
  char result[NF_MAX_INT_STRING_LEN];
161
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
162
0
  intToStr(value, 10, result, sz, false, width);
163
0
  str.append(result, sz);
164
0
}
165
166
167
void NumberFormatter::append0(std::string& str, long value, int width)
168
0
{
169
0
  char result[NF_MAX_INT_STRING_LEN];
170
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
171
0
  intToStr(value, 10, result, sz, false, width, '0');
172
0
  str.append(result, sz);
173
0
}
174
175
176
void NumberFormatter::appendHex(std::string& str, long value, bool lowercase)
177
0
{
178
0
  char result[NF_MAX_INT_STRING_LEN];
179
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
180
0
  intToStr(static_cast<unsigned long>(value), 0x10, result, sz, false, -1, ' ', 0, lowercase);
181
0
  str.append(result, sz);
182
0
}
183
184
185
void NumberFormatter::appendHex(std::string& str, long value, int width, bool lowercase)
186
0
{
187
0
  char result[NF_MAX_INT_STRING_LEN];
188
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
189
0
  intToStr(static_cast<unsigned long>(value), 0x10, result, sz, false, width, '0', 0, lowercase);
190
0
  str.append(result, sz);
191
0
}
192
193
194
void NumberFormatter::append(std::string& str, unsigned long value)
195
0
{
196
0
  char result[NF_MAX_INT_STRING_LEN];
197
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
198
0
  intToStr(value, 10, result, sz);
199
0
  str.append(result, sz);
200
0
}
201
202
203
void NumberFormatter::append(std::string& str, unsigned long value, int width)
204
0
{
205
0
  char result[NF_MAX_INT_STRING_LEN];
206
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
207
0
  intToStr(value, 10, result, sz, false, width, '0');
208
0
  str.append(result, sz);
209
0
}
210
211
212
void NumberFormatter::append0(std::string& str, unsigned long value, int width)
213
0
{
214
0
  char result[NF_MAX_INT_STRING_LEN];
215
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
216
0
  intToStr(value, 10, result, sz, false, width, '0');
217
0
  str.append(result, sz);
218
0
}
219
220
221
void NumberFormatter::appendHex(std::string& str, unsigned long value, bool lowercase)
222
0
{
223
0
  char result[NF_MAX_INT_STRING_LEN];
224
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
225
0
  intToStr(value, 0x10, result, sz, false, -1, ' ', 0, lowercase);
226
0
  str.append(result, sz);
227
0
}
228
229
230
void NumberFormatter::appendHex(std::string& str, unsigned long value, int width, bool lowercase)
231
0
{
232
0
  char result[NF_MAX_INT_STRING_LEN];
233
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
234
0
  intToStr(value, 0x10, result, sz, false, width, '0', 0, lowercase);
235
0
  str.append(result, sz);
236
0
}
237
238
239
#ifdef POCO_HAVE_INT64
240
#ifdef POCO_INT64_IS_LONG
241
242
243
void NumberFormatter::append(std::string& str, long long value)
244
0
{
245
0
  char result[NF_MAX_INT_STRING_LEN];
246
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
247
0
  intToStr(value, 10, result, sz);
248
0
  str.append(result, sz);
249
0
}
250
251
252
void NumberFormatter::append(std::string& str, long long value, int width)
253
0
{
254
0
  char result[NF_MAX_INT_STRING_LEN];
255
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
256
0
  intToStr(value, 10, result, sz, false, width, '0');
257
0
  str.append(result, sz);
258
0
}
259
260
261
void NumberFormatter::append0(std::string& str, long long value, int width)
262
0
{
263
0
  char result[NF_MAX_INT_STRING_LEN];
264
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
265
0
  intToStr(value, 10, result, sz, false, width, '0');
266
0
  str.append(result, sz);
267
0
}
268
269
270
void NumberFormatter::appendHex(std::string& str, long long value, bool lowercase)
271
0
{
272
0
  char result[NF_MAX_INT_STRING_LEN];
273
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
274
0
  intToStr(static_cast<unsigned long long>(value), 0x10, result, sz, false, -1, ' ', 0, lowercase);
275
0
  str.append(result, sz);
276
0
}
277
278
279
void NumberFormatter::appendHex(std::string& str, long long value, int width, bool lowercase)
280
0
{
281
0
  char result[NF_MAX_INT_STRING_LEN];
282
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
283
0
  intToStr(static_cast<unsigned long long>(value), 0x10, result, sz, false, width, '0', 0, lowercase);
284
0
  str.append(result, sz);
285
0
}
286
287
288
void NumberFormatter::append(std::string& str, unsigned long long value)
289
0
{
290
0
  char result[NF_MAX_INT_STRING_LEN];
291
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
292
0
  intToStr(value, 10, result, sz);
293
0
  str.append(result, sz);
294
0
}
295
296
297
void NumberFormatter::append(std::string& str, unsigned long long value, int width)
298
0
{
299
0
  char result[NF_MAX_INT_STRING_LEN];
300
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
301
0
  intToStr(value, 10, result, sz, false, width, '0');
302
0
  str.append(result, sz);
303
0
}
304
305
306
void NumberFormatter::append0(std::string& str, unsigned long long value, int width)
307
0
{
308
0
  char result[NF_MAX_INT_STRING_LEN];
309
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
310
0
  intToStr(value, 10, result, sz, false, width, '0');
311
0
  str.append(result, sz);
312
0
}
313
314
315
void NumberFormatter::appendHex(std::string& str, unsigned long long value, bool lowercase)
316
0
{
317
0
  char result[NF_MAX_INT_STRING_LEN];
318
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
319
0
  intToStr(value, 0x10, result, sz, false, -1, ' ', 0, lowercase);
320
0
  str.append(result, sz);
321
0
}
322
323
324
void NumberFormatter::appendHex(std::string& str, unsigned long long value, int width, bool lowercase)
325
0
{
326
0
  char result[NF_MAX_INT_STRING_LEN];
327
0
  std::size_t sz = NF_MAX_INT_STRING_LEN;
328
0
  intToStr(value, 0x10, result, sz, false, width, '0', 0, lowercase);
329
0
  str.append(result, sz);
330
0
}
331
332
333
#else // ifndef POCO_LONG_IS_64_BIT
334
335
336
void NumberFormatter::append(std::string& str, Int64 value)
337
{
338
  char result[NF_MAX_INT_STRING_LEN];
339
  std::size_t sz = NF_MAX_INT_STRING_LEN;
340
  intToStr(value, 10, result, sz);
341
  str.append(result, sz);
342
}
343
344
345
void NumberFormatter::append(std::string& str, Int64 value, int width)
346
{
347
  char result[NF_MAX_INT_STRING_LEN];
348
  std::size_t sz = NF_MAX_INT_STRING_LEN;
349
  intToStr(value, 10, result, sz, false, width, '0');
350
  str.append(result, sz);
351
}
352
353
354
void NumberFormatter::append0(std::string& str, Int64 value, int width)
355
{
356
  char result[NF_MAX_INT_STRING_LEN];
357
  std::size_t sz = NF_MAX_INT_STRING_LEN;
358
  intToStr(value, 10, result, sz, false, width, '0');
359
  str.append(result, sz);
360
}
361
362
363
void NumberFormatter::appendHex(std::string& str, Int64 value, bool lowercase)
364
{
365
  char result[NF_MAX_INT_STRING_LEN];
366
  std::size_t sz = NF_MAX_INT_STRING_LEN;
367
  intToStr(static_cast<UInt64>(value), 0x10, result, sz, false, -1, ' ', 0, lowercase);
368
  str.append(result, sz);
369
}
370
371
372
void NumberFormatter::appendHex(std::string& str, Int64 value, int width, bool lowercase)
373
{
374
  char result[NF_MAX_INT_STRING_LEN];
375
  std::size_t sz = NF_MAX_INT_STRING_LEN;
376
  intToStr(static_cast<UInt64>(value), 0x10, result, sz, false, width, '0', 0, lowercase);
377
  str.append(result, sz);
378
}
379
380
381
void NumberFormatter::append(std::string& str, UInt64 value)
382
{
383
  char result[NF_MAX_INT_STRING_LEN];
384
  std::size_t sz = NF_MAX_INT_STRING_LEN;
385
  intToStr(value, 10, result, sz);
386
  str.append(result, sz);
387
}
388
389
390
void NumberFormatter::append(std::string& str, UInt64 value, int width)
391
{
392
  char result[NF_MAX_INT_STRING_LEN];
393
  std::size_t sz = NF_MAX_INT_STRING_LEN;
394
  intToStr(value, 10, result, sz, false, width, '0');
395
  str.append(result, sz);
396
}
397
398
399
void NumberFormatter::append0(std::string& str, UInt64 value, int width)
400
{
401
  char result[NF_MAX_INT_STRING_LEN];
402
  std::size_t sz = NF_MAX_INT_STRING_LEN;
403
  intToStr(value, 10, result, sz, false, width, '0');
404
  str.append(result, sz);
405
}
406
407
408
void NumberFormatter::appendHex(std::string& str, UInt64 value, bool lowercase)
409
{
410
  char result[NF_MAX_INT_STRING_LEN];
411
  std::size_t sz = NF_MAX_INT_STRING_LEN;
412
  intToStr(value, 0x10, result, sz, false, -1, ' ', 0, lowercase);
413
  str.append(result, sz);
414
}
415
416
417
void NumberFormatter::appendHex(std::string& str, UInt64 value, int width, bool lowercase)
418
{
419
  char result[NF_MAX_INT_STRING_LEN];
420
  std::size_t sz = NF_MAX_INT_STRING_LEN;
421
  intToStr(value, 0x10, result, sz, false, width, '0', 0, lowercase);
422
  str.append(result, sz);
423
}
424
425
426
#endif // ifdef POCO_INT64_IS_LONG
427
#endif // ifdef POCO_HAVE_INT64
428
429
430
void NumberFormatter::append(std::string& str, float value)
431
0
{
432
0
  char buffer[NF_MAX_FLT_STRING_LEN];
433
0
  floatToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
434
0
  str.append(buffer);
435
0
}
436
437
438
void NumberFormatter::append(std::string& str, float value, int precision)
439
0
{
440
0
  char buffer[NF_MAX_FLT_STRING_LEN];
441
0
  floatToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision);
442
0
  str.append(buffer);
443
0
}
444
445
446
void NumberFormatter::append(std::string& str, float value, int width, int precision)
447
0
{
448
0
  std::string result;
449
0
  str.append(floatToFixedStr(result, value, precision, width));
450
0
}
451
452
453
void NumberFormatter::append(std::string& str, double value)
454
0
{
455
0
  char buffer[NF_MAX_FLT_STRING_LEN];
456
0
  doubleToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
457
0
  str.append(buffer);
458
0
}
459
460
461
void NumberFormatter::append(std::string& str, double value, int precision)
462
0
{
463
0
  char buffer[NF_MAX_FLT_STRING_LEN];
464
0
  doubleToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision);
465
0
  str.append(buffer);
466
0
}
467
468
469
void NumberFormatter::append(std::string& str, double value, int width, int precision)
470
0
{
471
0
  std::string result;
472
0
  str.append(doubleToFixedStr(result, value, precision, width));
473
0
}
474
475
476
void NumberFormatter::append(std::string& str, const void* ptr)
477
0
{
478
0
  char buffer[24];
479
0
#if defined(POCO_PTR_IS_64_BIT)
480
0
  std::snprintf(buffer, sizeof(buffer), "%016" PRIXPTR, (UIntPtr) ptr);
481
#else
482
  std::snprintf(buffer, sizeof(buffer), "%08" PRIXPTR, (UIntPtr) ptr);
483
#endif
484
0
  str.append(buffer);
485
0
}
486
487
488
//
489
// Deprecated functions
490
//
491
492
493
std::string NumberFormatter::formatHex(int value, bool prefix)
494
0
{
495
0
  return formatHex(static_cast<unsigned int>(value), prefix ? Options::PREFIX : Options::DEFAULT);
496
0
}
497
498
499
std::string NumberFormatter::formatHex(int value, int width, bool prefix)
500
0
{
501
0
  return formatHex(static_cast<unsigned int>(value), width, prefix ? Options::PREFIX : Options::DEFAULT);
502
0
}
503
504
505
std::string NumberFormatter::formatHex(unsigned value, bool prefix)
506
0
{
507
0
  return formatHex(value, prefix ? Options::PREFIX : Options::DEFAULT);
508
0
}
509
510
511
std::string NumberFormatter::formatHex(unsigned value, int width, bool prefix)
512
0
{
513
0
  return formatHex(value, width, prefix ? Options::PREFIX : Options::DEFAULT);
514
0
}
515
516
517
std::string NumberFormatter::formatHex(long value, bool prefix)
518
0
{
519
0
  return formatHex(static_cast<unsigned long>(value), prefix ? Options::PREFIX : Options::DEFAULT);
520
0
}
521
522
523
std::string NumberFormatter::formatHex(long value, int width, bool prefix)
524
0
{
525
0
  return formatHex(static_cast<unsigned long>(value), width, prefix ? Options::PREFIX : Options::DEFAULT);
526
0
}
527
528
529
std::string NumberFormatter::formatHex(unsigned long value, bool prefix)
530
0
{
531
0
  return formatHex(value, prefix ? Options::PREFIX : Options::DEFAULT);
532
0
}
533
534
535
std::string NumberFormatter::formatHex(unsigned long value, int width, bool prefix)
536
0
{
537
0
  return formatHex(value, width, prefix ? Options::PREFIX : Options::DEFAULT);
538
0
}
539
540
541
#ifdef POCO_HAVE_INT64
542
#ifdef POCO_INT64_IS_LONG
543
544
545
std::string NumberFormatter::formatHex(long long value, bool prefix)
546
0
{
547
0
  return formatHex(static_cast<unsigned long long>(value), prefix ? Options::PREFIX : Options::DEFAULT);
548
0
}
549
550
551
std::string NumberFormatter::formatHex(long long value, int width, bool prefix)
552
0
{
553
0
  return formatHex(static_cast<unsigned long long>(value), width, prefix ? Options::PREFIX : Options::DEFAULT);
554
0
}
555
556
557
std::string NumberFormatter::formatHex(unsigned long long value, bool prefix)
558
0
{
559
0
  return formatHex(value, prefix ? Options::PREFIX : Options::DEFAULT);
560
0
}
561
562
563
std::string NumberFormatter::formatHex(unsigned long long value, int width, bool prefix)
564
0
{
565
0
  return formatHex(value, width, prefix ? Options::PREFIX : Options::DEFAULT);
566
0
}
567
568
569
#else // ifndef POCO_LONG_IS_64_BIT
570
571
572
std::string NumberFormatter::formatHex(Int64 value, bool prefix)
573
{
574
  return formatHex(static_cast<UInt64>(value), prefix ? Options::PREFIX : Options::DEFAULT);
575
}
576
577
578
std::string NumberFormatter::formatHex(Int64 value, int width, bool prefix)
579
{
580
  return formatHex(static_cast<UInt64>(value), width, prefix ? Options::PREFIX : Options::DEFAULT);
581
}
582
583
584
std::string NumberFormatter::formatHex(UInt64 value, bool prefix)
585
{
586
  return formatHex(value, prefix ? Options::PREFIX : Options::DEFAULT);
587
}
588
589
590
std::string NumberFormatter::formatHex(UInt64 value, int width, bool prefix)
591
{
592
  return formatHex(value, width, prefix ? Options::PREFIX : Options::DEFAULT);
593
}
594
595
596
#endif // ifdef POCO_INT64_IS_LONG
597
#endif // ifdef POCO_HAVE_INT64
598
599
600
} // namespace Poco