111template <
typename IntegerType,
113 std::is_integral<IntegerType>::value &&
114 !std::is_same<IntegerType, bool>::value,
bool> =
true>
117 return std::to_string(x);
123template <
typename BoolType,
124 std::enable_if_t<std::is_same<BoolType, bool>::value,
bool> =
true>
127 return {x ?
"true" :
"false"};
133template <
typename FloatingType,
134 std::enable_if_t<std::is_floating_point<FloatingType>::value,
bool> =
true>
139 std::string str(buf);
146 struct lconv* lc = localeconv();
147 size_t offset = str.find(lc->decimal_point);
148 if (offset != std::string::npos) {
150 }
else if (str.find(
'e') == std::string::npos && str !=
"inf" && str !=
"-inf" && str !=
"nan") {
159template <
typename IntegerType,
161 std::is_integral<IntegerType>::value &&
162 std::is_signed<IntegerType>::value &&
163 !std::is_same<IntegerType, bool>::value,
bool> =
true>
164inline IntegerType
from_string(
const std::string& src, IntegerType defaultValue =
static_cast<IntegerType
>(0))
166 char *endptr =
nullptr;
167 const char* startptr = src.c_str();
168 static constexpr int base = 10;
171 auto ret = std::strtoll(startptr, &endptr, base);
178 if (endptr == startptr) {
183 if (endptr != startptr + src.size()) {
188 if (
ret < (std::numeric_limits<IntegerType>::min)() ||
ret > (std::numeric_limits<IntegerType>::max)()) {
193 return static_cast<IntegerType
>(
ret);
199template <
typename IntegerType,
201 std::is_integral<IntegerType>::value &&
202 !std::is_signed<IntegerType>::value &&
203 !std::is_same<IntegerType, bool>::value,
bool> =
true>
204inline IntegerType
from_string(
const std::string& src, IntegerType defaultValue =
static_cast<IntegerType
>(0))
206 char *endptr =
nullptr;
207 const char* startptr = src.c_str();
208 static constexpr int base = 10;
211 auto ret = std::strtoull(startptr, &endptr, base);
218 if (endptr == startptr) {
223 if (endptr != startptr + src.size()) {
228 if (
ret > (std::numeric_limits<IntegerType>::max)()) {
233 if (
ret > (std::numeric_limits<long long>::max)() && std::strtoll(startptr, &endptr, base) < 0) {
238 return static_cast<IntegerType
>(
ret);
245template <
typename BoolType,
246 std::enable_if_t<std::is_same<BoolType, bool>::value,
bool> =
true>
247inline BoolType
from_string(
const std::string& src, BoolType defaultValue =
false)
249 if (src ==
"1") {
return true; }
250 if (src ==
"true") {
return true; }
251 if (src ==
"True") {
return true; }
252 if (src ==
"TRUE") {
return true; }
253 if (src ==
"yes") {
return true; }
254 if (src ==
"Yes") {
return true; }
255 if (src ==
"YES") {
return true; }
256 if (src ==
"on") {
return true; }
257 if (src ==
"On") {
return true; }
258 if (src ==
"ON") {
return true; }
260 if (src ==
"0") {
return false; }
261 if (src ==
"false") {
return false; }
262 if (src ==
"False") {
return false; }
263 if (src ==
"FALSE") {
return false; }
264 if (src ==
"no") {
return false; }
265 if (src ==
"No") {
return false; }
266 if (src ==
"NO") {
return false; }
267 if (src ==
"off") {
return false; }
268 if (src ==
"Off") {
return false; }
269 if (src ==
"OFF") {
return false; }
277template <
typename FloatingType,
278 std::enable_if_t<std::is_floating_point<FloatingType>::value,
bool> =
true>
279inline FloatingType
from_string(
const std::string& src, FloatingType defaultValue =
static_cast<FloatingType
>(0.0))
282 return std::numeric_limits<FloatingType>::infinity();
285 return -std::numeric_limits<FloatingType>::infinity();
288 return std::numeric_limits<FloatingType>::quiet_NaN();
292 std::string src_c = src;
293 size_t offset = src.find(
'.');
294 if (offset != std::string::npos) {
295 struct lconv* lc = localeconv();
296 src_c[offset] = lc->decimal_point[0];
299 char *endptr =
nullptr;
300 const char* startptr = src_c.c_str();
301 auto ret =
static_cast<FloatingType
>(strtod(startptr, &endptr));
303 if (endptr == startptr) {
308 if (endptr != startptr + src.size()) {
316template <
typename IntegerType,
318 std::is_integral<IntegerType>::value &&
319 sizeof(IntegerType) != 1 &&
320 !std::is_same<IntegerType, bool>::value,
bool> =
true>
323 std::stringstream stream;
324 stream << std::hex << i;
329template <
typename IntegerType,
330 std::enable_if_t<
sizeof(IntegerType) == 1,
bool> =
true>
335 return to_hex_string<int>(
static_cast<int>(
static_cast<uint8_t
>(i)));