Skip to content

Commit 2e906c2

Browse files
committed
Change toDouble
1 parent fa55aa5 commit 2e906c2

File tree

1 file changed

+58
-26
lines changed

1 file changed

+58
-26
lines changed

include/simstr/sstring.h

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const bool isx64 = sizeof(void*) == 8; // NOLINT
7575
#include <memory>
7676
#include <string.h>
7777
#include <iostream>
78+
#include <cmath>
7879

7980
#ifdef _WIN32
8081
#include <stdio.h>
@@ -1460,43 +1461,74 @@ class str_algs : public buffer_pointers<K, Impl, Mutable> {
14601461
* @return double. So far it only works for strings of char, wchar_t and types compatible with wchar_t in size.
14611462
*/
14621463
double to_double() const noexcept {
1463-
static_assert(sizeof(K) == 1 || sizeof(K) == sizeof(wchar_t), "Only char and wchar available for conversion to double now");
1464-
size_t len = _len();
1465-
if (len) {
1466-
const size_t copyLen = 64;
1467-
K buf[copyLen + 1];
1468-
const K* ptr = _str();
1469-
if (ptr[len] != 0) {
1464+
if constexpr (is_one_of_std_char_v<K>) {
1465+
size_t len = _len();
1466+
if (len) {
1467+
const size_t copyLen = 64;
1468+
K buf[copyLen + 1];
1469+
const K* ptr = _str();
1470+
if (ptr[len] != 0) {
1471+
while (len && uns_type(*ptr) <= ' ') {
1472+
len--;
1473+
ptr++;
1474+
}
1475+
if (len) {
1476+
len = std::min(copyLen, len);
1477+
traits::copy(buf, ptr, len);
1478+
buf[len] = 0;
1479+
ptr = buf;
1480+
}
1481+
}
1482+
if (len) {
1483+
#ifdef _MSC_VER
1484+
static const _locale_t lc = _wcreate_locale(LC_NUMERIC, L"C");
1485+
if constexpr (sizeof(K) == 1) {
1486+
return _strtod_l(ptr, nullptr, lc);
1487+
}
1488+
if constexpr (sizeof(K) == sizeof(wchar_t)) {
1489+
return _wcstod_l((const wchar_t*)ptr, nullptr, lc);
1490+
}
1491+
#else
1492+
if constexpr (sizeof(K) == 1) {
1493+
return std::strtod(ptr, nullptr);
1494+
} else if constexpr (sizeof(K) == sizeof(wchar_t)) {
1495+
return std::wcstod((const wchar_t*)ptr, nullptr);
1496+
}
1497+
#endif
1498+
}
1499+
}
1500+
return std::nan(nullptr);
1501+
} else {
1502+
size_t len = _len();
1503+
if (len) {
1504+
const size_t copyLen = 64;
1505+
char buf[copyLen + 1];
1506+
const K* ptr = _str();
14701507
while (len && uns_type(*ptr) <= ' ') {
14711508
len--;
14721509
ptr++;
14731510
}
14741511
if (len) {
14751512
len = std::min(copyLen, len);
1476-
traits::copy(buf, ptr, len);
1513+
for (unsigned idx = 0; idx < len; idx++) {
1514+
if (ptr[idx] > 128) {
1515+
return std::nan(nullptr);
1516+
}
1517+
buf[idx] = ptr[idx];
1518+
}
14771519
buf[len] = 0;
1478-
ptr = buf;
1479-
}
1480-
}
1481-
if (len) {
1482-
#ifdef _MSC_VER
1483-
static const _locale_t lc = _wcreate_locale(LC_NUMERIC, L"C");
1484-
if constexpr (sizeof(K) == 1) {
1485-
return _strtod_l(ptr, nullptr, lc);
1486-
}
1487-
if constexpr (sizeof(K) == sizeof(wchar_t)) {
1488-
return _wcstod_l((const wchar_t*)ptr, nullptr, lc);
14891520
}
1490-
#else
1491-
if constexpr (sizeof(K) == 1) {
1492-
return std::strtod(ptr, nullptr);
1493-
} else if constexpr (sizeof(K) == sizeof(wchar_t)) {
1494-
return std::wcstod((const wchar_t*)ptr, nullptr);
1521+
if (len) {
1522+
#ifdef _MSC_VER
1523+
static const _locale_t lc = _wcreate_locale(LC_NUMERIC, L"C");
1524+
return _strtod_l(buf, nullptr, lc);
1525+
#else
1526+
return std::strtod(buf, nullptr);
1527+
#endif
14951528
}
1496-
#endif
14971529
}
1530+
return std::nan(nullptr);
14981531
}
1499-
return 0.0;
15001532
}
15011533

15021534
/*!

0 commit comments

Comments
 (0)