Skip to content

Commit a7c248f

Browse files
committed
Use from_chars when supported in GCC and VS
1 parent 1b9fd5d commit a7c248f

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

include/jsoncons/config/compiler_support.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@
114114
#endif
115115
#endif
116116

117+
#if !defined(JSONCONS_HAS_STD_FROM_CHARS)
118+
# if defined(__GNUC__)
119+
# if (__GNUC__ >= 11)
120+
# if (__cplusplus >= 201703)
121+
# define JSONCONS_HAS_STD_FROM_CHARS 1
122+
# endif // (__cplusplus >= 201703)
123+
# endif // (__GNUC__ >= 11)
124+
# endif // defined(__GNUC__)
125+
# if defined(_MSC_VER)
126+
# if (_MSC_VER >= 1924 && _MSVC_LANG >= 201703)
127+
# define JSONCONS_HAS_STD_FROM_CHARS 1
128+
# endif // (_MSC_VER >= 1924 && MSVC_LANG >= 201703)
129+
# endif // defined(_MSC_VER)
130+
#endif
131+
#if defined(JSONCONS_HAS_STD_FROM_CHARS)
132+
#include <charconv>
133+
#endif
134+
117135
#if !defined(JSONCONS_HAS_2017)
118136
# if defined(__clang__)
119137
# if (__cplusplus >= 201703)

include/jsoncons/detail/parse_number.hpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,43 @@ base16_to_integer(const CharT* s, std::size_t length, T& n)
909909
return to_integer_result<T,CharT>(s, to_integer_errc());
910910
}
911911

912-
#if defined(JSONCONS_HAS_MSC_STRTOD_L)
912+
913+
#if defined(JSONCONS_HAS_STD_FROM_CHARS)
914+
915+
class to_double_t
916+
{
917+
public:
918+
919+
char get_decimal_point() const
920+
{
921+
return '.';
922+
}
923+
924+
template <class CharT>
925+
typename std::enable_if<std::is_same<CharT,char>::value,double>::type
926+
operator()(const CharT* s, std::size_t len) const
927+
{
928+
double val = 0;
929+
std::from_chars(s, s+len, val);
930+
return val;
931+
}
932+
933+
template <class CharT>
934+
typename std::enable_if<std::is_same<CharT,wchar_t>::value,double>::type
935+
operator()(const CharT* s, std::size_t len) const
936+
{
937+
std::string input(len,'0');
938+
for (size_t i = 0; i < len; ++i)
939+
{
940+
input[i] = static_cast<char>(s[i]);
941+
}
942+
943+
double val = 0;
944+
std::from_chars(input.data(), input.data() + len, val);
945+
return val;
946+
}
947+
};
948+
#elif defined(JSONCONS_HAS_MSC_STRTOD_L)
913949

914950
class to_double_t
915951
{

0 commit comments

Comments
 (0)