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