112template <
typename IntegerType,
114 std::is_integral<IntegerType>::value &&
115 !std::is_same<IntegerType, bool>::value,
bool> =
true>
118 return std::to_string(x);
124template <
typename BoolType,
125 std::enable_if_t<std::is_same<BoolType, bool>::value,
bool> =
true>
128 return {x ?
"true" :
"false"};
134template <
typename FloatingType,
135 std::enable_if_t<std::is_floating_point<FloatingType>::value,
bool> =
true>
140 std::string str(buf);
147 struct lconv* lc = localeconv();
148 size_t offset = str.find(lc->decimal_point);
149 if (offset != std::string::npos) {
151 }
else if (str.find(
'e') == std::string::npos && str !=
"inf" && str !=
"-inf" && str !=
"nan") {
160template <
typename IntegerType,
162 std::is_integral<IntegerType>::value &&
163 std::is_signed<IntegerType>::value &&
164 !std::is_same<IntegerType, bool>::value,
bool> =
true>
165inline IntegerType
from_string(
const std::string& src, IntegerType defaultValue =
static_cast<IntegerType
>(0))
167 char *endptr =
nullptr;
168 const char* startptr = src.c_str();
169 static constexpr int base = 10;
172 auto ret = std::strtoll(startptr, &endptr, base);
179 if (endptr == startptr) {
184 if (endptr != startptr + src.size()) {
189 if (
ret < (std::numeric_limits<IntegerType>::min)() ||
ret > (std::numeric_limits<IntegerType>::max)()) {
194 return static_cast<IntegerType
>(
ret);
200template <
typename IntegerType,
202 std::is_integral<IntegerType>::value &&
203 !std::is_signed<IntegerType>::value &&
204 !std::is_same<IntegerType, bool>::value,
bool> =
true>
205inline IntegerType
from_string(
const std::string& src, IntegerType defaultValue =
static_cast<IntegerType
>(0))
207 char *endptr =
nullptr;
208 const char* startptr = src.c_str();
209 static constexpr int base = 10;
212 auto ret = std::strtoull(startptr, &endptr, base);
219 if (endptr == startptr) {
224 if (endptr != startptr + src.size()) {
229 if (
ret > (std::numeric_limits<IntegerType>::max)()) {
234 if (
ret > (std::numeric_limits<long long>::max)() && std::strtoll(startptr, &endptr, base) < 0) {
239 return static_cast<IntegerType
>(
ret);
246template <
typename BoolType,
247 std::enable_if_t<std::is_same<BoolType, bool>::value,
bool> =
true>
248inline BoolType
from_string(
const std::string& src, BoolType defaultValue =
false)
250 if (src ==
"1") {
return true; }
251 if (src ==
"true") {
return true; }
252 if (src ==
"True") {
return true; }
253 if (src ==
"TRUE") {
return true; }
254 if (src ==
"yes") {
return true; }
255 if (src ==
"Yes") {
return true; }
256 if (src ==
"YES") {
return true; }
257 if (src ==
"on") {
return true; }
258 if (src ==
"On") {
return true; }
259 if (src ==
"ON") {
return true; }
261 if (src ==
"0") {
return false; }
262 if (src ==
"false") {
return false; }
263 if (src ==
"False") {
return false; }
264 if (src ==
"FALSE") {
return false; }
265 if (src ==
"no") {
return false; }
266 if (src ==
"No") {
return false; }
267 if (src ==
"NO") {
return false; }
268 if (src ==
"off") {
return false; }
269 if (src ==
"Off") {
return false; }
270 if (src ==
"OFF") {
return false; }
278template <
typename FloatingType,
279 std::enable_if_t<std::is_floating_point<FloatingType>::value,
bool> =
true>
280inline FloatingType
from_string(
const std::string& src, FloatingType defaultValue =
static_cast<FloatingType
>(0.0))
283 return std::numeric_limits<FloatingType>::infinity();
286 return -std::numeric_limits<FloatingType>::infinity();
289 return std::numeric_limits<FloatingType>::quiet_NaN();
293 std::string src_c = src;
294 size_t offset = src.find(
'.');
295 if (offset != std::string::npos) {
296 struct lconv* lc = localeconv();
297 src_c[offset] = lc->decimal_point[0];
300 char *endptr =
nullptr;
301 const char* startptr = src_c.c_str();
302 auto ret =
static_cast<FloatingType
>(strtod(startptr, &endptr));
304 if (endptr == startptr) {
309 if (endptr != startptr + src.size()) {
317template <
typename IntegerType,
319 std::is_integral<IntegerType>::value &&
320 sizeof(IntegerType) != 1 &&
321 !std::is_same<IntegerType, bool>::value,
bool> =
true>
324 std::stringstream stream;
325 stream << std::hex << i;
330template <
typename IntegerType,
331 std::enable_if_t<
sizeof(IntegerType) == 1,
bool> =
true>
336 return to_hex_string<int>(
static_cast<int>(
static_cast<uint8_t
>(i)));