/src/poco/Foundation/include/Poco/NumericString.h
Line | Count | Source |
1 | | // |
2 | | // NumericString.h |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: Core |
6 | | // Module: NumericString |
7 | | // |
8 | | // Numeric string utility functions. |
9 | | // |
10 | | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef Foundation_NumericString_INCLUDED |
18 | | #define Foundation_NumericString_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/Foundation.h" |
22 | | #include "Poco/Buffer.h" |
23 | | #include "Poco/FPEnvironment.h" |
24 | | #ifdef min |
25 | | #undef min |
26 | | #endif |
27 | | #ifdef max |
28 | | #undef max |
29 | | #endif |
30 | | #include <limits> |
31 | | #include <cmath> |
32 | | #include <cctype> |
33 | | #if !defined(POCO_NO_LOCALE) |
34 | | #include <locale> |
35 | | #endif |
36 | | #include <type_traits> |
37 | | #if defined(POCO_NOINTMAX) |
38 | | typedef Poco::UInt64 uintmax_t; |
39 | | typedef Poco::Int64 intmax_t; |
40 | | #endif |
41 | | #if !defined (INTMAX_MAX) |
42 | | #define INTMAX_MAX std::numeric_limits<intmax_t>::max() |
43 | | #endif |
44 | | #ifdef POCO_COMPILER_MSVC |
45 | | #pragma warning(push) |
46 | | #pragma warning(disable : 4146) |
47 | | #endif // POCO_COMPILER_MSVC |
48 | | |
49 | | // binary numbers are supported, thus 64 (bits) + 1 (string terminating zero) + 2 (hex prefix) |
50 | 361M | #define POCO_MAX_INT_STRING_LEN (67) |
51 | | // value from strtod.cc (double_conversion::kMaxSignificantDecimalDigits) |
52 | 828k | #define POCO_MAX_FLT_STRING_LEN 780 |
53 | | |
54 | 828k | #define POCO_FLT_INF "inf" |
55 | 828k | #define POCO_FLT_NAN "nan" |
56 | 828k | #define POCO_FLT_EXP 'e' |
57 | | |
58 | | |
59 | | namespace Poco { |
60 | | |
61 | | |
62 | | namespace Impl { |
63 | | |
64 | | template<bool SIGNED, typename T> |
65 | | class IsNegativeImpl; |
66 | | |
67 | | template<typename T> |
68 | | class IsNegativeImpl<true, T> |
69 | | { |
70 | | public: |
71 | | bool operator()(T x) { return x < 0; } |
72 | | }; |
73 | | |
74 | | template<typename T> |
75 | | class IsNegativeImpl<false, T> |
76 | | { |
77 | | public: |
78 | | bool operator()(T) { return false; } |
79 | | }; |
80 | | |
81 | | } |
82 | | |
83 | | |
84 | | template<typename T> |
85 | | inline bool isNegative(T x) |
86 | | { |
87 | | using namespace Impl; |
88 | | return IsNegativeImpl<std::numeric_limits<T>::is_signed, T>()(x); |
89 | | } |
90 | | |
91 | | |
92 | | template<typename To, typename From> |
93 | | inline bool isIntOverflow(From val) |
94 | | { |
95 | | poco_assert_dbg (std::numeric_limits<From>::is_integer); |
96 | | poco_assert_dbg (std::numeric_limits<To>::is_integer); |
97 | | bool ret; |
98 | | if (std::numeric_limits<To>::is_signed) |
99 | | { |
100 | | ret = (!std::numeric_limits<From>::is_signed && |
101 | | (uintmax_t)val > (uintmax_t)INTMAX_MAX) || |
102 | | (intmax_t)val < (intmax_t)std::numeric_limits<To>::min() || |
103 | | (intmax_t)val > (intmax_t)std::numeric_limits<To>::max(); |
104 | | } |
105 | | else |
106 | | { |
107 | | ret = isNegative(val) || |
108 | | (uintmax_t)val > (uintmax_t)std::numeric_limits<To>::max(); |
109 | | } |
110 | | return ret; |
111 | | } |
112 | | |
113 | | |
114 | | template<typename R, typename F, typename S> |
115 | | bool safeMultiply(R& result, F f, S s) |
116 | 4.38M | { |
117 | 4.38M | if ((f == 0) || (s==0)) |
118 | 2.21M | { |
119 | 2.21M | result = 0; |
120 | 2.21M | return true; |
121 | 2.21M | } |
122 | | |
123 | 2.17M | if (f > 0) |
124 | 2.17M | { |
125 | 2.17M | if (s > 0) |
126 | 2.17M | { |
127 | 2.17M | if (f > (std::numeric_limits<R>::max() / s)) |
128 | 0 | return false; |
129 | 2.17M | } |
130 | 0 | else |
131 | 0 | { |
132 | 0 | if (s < (std::numeric_limits<R>::min() / f)) |
133 | 0 | return false; |
134 | 0 | } |
135 | 2.17M | } |
136 | 0 | else |
137 | 0 | { |
138 | 0 | if (s > 0) |
139 | 0 | { |
140 | 0 | if (f < (std::numeric_limits<R>::min() / s)) |
141 | 0 | return false; |
142 | 0 | } |
143 | 0 | else |
144 | 0 | { |
145 | 0 | if (s < (std::numeric_limits<R>::max() / f)) |
146 | 0 | return false; |
147 | 0 | } |
148 | 0 | } |
149 | 2.17M | result = f * s; |
150 | 2.17M | return true; |
151 | 2.17M | } |
152 | | |
153 | | |
154 | | template <typename F, typename T> |
155 | | inline T& isSafeIntCast(F from) |
156 | | /// Returns true if it is safe to cast |
157 | | /// integer from F to T. |
158 | | { |
159 | | if (!isIntOverflow<T, F>(from)) return true; |
160 | | return false; |
161 | | } |
162 | | |
163 | | |
164 | | template <typename F, typename T> |
165 | | inline T& safeIntCast(F from, T& to) |
166 | | /// Returns cast value if it is safe |
167 | | /// to cast integer from F to T, |
168 | | /// otherwise throws BadCastException. |
169 | | { |
170 | | if (!isIntOverflow<T, F>(from)) |
171 | | { |
172 | | to = static_cast<T>(from); |
173 | | return to; |
174 | | } |
175 | | throw BadCastException("safeIntCast: Integer overflow"); |
176 | | } |
177 | | |
178 | | inline char decimalSeparator() |
179 | | /// Returns decimal separator from global locale or |
180 | | /// default '.' for platforms where locale is unavailable. |
181 | 0 | { |
182 | 0 | #if !defined(POCO_NO_LOCALE) |
183 | 0 | return std::use_facet<std::numpunct<char>>(std::locale()).decimal_point(); |
184 | 0 | #else |
185 | 0 | return '.'; |
186 | 0 | #endif |
187 | 0 | } |
188 | | |
189 | | |
190 | | inline char thousandSeparator() |
191 | | /// Returns thousand separator from global locale or |
192 | | /// default ',' for platforms where locale is unavailable. |
193 | 0 | { |
194 | 0 | #if !defined(POCO_NO_LOCALE) |
195 | 0 | return std::use_facet<std::numpunct<char>>(std::locale()).thousands_sep(); |
196 | 0 | #else |
197 | 0 | return ','; |
198 | 0 | #endif |
199 | 0 | } |
200 | | |
201 | | |
202 | | // |
203 | | // String to Number Conversions |
204 | | // |
205 | | |
206 | | template <typename I> |
207 | | bool strToInt(const char* pStr, I& outResult, short base, char thSep = ',') |
208 | | /// Converts zero-terminated character array to integer number; |
209 | | /// Thousand separators are recognized for base10 and current locale; |
210 | | /// they are silently skipped and not verified for correct positioning. |
211 | | /// It is not allowed to convert a negative number to anything except |
212 | | /// 10-base signed integer. |
213 | | /// For hexadecimal numbers, the case of the digits is not relevant. |
214 | | /// |
215 | | /// Function returns true if successful. If parsing was unsuccessful, |
216 | | /// the return value is false with the result value undetermined. |
217 | 2.21M | { |
218 | 2.21M | poco_assert_dbg (base == 2 || base == 8 || base == 10 || base == 16); |
219 | | |
220 | 2.21M | if (!pStr) return false; |
221 | 2.21M | while (std::isspace(*pStr)) ++pStr; |
222 | 2.21M | if ((*pStr == '\0') || ((*pStr == '-') && ((base != 10) || (std::is_unsigned<I>::value)))) |
223 | 164 | return false; |
224 | | |
225 | 2.21M | bool negative = false; |
226 | 2.21M | if ((base == 10) && (*pStr == '-')) |
227 | 40.0k | { |
228 | 40.0k | if (!std::numeric_limits<I>::is_signed) return false; |
229 | 40.0k | negative = true; |
230 | 40.0k | ++pStr; |
231 | 40.0k | } |
232 | 2.17M | else if (*pStr == '+') ++pStr; |
233 | | |
234 | | // numbers are parsed as unsigned, for negative numbers the sign is applied after parsing |
235 | | // overflow is checked in every parse step |
236 | 2.21M | uintmax_t limitCheck = std::numeric_limits<I>::max(); |
237 | 2.21M | if (negative) ++limitCheck; |
238 | 2.21M | uintmax_t result = 0; |
239 | 2.21M | unsigned char add = 0; |
240 | 6.59M | for (; *pStr != '\0'; ++pStr) |
241 | 4.40M | { |
242 | 4.40M | if (*pStr == thSep) |
243 | 1.04k | { |
244 | 1.04k | if (base == 10) continue; |
245 | | // thousand separators only allowed for base 10 |
246 | 0 | return false; |
247 | 1.04k | } |
248 | 4.40M | if (result > (limitCheck / base)) return false; |
249 | 4.38M | if (!safeMultiply(result, result, base)) return false; |
250 | 4.38M | switch (*pStr) |
251 | 4.38M | { |
252 | 3.66M | case '0': case '1': case '2': case '3': |
253 | 4.22M | case '4': case '5': case '6': case '7': |
254 | 4.22M | add = (*pStr - '0'); |
255 | 4.22M | break; |
256 | | |
257 | 148k | case '8': case '9': |
258 | 148k | if ((base == 10) || (base == 0x10)) add = (*pStr - '0'); |
259 | 0 | else return false; |
260 | 148k | break; |
261 | | |
262 | 148k | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': |
263 | 5.58k | if (base != 0x10) return false; |
264 | 3.09k | add = (*pStr - 'a') + 10; |
265 | 3.09k | break; |
266 | | |
267 | 3.22k | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': |
268 | 3.22k | if (base != 0x10) return false; |
269 | 1.45k | add = (*pStr - 'A') + 10; |
270 | 1.45k | break; |
271 | | |
272 | 6.07k | default: |
273 | 6.07k | return false; |
274 | 4.38M | } |
275 | 4.37M | if ((limitCheck - static_cast<uintmax_t>(result)) < add) return false; |
276 | 4.37M | result += add; |
277 | 4.37M | } |
278 | | |
279 | 2.18M | if (negative && (base == 10)) |
280 | 39.3k | outResult = static_cast<I>(-result); |
281 | 2.14M | else |
282 | 2.14M | outResult = static_cast<I>(result); |
283 | | |
284 | 2.18M | return true; |
285 | 2.21M | } bool Poco::strToInt<int>(char const*, int&, short, char) Line | Count | Source | 217 | 20.4k | { | 218 | 20.4k | poco_assert_dbg (base == 2 || base == 8 || base == 10 || base == 16); | 219 | | | 220 | 20.4k | if (!pStr) return false; | 221 | 20.8k | while (std::isspace(*pStr)) ++pStr; | 222 | 20.4k | if ((*pStr == '\0') || ((*pStr == '-') && ((base != 10) || (std::is_unsigned<I>::value)))) | 223 | 151 | return false; | 224 | | | 225 | 20.3k | bool negative = false; | 226 | 20.3k | if ((base == 10) && (*pStr == '-')) | 227 | 1.82k | { | 228 | 1.82k | if (!std::numeric_limits<I>::is_signed) return false; | 229 | 1.82k | negative = true; | 230 | 1.82k | ++pStr; | 231 | 1.82k | } | 232 | 18.4k | else if (*pStr == '+') ++pStr; | 233 | | | 234 | | // numbers are parsed as unsigned, for negative numbers the sign is applied after parsing | 235 | | // overflow is checked in every parse step | 236 | 20.3k | uintmax_t limitCheck = std::numeric_limits<I>::max(); | 237 | 20.3k | if (negative) ++limitCheck; | 238 | 20.3k | uintmax_t result = 0; | 239 | 20.3k | unsigned char add = 0; | 240 | 50.2k | for (; *pStr != '\0'; ++pStr) | 241 | 40.7k | { | 242 | 40.7k | if (*pStr == thSep) | 243 | 1.04k | { | 244 | 1.04k | if (base == 10) continue; | 245 | | // thousand separators only allowed for base 10 | 246 | 0 | return false; | 247 | 1.04k | } | 248 | 39.6k | if (result > (limitCheck / base)) return false; | 249 | 39.3k | if (!safeMultiply(result, result, base)) return false; | 250 | 39.3k | switch (*pStr) | 251 | 39.3k | { | 252 | 14.6k | case '0': case '1': case '2': case '3': | 253 | 26.1k | case '4': case '5': case '6': case '7': | 254 | 26.1k | add = (*pStr - '0'); | 255 | 26.1k | break; | 256 | | | 257 | 2.87k | case '8': case '9': | 258 | 2.87k | if ((base == 10) || (base == 0x10)) add = (*pStr - '0'); | 259 | 0 | else return false; | 260 | 2.87k | break; | 261 | | | 262 | 2.87k | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | 263 | 2.49k | if (base != 0x10) return false; | 264 | 0 | add = (*pStr - 'a') + 10; | 265 | 0 | break; | 266 | | | 267 | 1.76k | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | 268 | 1.76k | if (base != 0x10) return false; | 269 | 0 | add = (*pStr - 'A') + 10; | 270 | 0 | break; | 271 | | | 272 | 6.07k | default: | 273 | 6.07k | return false; | 274 | 39.3k | } | 275 | 28.9k | if ((limitCheck - static_cast<uintmax_t>(result)) < add) return false; | 276 | 28.9k | result += add; | 277 | 28.9k | } | 278 | | | 279 | 9.53k | if (negative && (base == 10)) | 280 | 1.13k | outResult = static_cast<I>(-result); | 281 | 8.40k | else | 282 | 8.40k | outResult = static_cast<I>(result); | 283 | | | 284 | 9.53k | return true; | 285 | 20.3k | } |
bool Poco::strToInt<unsigned int>(char const*, unsigned int&, short, char) Line | Count | Source | 217 | 4.09k | { | 218 | 4.09k | poco_assert_dbg (base == 2 || base == 8 || base == 10 || base == 16); | 219 | | | 220 | 4.09k | if (!pStr) return false; | 221 | 4.09k | while (std::isspace(*pStr)) ++pStr; | 222 | 4.09k | if ((*pStr == '\0') || ((*pStr == '-') && ((base != 10) || (std::is_unsigned<I>::value)))) | 223 | 0 | return false; | 224 | | | 225 | 4.09k | bool negative = false; | 226 | 4.09k | if ((base == 10) && (*pStr == '-')) | 227 | 0 | { | 228 | 0 | if (!std::numeric_limits<I>::is_signed) return false; | 229 | 0 | negative = true; | 230 | 0 | ++pStr; | 231 | 0 | } | 232 | 4.09k | else if (*pStr == '+') ++pStr; | 233 | | | 234 | | // numbers are parsed as unsigned, for negative numbers the sign is applied after parsing | 235 | | // overflow is checked in every parse step | 236 | 4.09k | uintmax_t limitCheck = std::numeric_limits<I>::max(); | 237 | 4.09k | if (negative) ++limitCheck; | 238 | 4.09k | uintmax_t result = 0; | 239 | 4.09k | unsigned char add = 0; | 240 | 12.2k | for (; *pStr != '\0'; ++pStr) | 241 | 8.18k | { | 242 | 8.18k | if (*pStr == thSep) | 243 | 0 | { | 244 | 0 | if (base == 10) continue; | 245 | | // thousand separators only allowed for base 10 | 246 | 0 | return false; | 247 | 0 | } | 248 | 8.18k | if (result > (limitCheck / base)) return false; | 249 | 8.18k | if (!safeMultiply(result, result, base)) return false; | 250 | 8.18k | switch (*pStr) | 251 | 8.18k | { | 252 | 1.78k | case '0': case '1': case '2': case '3': | 253 | 3.02k | case '4': case '5': case '6': case '7': | 254 | 3.02k | add = (*pStr - '0'); | 255 | 3.02k | break; | 256 | | | 257 | 604 | case '8': case '9': | 258 | 604 | if ((base == 10) || (base == 0x10)) add = (*pStr - '0'); | 259 | 0 | else return false; | 260 | 604 | break; | 261 | | | 262 | 3.09k | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | 263 | 3.09k | if (base != 0x10) return false; | 264 | 3.09k | add = (*pStr - 'a') + 10; | 265 | 3.09k | break; | 266 | | | 267 | 1.45k | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | 268 | 1.45k | if (base != 0x10) return false; | 269 | 1.45k | add = (*pStr - 'A') + 10; | 270 | 1.45k | break; | 271 | | | 272 | 0 | default: | 273 | 0 | return false; | 274 | 8.18k | } | 275 | 8.18k | if ((limitCheck - static_cast<uintmax_t>(result)) < add) return false; | 276 | 8.18k | result += add; | 277 | 8.18k | } | 278 | | | 279 | 4.09k | if (negative && (base == 10)) | 280 | 0 | outResult = static_cast<I>(-result); | 281 | 4.09k | else | 282 | 4.09k | outResult = static_cast<I>(result); | 283 | | | 284 | 4.09k | return true; | 285 | 4.09k | } |
bool Poco::strToInt<long>(char const*, long&, short, char) Line | Count | Source | 217 | 2.17M | { | 218 | 2.17M | poco_assert_dbg (base == 2 || base == 8 || base == 10 || base == 16); | 219 | | | 220 | 2.17M | if (!pStr) return false; | 221 | 2.17M | while (std::isspace(*pStr)) ++pStr; | 222 | 2.17M | if ((*pStr == '\0') || ((*pStr == '-') && ((base != 10) || (std::is_unsigned<I>::value)))) | 223 | 0 | return false; | 224 | | | 225 | 2.17M | bool negative = false; | 226 | 2.17M | if ((base == 10) && (*pStr == '-')) | 227 | 38.2k | { | 228 | 38.2k | if (!std::numeric_limits<I>::is_signed) return false; | 229 | 38.2k | negative = true; | 230 | 38.2k | ++pStr; | 231 | 38.2k | } | 232 | 2.13M | else if (*pStr == '+') ++pStr; | 233 | | | 234 | | // numbers are parsed as unsigned, for negative numbers the sign is applied after parsing | 235 | | // overflow is checked in every parse step | 236 | 2.17M | uintmax_t limitCheck = std::numeric_limits<I>::max(); | 237 | 2.17M | if (negative) ++limitCheck; | 238 | 2.17M | uintmax_t result = 0; | 239 | 2.17M | unsigned char add = 0; | 240 | 6.19M | for (; *pStr != '\0'; ++pStr) | 241 | 4.04M | { | 242 | 4.04M | if (*pStr == thSep) | 243 | 0 | { | 244 | 0 | if (base == 10) continue; | 245 | | // thousand separators only allowed for base 10 | 246 | 0 | return false; | 247 | 0 | } | 248 | 4.04M | if (result > (limitCheck / base)) return false; | 249 | 4.02M | if (!safeMultiply(result, result, base)) return false; | 250 | 4.02M | switch (*pStr) | 251 | 4.02M | { | 252 | 3.43M | case '0': case '1': case '2': case '3': | 253 | 3.91M | case '4': case '5': case '6': case '7': | 254 | 3.91M | add = (*pStr - '0'); | 255 | 3.91M | break; | 256 | | | 257 | 111k | case '8': case '9': | 258 | 111k | if ((base == 10) || (base == 0x10)) add = (*pStr - '0'); | 259 | 0 | else return false; | 260 | 111k | break; | 261 | | | 262 | 111k | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | 263 | 0 | if (base != 0x10) return false; | 264 | 0 | add = (*pStr - 'a') + 10; | 265 | 0 | break; | 266 | | | 267 | 0 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | 268 | 0 | if (base != 0x10) return false; | 269 | 0 | add = (*pStr - 'A') + 10; | 270 | 0 | break; | 271 | | | 272 | 0 | default: | 273 | 0 | return false; | 274 | 4.02M | } | 275 | 4.02M | if ((limitCheck - static_cast<uintmax_t>(result)) < add) return false; | 276 | 4.02M | result += add; | 277 | 4.02M | } | 278 | | | 279 | 2.15M | if (negative && (base == 10)) | 280 | 38.2k | outResult = static_cast<I>(-result); | 281 | 2.11M | else | 282 | 2.11M | outResult = static_cast<I>(result); | 283 | | | 284 | 2.15M | return true; | 285 | 2.17M | } |
bool Poco::strToInt<unsigned long>(char const*, unsigned long&, short, char) Line | Count | Source | 217 | 16.1k | { | 218 | 16.1k | poco_assert_dbg (base == 2 || base == 8 || base == 10 || base == 16); | 219 | | | 220 | 16.1k | if (!pStr) return false; | 221 | 16.1k | while (std::isspace(*pStr)) ++pStr; | 222 | 16.1k | if ((*pStr == '\0') || ((*pStr == '-') && ((base != 10) || (std::is_unsigned<I>::value)))) | 223 | 13 | return false; | 224 | | | 225 | 16.0k | bool negative = false; | 226 | 16.0k | if ((base == 10) && (*pStr == '-')) | 227 | 0 | { | 228 | 0 | if (!std::numeric_limits<I>::is_signed) return false; | 229 | 0 | negative = true; | 230 | 0 | ++pStr; | 231 | 0 | } | 232 | 16.0k | else if (*pStr == '+') ++pStr; | 233 | | | 234 | | // numbers are parsed as unsigned, for negative numbers the sign is applied after parsing | 235 | | // overflow is checked in every parse step | 236 | 16.0k | uintmax_t limitCheck = std::numeric_limits<I>::max(); | 237 | 16.0k | if (negative) ++limitCheck; | 238 | 16.0k | uintmax_t result = 0; | 239 | 16.0k | unsigned char add = 0; | 240 | 332k | for (; *pStr != '\0'; ++pStr) | 241 | 316k | { | 242 | 316k | if (*pStr == thSep) | 243 | 0 | { | 244 | 0 | if (base == 10) continue; | 245 | | // thousand separators only allowed for base 10 | 246 | 0 | return false; | 247 | 0 | } | 248 | 316k | if (result > (limitCheck / base)) return false; | 249 | 316k | if (!safeMultiply(result, result, base)) return false; | 250 | 316k | switch (*pStr) | 251 | 316k | { | 252 | 218k | case '0': case '1': case '2': case '3': | 253 | 283k | case '4': case '5': case '6': case '7': | 254 | 283k | add = (*pStr - '0'); | 255 | 283k | break; | 256 | | | 257 | 32.7k | case '8': case '9': | 258 | 32.7k | if ((base == 10) || (base == 0x10)) add = (*pStr - '0'); | 259 | 0 | else return false; | 260 | 32.7k | break; | 261 | | | 262 | 32.7k | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | 263 | 0 | if (base != 0x10) return false; | 264 | 0 | add = (*pStr - 'a') + 10; | 265 | 0 | break; | 266 | | | 267 | 0 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | 268 | 0 | if (base != 0x10) return false; | 269 | 0 | add = (*pStr - 'A') + 10; | 270 | 0 | break; | 271 | | | 272 | 0 | default: | 273 | 0 | return false; | 274 | 316k | } | 275 | 316k | if ((limitCheck - static_cast<uintmax_t>(result)) < add) return false; | 276 | 316k | result += add; | 277 | 316k | } | 278 | | | 279 | 15.3k | if (negative && (base == 10)) | 280 | 0 | outResult = static_cast<I>(-result); | 281 | 15.3k | else | 282 | 15.3k | outResult = static_cast<I>(result); | 283 | | | 284 | 15.3k | return true; | 285 | 16.0k | } |
|
286 | | |
287 | | |
288 | | template <typename I> |
289 | | bool strToInt(const std::string& str, I& result, short base, char thSep = ',') |
290 | | /// Converts string to integer number; |
291 | | /// This is a wrapper function, for details see see the |
292 | | /// bool strToInt(const char*, I&, short, char) implementation. |
293 | | { |
294 | | return strToInt(str.c_str(), result, base, thSep); |
295 | | } |
296 | | |
297 | | |
298 | | // |
299 | | // Number to String Conversions |
300 | | // |
301 | | |
302 | | namespace Impl { |
303 | | |
304 | | class Ptr |
305 | | /// Utility char pointer wrapper class. |
306 | | /// Class ensures increment/decrement remain within boundaries. |
307 | | { |
308 | | public: |
309 | 366M | Ptr(char* ptr, std::size_t offset): _beg(ptr), _cur(ptr), _end(ptr + offset) |
310 | 366M | { |
311 | 366M | } |
312 | | |
313 | | char*& operator ++ () // prefix |
314 | 0 | { |
315 | 0 | checkBounds(_cur + 1); |
316 | 0 | return ++_cur; |
317 | 0 | } |
318 | | |
319 | | char* operator ++ (int) // postfix |
320 | 739M | { |
321 | 739M | checkBounds(_cur + 1); |
322 | 739M | char* tmp = _cur++; |
323 | 739M | return tmp; |
324 | 739M | } |
325 | | |
326 | | char*& operator -- () // prefix |
327 | 0 | { |
328 | 0 | checkBounds(_cur - 1); |
329 | 0 | return --_cur; |
330 | 0 | } |
331 | | |
332 | | char* operator -- (int) // postfix |
333 | 735M | { |
334 | 735M | checkBounds(_cur - 1); |
335 | 735M | char* tmp = _cur--; |
336 | 735M | return tmp; |
337 | 735M | } |
338 | | |
339 | | char*& operator += (int incr) |
340 | 0 | { |
341 | 0 | checkBounds(_cur + incr); |
342 | 0 | return _cur += incr; |
343 | 0 | } |
344 | | |
345 | | char*& operator -= (int decr) |
346 | 0 | { |
347 | 0 | checkBounds(_cur - decr); |
348 | 0 | return _cur -= decr; |
349 | 0 | } |
350 | | |
351 | | operator char* () const |
352 | 1.93G | { |
353 | 1.93G | return _cur; |
354 | 1.93G | } |
355 | | |
356 | | std::size_t span() const |
357 | 0 | { |
358 | 0 | return _end - _beg; |
359 | 0 | } |
360 | | |
361 | | private: |
362 | | void checkBounds(char* ptr) |
363 | 1.47G | { |
364 | 1.47G | if (ptr > _end) throw RangeException(); |
365 | 1.47G | } |
366 | | |
367 | | const char* _beg; |
368 | | char* _cur; |
369 | | const char* _end; |
370 | | }; |
371 | | |
372 | | template <typename T> |
373 | | using EnableSigned = typename std::enable_if_t< std::is_signed<T>::value >*; |
374 | | |
375 | | template <typename T> |
376 | | using EnableUnsigned = typename std::enable_if_t< std::is_unsigned<T>::value >*; |
377 | | |
378 | | } // namespace Impl |
379 | | |
380 | | |
381 | | template <typename T, Impl::EnableSigned<T> = nullptr> |
382 | | bool intToStr(T value, |
383 | | unsigned short base, |
384 | | char* result, |
385 | | std::size_t& size, |
386 | | bool prefix = false, |
387 | | int width = -1, |
388 | | char fill = ' ', |
389 | | char thSep = 0, |
390 | | bool lowercase = false) |
391 | | /// Converts signed integer to string. Standard numeric bases from binary to hexadecimal |
392 | | /// are supported. |
393 | | /// If width is non-zero, it pads the return value with fill character to the specified width. |
394 | | /// When padding is zero character ('0'), it is prepended to the number itself; all other |
395 | | /// paddings are prepended to the formatted result with minus sign or base prefix included |
396 | | /// If prefix is true and base is octal or hexadecimal, respective prefix ('0' for octal, |
397 | | /// "0x" for hexadecimal) is prepended. For all other bases, prefix argument is ignored. |
398 | | /// Formatted string has at least [width] total length. |
399 | 735k | { |
400 | 735k | poco_assert_dbg (((value < 0) && (base == 10)) || (value >= 0)); |
401 | | |
402 | 735k | if (base < 2 || base > 0x10) |
403 | 0 | { |
404 | 0 | *result = '\0'; |
405 | 0 | return false; |
406 | 0 | } |
407 | | |
408 | 735k | Impl::Ptr ptr(result, size); |
409 | 735k | int thCount = 0; |
410 | 735k | T tmpVal; |
411 | 735k | do |
412 | 2.14M | { |
413 | 2.14M | tmpVal = value; |
414 | 2.14M | value /= base; |
415 | 2.14M | *ptr++ = (lowercase ? "fedcba9876543210123456789abcdef" : "FEDCBA9876543210123456789ABCDEF")[15 + (tmpVal - value * base)]; |
416 | 2.14M | if (thSep && (base == 10) && (++thCount == 3)) |
417 | 0 | { |
418 | 0 | *ptr++ = thSep; |
419 | 0 | thCount = 0; |
420 | 0 | } |
421 | 2.14M | } while (value); |
422 | | |
423 | 735k | if ('0' == fill) |
424 | 153k | { |
425 | 153k | if (tmpVal < 0) --width; |
426 | 153k | if (prefix && base == 010) --width; |
427 | 153k | if (prefix && base == 0x10) width -= 2; |
428 | 216k | while ((ptr - result) < width) *ptr++ = fill; |
429 | 153k | } |
430 | | |
431 | 735k | if (prefix && base == 010) *ptr++ = '0'; |
432 | 735k | else if (prefix && base == 0x10) |
433 | 0 | { |
434 | 0 | *ptr++ = 'x'; |
435 | 0 | *ptr++ = '0'; |
436 | 0 | } |
437 | | |
438 | 735k | if (tmpVal < 0) *ptr++ = '-'; |
439 | | |
440 | 735k | if ('0' != fill) |
441 | 581k | { |
442 | 581k | while ((ptr - result) < width) *ptr++ = fill; |
443 | 581k | } |
444 | | |
445 | 735k | size = ptr - result; |
446 | 735k | poco_assert_dbg (size <= ptr.span()); |
447 | 735k | poco_assert_dbg ((-1 == width) || (size >= size_t(width))); |
448 | 735k | *ptr-- = '\0'; |
449 | | |
450 | 735k | char* ptrr = result; |
451 | 735k | char tmp; |
452 | 1.74M | while(ptrr < ptr) |
453 | 1.01M | { |
454 | 1.01M | tmp = *ptr; |
455 | 1.01M | *ptr-- = *ptrr; |
456 | 1.01M | *ptrr++ = tmp; |
457 | 1.01M | } |
458 | | |
459 | 735k | return true; |
460 | 735k | } _ZN4Poco8intToStrIiTnPNSt3__19enable_ifIXsr3std9is_signedIT_EE5valueEvE4typeELPv0EEEbS3_tPcRmbiccb Line | Count | Source | 399 | 174k | { | 400 | 174k | poco_assert_dbg (((value < 0) && (base == 10)) || (value >= 0)); | 401 | | | 402 | 174k | if (base < 2 || base > 0x10) | 403 | 0 | { | 404 | 0 | *result = '\0'; | 405 | 0 | return false; | 406 | 0 | } | 407 | | | 408 | 174k | Impl::Ptr ptr(result, size); | 409 | 174k | int thCount = 0; | 410 | 174k | T tmpVal; | 411 | 174k | do | 412 | 339k | { | 413 | 339k | tmpVal = value; | 414 | 339k | value /= base; | 415 | 339k | *ptr++ = (lowercase ? "fedcba9876543210123456789abcdef" : "FEDCBA9876543210123456789ABCDEF")[15 + (tmpVal - value * base)]; | 416 | 339k | if (thSep && (base == 10) && (++thCount == 3)) | 417 | 0 | { | 418 | 0 | *ptr++ = thSep; | 419 | 0 | thCount = 0; | 420 | 0 | } | 421 | 339k | } while (value); | 422 | | | 423 | 174k | if ('0' == fill) | 424 | 153k | { | 425 | 153k | if (tmpVal < 0) --width; | 426 | 153k | if (prefix && base == 010) --width; | 427 | 153k | if (prefix && base == 0x10) width -= 2; | 428 | 216k | while ((ptr - result) < width) *ptr++ = fill; | 429 | 153k | } | 430 | | | 431 | 174k | if (prefix && base == 010) *ptr++ = '0'; | 432 | 174k | else if (prefix && base == 0x10) | 433 | 0 | { | 434 | 0 | *ptr++ = 'x'; | 435 | 0 | *ptr++ = '0'; | 436 | 0 | } | 437 | | | 438 | 174k | if (tmpVal < 0) *ptr++ = '-'; | 439 | | | 440 | 174k | if ('0' != fill) | 441 | 20.4k | { | 442 | 20.6k | while ((ptr - result) < width) *ptr++ = fill; | 443 | 20.4k | } | 444 | | | 445 | 174k | size = ptr - result; | 446 | 174k | poco_assert_dbg (size <= ptr.span()); | 447 | 174k | poco_assert_dbg ((-1 == width) || (size >= size_t(width))); | 448 | 174k | *ptr-- = '\0'; | 449 | | | 450 | 174k | char* ptrr = result; | 451 | 174k | char tmp; | 452 | 373k | while(ptrr < ptr) | 453 | 199k | { | 454 | 199k | tmp = *ptr; | 455 | 199k | *ptr-- = *ptrr; | 456 | 199k | *ptrr++ = tmp; | 457 | 199k | } | 458 | | | 459 | 174k | return true; | 460 | 174k | } |
_ZN4Poco8intToStrIlTnPNSt3__19enable_ifIXsr3std9is_signedIT_EE5valueEvE4typeELPv0EEEbS3_tPcRmbiccb Line | Count | Source | 399 | 561k | { | 400 | 561k | poco_assert_dbg (((value < 0) && (base == 10)) || (value >= 0)); | 401 | | | 402 | 561k | if (base < 2 || base > 0x10) | 403 | 0 | { | 404 | 0 | *result = '\0'; | 405 | 0 | return false; | 406 | 0 | } | 407 | | | 408 | 561k | Impl::Ptr ptr(result, size); | 409 | 561k | int thCount = 0; | 410 | 561k | T tmpVal; | 411 | 561k | do | 412 | 1.80M | { | 413 | 1.80M | tmpVal = value; | 414 | 1.80M | value /= base; | 415 | 1.80M | *ptr++ = (lowercase ? "fedcba9876543210123456789abcdef" : "FEDCBA9876543210123456789ABCDEF")[15 + (tmpVal - value * base)]; | 416 | 1.80M | if (thSep && (base == 10) && (++thCount == 3)) | 417 | 0 | { | 418 | 0 | *ptr++ = thSep; | 419 | 0 | thCount = 0; | 420 | 0 | } | 421 | 1.80M | } while (value); | 422 | | | 423 | 561k | if ('0' == fill) | 424 | 0 | { | 425 | 0 | if (tmpVal < 0) --width; | 426 | 0 | if (prefix && base == 010) --width; | 427 | 0 | if (prefix && base == 0x10) width -= 2; | 428 | 0 | while ((ptr - result) < width) *ptr++ = fill; | 429 | 0 | } | 430 | | | 431 | 561k | if (prefix && base == 010) *ptr++ = '0'; | 432 | 561k | else if (prefix && base == 0x10) | 433 | 0 | { | 434 | 0 | *ptr++ = 'x'; | 435 | 0 | *ptr++ = '0'; | 436 | 0 | } | 437 | | | 438 | 561k | if (tmpVal < 0) *ptr++ = '-'; | 439 | | | 440 | 561k | if ('0' != fill) | 441 | 561k | { | 442 | 561k | while ((ptr - result) < width) *ptr++ = fill; | 443 | 561k | } | 444 | | | 445 | 561k | size = ptr - result; | 446 | 561k | poco_assert_dbg (size <= ptr.span()); | 447 | 561k | poco_assert_dbg ((-1 == width) || (size >= size_t(width))); | 448 | 561k | *ptr-- = '\0'; | 449 | | | 450 | 561k | char* ptrr = result; | 451 | 561k | char tmp; | 452 | 1.37M | while(ptrr < ptr) | 453 | 814k | { | 454 | 814k | tmp = *ptr; | 455 | 814k | *ptr-- = *ptrr; | 456 | 814k | *ptrr++ = tmp; | 457 | 814k | } | 458 | | | 459 | 561k | return true; | 460 | 561k | } |
Unexecuted instantiation: _ZN4Poco8intToStrIxTnPNSt3__19enable_ifIXsr3std9is_signedIT_EE5valueEvE4typeELPv0EEEbS3_tPcRmbiccb |
461 | | |
462 | | |
463 | | template <typename T, Impl::EnableUnsigned<T> = nullptr> |
464 | | bool intToStr(T value, |
465 | | unsigned short base, |
466 | | char* result, |
467 | | std::size_t& size, |
468 | | bool prefix = false, |
469 | | int width = -1, |
470 | | char fill = ' ', |
471 | | char thSep = 0, |
472 | | bool lowercase = false) |
473 | | /// Converts unsigned integer to string. Numeric bases from binary to hexadecimal are supported. |
474 | | /// If width is non-zero, it pads the return value with fill character to the specified width. |
475 | | /// When padding is zero character ('0'), it is prepended to the number itself; all other |
476 | | /// paddings are prepended to the formatted result with minus sign or base prefix included |
477 | | /// If prefix is true and base is octal or hexadecimal, respective prefix ('0' for octal, |
478 | | /// "0x" for hexadecimal) is prepended. For all other bases, prefix argument is ignored. |
479 | | /// Formatted string has at least [width] total length. |
480 | 365M | { |
481 | 365M | if (base < 2 || base > 0x10) |
482 | 0 | { |
483 | 0 | *result = '\0'; |
484 | 0 | return false; |
485 | 0 | } |
486 | | |
487 | 365M | Impl::Ptr ptr(result, size); |
488 | 365M | int thCount = 0; |
489 | 365M | T tmpVal; |
490 | 365M | do |
491 | 644M | { |
492 | 644M | tmpVal = value; |
493 | 644M | value /= base; |
494 | 644M | *ptr++ = (lowercase ? "fedcba9876543210123456789abcdef" : "FEDCBA9876543210123456789ABCDEF")[15 + (tmpVal - value * base)]; |
495 | 644M | if (thSep && (base == 10) && (++thCount == 3)) |
496 | 0 | { |
497 | 0 | *ptr++ = thSep; |
498 | 0 | thCount = 0; |
499 | 0 | } |
500 | 644M | } while (value); |
501 | | |
502 | 365M | if ('0' == fill) |
503 | 365M | { |
504 | 365M | if (prefix && base == 010) --width; |
505 | 365M | if (prefix && base == 0x10) width -= 2; |
506 | 458M | while ((ptr - result) < width) *ptr++ = fill; |
507 | 365M | } |
508 | | |
509 | 365M | if (prefix && base == 010) *ptr++ = '0'; |
510 | 365M | else if (prefix && base == 0x10) |
511 | 0 | { |
512 | 0 | *ptr++ = 'x'; |
513 | 0 | *ptr++ = '0'; |
514 | 0 | } |
515 | | |
516 | 365M | if ('0' != fill) |
517 | 10.3k | { |
518 | 10.3k | while ((ptr - result) < width) *ptr++ = fill; |
519 | 10.3k | } |
520 | | |
521 | 365M | size = ptr - result; |
522 | 365M | poco_assert_dbg (size <= ptr.span()); |
523 | 365M | poco_assert_dbg ((-1 == width) || (size >= size_t(width))); |
524 | 365M | *ptr-- = '\0'; |
525 | | |
526 | 365M | char* ptrr = result; |
527 | 365M | char tmp; |
528 | 734M | while(ptrr < ptr) |
529 | 368M | { |
530 | 368M | tmp = *ptr; |
531 | 368M | *ptr-- = *ptrr; |
532 | 368M | *ptrr++ = tmp; |
533 | 368M | } |
534 | | |
535 | 365M | return true; |
536 | 365M | } _ZN4Poco8intToStrIjTnPNSt3__19enable_ifIXsr3std11is_unsignedIT_EE5valueEvE4typeELPv0EEEbS3_tPcRmbiccb Line | Count | Source | 480 | 365M | { | 481 | 365M | if (base < 2 || base > 0x10) | 482 | 0 | { | 483 | 0 | *result = '\0'; | 484 | 0 | return false; | 485 | 0 | } | 486 | | | 487 | 365M | Impl::Ptr ptr(result, size); | 488 | 365M | int thCount = 0; | 489 | 365M | T tmpVal; | 490 | 365M | do | 491 | 644M | { | 492 | 644M | tmpVal = value; | 493 | 644M | value /= base; | 494 | 644M | *ptr++ = (lowercase ? "fedcba9876543210123456789abcdef" : "FEDCBA9876543210123456789ABCDEF")[15 + (tmpVal - value * base)]; | 495 | 644M | if (thSep && (base == 10) && (++thCount == 3)) | 496 | 0 | { | 497 | 0 | *ptr++ = thSep; | 498 | 0 | thCount = 0; | 499 | 0 | } | 500 | 644M | } while (value); | 501 | | | 502 | 365M | if ('0' == fill) | 503 | 365M | { | 504 | 365M | if (prefix && base == 010) --width; | 505 | 365M | if (prefix && base == 0x10) width -= 2; | 506 | 458M | while ((ptr - result) < width) *ptr++ = fill; | 507 | 365M | } | 508 | | | 509 | 365M | if (prefix && base == 010) *ptr++ = '0'; | 510 | 365M | else if (prefix && base == 0x10) | 511 | 0 | { | 512 | 0 | *ptr++ = 'x'; | 513 | 0 | *ptr++ = '0'; | 514 | 0 | } | 515 | | | 516 | 365M | if ('0' != fill) | 517 | 0 | { | 518 | 0 | while ((ptr - result) < width) *ptr++ = fill; | 519 | 0 | } | 520 | | | 521 | 365M | size = ptr - result; | 522 | 365M | poco_assert_dbg (size <= ptr.span()); | 523 | 365M | poco_assert_dbg ((-1 == width) || (size >= size_t(width))); | 524 | 365M | *ptr-- = '\0'; | 525 | | | 526 | 365M | char* ptrr = result; | 527 | 365M | char tmp; | 528 | 733M | while(ptrr < ptr) | 529 | 368M | { | 530 | 368M | tmp = *ptr; | 531 | 368M | *ptr-- = *ptrr; | 532 | 368M | *ptrr++ = tmp; | 533 | 368M | } | 534 | | | 535 | 365M | return true; | 536 | 365M | } |
_ZN4Poco8intToStrImTnPNSt3__19enable_ifIXsr3std11is_unsignedIT_EE5valueEvE4typeELPv0EEEbS3_tPcRmbiccb Line | Count | Source | 480 | 10.3k | { | 481 | 10.3k | if (base < 2 || base > 0x10) | 482 | 0 | { | 483 | 0 | *result = '\0'; | 484 | 0 | return false; | 485 | 0 | } | 486 | | | 487 | 10.3k | Impl::Ptr ptr(result, size); | 488 | 10.3k | int thCount = 0; | 489 | 10.3k | T tmpVal; | 490 | 10.3k | do | 491 | 204k | { | 492 | 204k | tmpVal = value; | 493 | 204k | value /= base; | 494 | 204k | *ptr++ = (lowercase ? "fedcba9876543210123456789abcdef" : "FEDCBA9876543210123456789ABCDEF")[15 + (tmpVal - value * base)]; | 495 | 204k | if (thSep && (base == 10) && (++thCount == 3)) | 496 | 0 | { | 497 | 0 | *ptr++ = thSep; | 498 | 0 | thCount = 0; | 499 | 0 | } | 500 | 204k | } while (value); | 501 | | | 502 | 10.3k | if ('0' == fill) | 503 | 0 | { | 504 | 0 | if (prefix && base == 010) --width; | 505 | 0 | if (prefix && base == 0x10) width -= 2; | 506 | 0 | while ((ptr - result) < width) *ptr++ = fill; | 507 | 0 | } | 508 | | | 509 | 10.3k | if (prefix && base == 010) *ptr++ = '0'; | 510 | 10.3k | else if (prefix && base == 0x10) | 511 | 0 | { | 512 | 0 | *ptr++ = 'x'; | 513 | 0 | *ptr++ = '0'; | 514 | 0 | } | 515 | | | 516 | 10.3k | if ('0' != fill) | 517 | 10.3k | { | 518 | 10.3k | while ((ptr - result) < width) *ptr++ = fill; | 519 | 10.3k | } | 520 | | | 521 | 10.3k | size = ptr - result; | 522 | 10.3k | poco_assert_dbg (size <= ptr.span()); | 523 | 10.3k | poco_assert_dbg ((-1 == width) || (size >= size_t(width))); | 524 | 10.3k | *ptr-- = '\0'; | 525 | | | 526 | 10.3k | char* ptrr = result; | 527 | 10.3k | char tmp; | 528 | 111k | while(ptrr < ptr) | 529 | 101k | { | 530 | 101k | tmp = *ptr; | 531 | 101k | *ptr-- = *ptrr; | 532 | 101k | *ptrr++ = tmp; | 533 | 101k | } | 534 | | | 535 | 10.3k | return true; | 536 | 10.3k | } |
Unexecuted instantiation: _ZN4Poco8intToStrIyTnPNSt3__19enable_ifIXsr3std11is_unsignedIT_EE5valueEvE4typeELPv0EEEbS3_tPcRmbiccb |
537 | | |
538 | | |
539 | | template <typename T> |
540 | | POCO_DEPRECATED("use intToStr instead") |
541 | | bool uIntToStr(T value, |
542 | | unsigned short base, |
543 | | char* result, |
544 | | std::size_t& size, |
545 | | bool prefix = false, |
546 | | int width = -1, |
547 | | char fill = ' ', |
548 | | char thSep = 0, |
549 | | bool lowercase = false) |
550 | | /// Converts unsigned integer to string. Numeric bases from binary to hexadecimal are supported. |
551 | | /// If width is non-zero, it pads the return value with fill character to the specified width. |
552 | | /// When padding is zero character ('0'), it is prepended to the number itself; all other |
553 | | /// paddings are prepended to the formatted result with minus sign or base prefix included |
554 | | /// If prefix is true and base is octal or hexadecimal, respective prefix ('0' for octal, |
555 | | /// "0x" for hexadecimal) is prepended. For all other bases, prefix argument is ignored. |
556 | | /// Formatted string has at least [width] total length. |
557 | | /// |
558 | | /// This function is deprecated; use intToStr instead. |
559 | | { |
560 | | return intToStr(value, base, result, size, prefix, width, fill, thSep, lowercase); |
561 | | } |
562 | | |
563 | | |
564 | | template <typename T> |
565 | | bool intToStr (T number, |
566 | | unsigned short base, |
567 | | std::string& result, |
568 | | bool prefix = false, |
569 | | int width = -1, |
570 | | char fill = ' ', |
571 | | char thSep = 0, |
572 | | bool lowercase = false) |
573 | | /// Converts integer to string; This is a wrapper function, for details see the |
574 | | /// bool intToStr(T, unsigned short, char*, int, int, char, char) implementation. |
575 | 361M | { |
576 | 361M | char res[POCO_MAX_INT_STRING_LEN] = {0}; |
577 | 361M | std::size_t size = POCO_MAX_INT_STRING_LEN; |
578 | 361M | bool ret = intToStr(number, base, res, size, prefix, width, fill, thSep, lowercase); |
579 | 361M | result.assign(res, size); |
580 | 361M | return ret; |
581 | 361M | } Unexecuted instantiation: bool Poco::intToStr<int>(int, unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, bool, int, char, char, bool) bool Poco::intToStr<unsigned int>(unsigned int, unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, bool, int, char, char, bool) Line | Count | Source | 575 | 360M | { | 576 | 360M | char res[POCO_MAX_INT_STRING_LEN] = {0}; | 577 | 360M | std::size_t size = POCO_MAX_INT_STRING_LEN; | 578 | 360M | bool ret = intToStr(number, base, res, size, prefix, width, fill, thSep, lowercase); | 579 | 360M | result.assign(res, size); | 580 | 360M | return ret; | 581 | 360M | } |
bool Poco::intToStr<long>(long, unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, bool, int, char, char, bool) Line | Count | Source | 575 | 561k | { | 576 | 561k | char res[POCO_MAX_INT_STRING_LEN] = {0}; | 577 | 561k | std::size_t size = POCO_MAX_INT_STRING_LEN; | 578 | 561k | bool ret = intToStr(number, base, res, size, prefix, width, fill, thSep, lowercase); | 579 | 561k | result.assign(res, size); | 580 | 561k | return ret; | 581 | 561k | } |
bool Poco::intToStr<unsigned long>(unsigned long, unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, bool, int, char, char, bool) Line | Count | Source | 575 | 10.3k | { | 576 | 10.3k | char res[POCO_MAX_INT_STRING_LEN] = {0}; | 577 | 10.3k | std::size_t size = POCO_MAX_INT_STRING_LEN; | 578 | 10.3k | bool ret = intToStr(number, base, res, size, prefix, width, fill, thSep, lowercase); | 579 | 10.3k | result.assign(res, size); | 580 | 10.3k | return ret; | 581 | 10.3k | } |
Unexecuted instantiation: bool Poco::intToStr<long long>(long long, unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, bool, int, char, char, bool) Unexecuted instantiation: bool Poco::intToStr<unsigned long long>(unsigned long long, unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, bool, int, char, char, bool) Unexecuted instantiation: bool Poco::intToStr<int>(int, unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, bool, int, char, char, bool) |
582 | | |
583 | | |
584 | | template <typename T> |
585 | | POCO_DEPRECATED("use intToStr instead") |
586 | | bool uIntToStr (T number, |
587 | | unsigned short base, |
588 | | std::string& result, |
589 | | bool prefix = false, |
590 | | int width = -1, |
591 | | char fill = ' ', |
592 | | char thSep = 0, |
593 | | bool lowercase = false) |
594 | | /// Converts unsigned integer to string; This is a wrapper function, for details see the |
595 | | /// bool uIntToStr(T, unsigned short, char*, int, int, char, char) implementation. |
596 | | /// |
597 | | /// This function is deprecated; use intToStr instead. |
598 | | { |
599 | | return intToStr(number, base, result, prefix, width, fill, thSep, lowercase); |
600 | | } |
601 | | |
602 | | |
603 | | // |
604 | | // Wrappers for double-conversion library (http://code.google.com/p/double-conversion/). |
605 | | // |
606 | | // Library is the implementation of the algorithm described in Florian Loitsch's paper: |
607 | | // http://florian.loitsch.com/publications/dtoa-pldi2010.pdf |
608 | | // |
609 | | |
610 | | |
611 | | Foundation_API void floatToStr(char* buffer, |
612 | | int bufferSize, |
613 | | float value, |
614 | | int lowDec = -std::numeric_limits<float>::digits10, |
615 | | int highDec = std::numeric_limits<float>::digits10); |
616 | | /// Converts a float value to string. Converted string must be shorter than bufferSize. |
617 | | /// Conversion is done by computing the shortest string of digits that correctly represents |
618 | | /// the input number. Depending on lowDec and highDec values, the function returns |
619 | | /// decimal or exponential representation. |
620 | | |
621 | | Foundation_API void floatToFixedStr(char* buffer, |
622 | | int bufferSize, |
623 | | float value, |
624 | | int precision); |
625 | | /// Converts a float value to string. Converted string must be shorter than bufferSize. |
626 | | /// Computes a decimal representation with a fixed number of digits after the |
627 | | /// decimal point. |
628 | | |
629 | | |
630 | | Foundation_API std::string& floatToStr(std::string& str, |
631 | | float value, |
632 | | int precision = -1, |
633 | | int width = 0, |
634 | | char thSep = 0, |
635 | | char decSep = 0); |
636 | | /// Converts a float value, assigns it to the supplied string and returns the reference. |
637 | | /// This function calls floatToStr(char*, int, float, int, int) and formats the result according to |
638 | | /// precision (total number of digits after the decimal point, -1 means ignore precision argument) |
639 | | /// and width (total length of formatted string). |
640 | | |
641 | | |
642 | | Foundation_API std::string& floatToFixedStr(std::string& str, |
643 | | float value, |
644 | | int precision, |
645 | | int width = 0, |
646 | | char thSep = 0, |
647 | | char decSep = 0); |
648 | | /// Converts a float value, assigns it to the supplied string and returns the reference. |
649 | | /// This function calls floatToFixedStr(char*, int, float, int) and formats the result according to |
650 | | /// precision (total number of digits after the decimal point) and width (total length of formatted string). |
651 | | |
652 | | |
653 | | Foundation_API void doubleToStr(char* buffer, |
654 | | int bufferSize, |
655 | | double value, |
656 | | int lowDec = -std::numeric_limits<double>::digits10, |
657 | | int highDec = std::numeric_limits<double>::digits10); |
658 | | /// Converts a double value to string. Converted string must be shorter than bufferSize. |
659 | | /// Conversion is done by computing the shortest string of digits that correctly represents |
660 | | /// the input number. Depending on lowDec and highDec values, the function returns |
661 | | /// decimal or exponential representation. |
662 | | |
663 | | |
664 | | Foundation_API void doubleToFixedStr(char* buffer, |
665 | | int bufferSize, |
666 | | double value, |
667 | | int precision); |
668 | | /// Converts a double value to string. Converted string must be shorter than bufferSize. |
669 | | /// Computes a decimal representation with a fixed number of digits after the |
670 | | /// decimal point. |
671 | | |
672 | | |
673 | | Foundation_API std::string& doubleToStr(std::string& str, |
674 | | double value, |
675 | | int precision = -1, |
676 | | int width = 0, |
677 | | char thSep = 0, |
678 | | char decSep = 0); |
679 | | /// Converts a double value, assigns it to the supplied string and returns the reference. |
680 | | /// This function calls doubleToStr(char*, int, double, int, int) and formats the result according to |
681 | | /// precision (total number of digits after the decimal point, -1 means ignore precision argument) |
682 | | /// and width (total length of formatted string). |
683 | | |
684 | | |
685 | | Foundation_API std::string& doubleToFixedStr(std::string& str, |
686 | | double value, |
687 | | int precision = -1, |
688 | | int width = 0, |
689 | | char thSep = 0, |
690 | | char decSep = 0); |
691 | | /// Converts a double value, assigns it to the supplied string and returns the reference. |
692 | | /// This function calls doubleToFixedStr(char*, int, double, int) and formats the result according to |
693 | | /// precision (total number of digits after the decimal point) and width (total length of formatted string). |
694 | | |
695 | | |
696 | | Foundation_API float strToFloat(const char* str, |
697 | | const char* inf = POCO_FLT_INF, const char* nan = POCO_FLT_NAN); |
698 | | /// Converts the string of characters into single-precision floating point number. |
699 | | /// Function uses double_conversion::DoubleToStringConverter to do the conversion. |
700 | | |
701 | | |
702 | | Foundation_API bool strToFloat(const std::string&, float& result, |
703 | | char decSep = '.', char thSep = ',', |
704 | | const char* inf = POCO_FLT_INF, const char* nan = POCO_FLT_NAN); |
705 | | /// Converts the string of characters into single-precision floating point number. |
706 | | /// The conversion result is assigned to the result parameter. |
707 | | /// If decimal separator and/or thousand separator are different from defaults, they should be |
708 | | /// supplied to ensure proper conversion. |
709 | | /// |
710 | | /// Returns true if successful, false otherwise. |
711 | | |
712 | | |
713 | | Foundation_API double strToDouble(const char* str, |
714 | | const char* inf = POCO_FLT_INF, const char* nan = POCO_FLT_NAN); |
715 | | /// Converts the string of characters into double-precision floating point number. |
716 | | |
717 | | |
718 | | Foundation_API bool strToDouble(const std::string& str, double& result, |
719 | | char decSep = '.', char thSep = ',', |
720 | | const char* inf = POCO_FLT_INF, const char* nan = POCO_FLT_NAN); |
721 | | /// Converts the string of characters into double-precision floating point number. |
722 | | /// The conversion result is assigned to the result parameter. |
723 | | /// If decimal separator and/or thousand separator are different from defaults, they should be |
724 | | /// supplied to ensure proper conversion. |
725 | | /// |
726 | | /// Returns true if successful, false otherwise. |
727 | | |
728 | | |
729 | | } // namespace Poco |
730 | | |
731 | | |
732 | | #ifdef POCO_COMPILER_MSVC |
733 | | #pragma warning(pop) |
734 | | #endif // POCO_COMPILER_MSVC |
735 | | |
736 | | |
737 | | #endif // Foundation_NumericString_INCLUDED |