17 #include <boost/version.hpp>
22 #include <string_view>
27 #if BOOST_VERSION >= 108500 && __has_include(<boost/charconv.hpp>)
28 #define USE_BOOST_CHARCONV
29 #elif defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 11
30 #define USE_STD_CHARCONV
31 #elif defined(_MSC_VER) && _MSC_VER >= 1924
32 #define USE_STD_CHARCONV
33 #elif defined(_LIBCPP_VERSION) && _LIBCPP_VERSION >= 14000
34 #define USE_FALLBACK_CHARCONV
36 #error No charconv implementation found.
39 #ifdef USE_BOOST_CHARCONV
40 #include <boost/charconv.hpp>
41 namespace charconv_impl = boost::charconv;
44 namespace charconv_impl = std;
54 template<
typename... T>
66 #ifndef USE_FALLBACK_CHARCONV
87 while(!v.empty() && std::isspace(v.front())) {
90 if(v.size() >= 2 && v[0] ==
'+' && v[1] !=
'-' ) {
97 template<
typename TNum>
100 std::array<char, utils::charconv::buffer_size<TNum>>
buffer;
117 if(ec != std::errc()) {
119 assert(!
"Error in charconv_buffer, buffer not large enough");
141 inline double stod(std::string_view str) {
145 if(ec == std::errc::invalid_argument) {
146 throw std::invalid_argument(
"");
147 }
else if(ec == std::errc::result_out_of_range) {
148 throw std::out_of_range(
"");
154 inline int stoi(std::string_view str) {
158 if(ec == std::errc::invalid_argument) {
159 throw std::invalid_argument(
"");
160 }
else if(ec == std::errc::result_out_of_range) {
161 throw std::out_of_range(
"");
std::enable_if_t< std::is_integral_v< T >, from_chars_result > from_chars(const char *first, const char *last, T &value, int base=10)
charconv_impl::from_chars_result from_chars_result
charconv_impl::chars_format chars_format
to_chars_result to_chars(char *first, char *last, T &&... value)
constexpr size_t buffer_size
charconv_impl::to_chars_result to_chars_result
std::enable_if_t< std::is_floating_point_v< T >, from_chars_result > from_chars(const char *first, const char *last, T &value, chars_format fmt=chars_format::general)
int stoi(std::string_view str)
Same interface as std::stoi and meant as a drop in replacement, except:
void trim_for_from_chars(std::string_view &v)
double stod(std::string_view str)
Same interface as std::stod and meant as a drop in replacement, except:
std::string_view get_view() const
std::string to_string() const
std::array< char, utils::charconv::buffer_size< TNum > > buffer
charconv_buffer(TNum num)