12 #ifndef QMCPLUSPLUS_MODERNSTRINGUTILS_HPP 13 #define QMCPLUSPLUS_MODERNSTRINGUTILS_HPP 18 #include <string_view> 20 #include <type_traits> 37 std::string
lowerCase(
const std::string_view
s);
40 namespace modernstrutil
43 std::vector<std::string_view>
split(
const std::string_view
s,
const std::string_view delimiters);
45 std::string_view
strip(
const std::string_view
s);
54 static_assert(std::is_floating_point_v<T>);
58 #if _GLIBCXX_RELEASE > 10 59 auto [prt, ec] = std::from_chars(svalue.data(), svalue.data() + svalue.size(), result, std::chars_format::general);
60 if (ec != std::errc())
61 throw std::runtime_error(
"Could not convert from string to real value");
64 std::string str_value(svalue);
65 result =
static_cast<T
>(atof(str_value.c_str()));
76 static_assert(std::is_integral_v<T>);
80 #if _GLIBCXX_RELEASE > 10 81 auto [prt, ec] = std::from_chars(svalue.data(), svalue.data() + svalue.size(), result);
83 if (ec != std::errc())
85 if (ec == std::errc::result_out_of_range)
86 throw std::range_error(
"Value out of range for integral type parameter of string2Int");
89 std::ostringstream msg;
90 msg <<
"Could not convert from string " << std::string(svalue) <<
" to int!";
91 throw std::runtime_error(msg.str());
96 std::string str_value(svalue);
97 if constexpr (std::is_same_v<T, int>)
98 result = atoi(str_value.c_str());
99 else if constexpr (std::is_same_v<T, long>)
100 result = atol(str_value.c_str());
101 else if constexpr (std::is_same_v<T, long long>)
102 result = atol(str_value.c_str());
104 throw std::runtime_error(
"unsupported type for string to integral type conversion with pre v.10 stdlibc++!");
T string2Int(const std::string_view svalue)
alternate to string2real calls c++ string to real conversion based on T's precision template<typename...
helper functions for EinsplineSetBuilder
std::string_view strip(const string_view s)
T string2Real(const std::string_view svalue)
alternate to string2real calls c++ string to real conversion based on T's precision template<typename...
std::string lowerCase(const std::string_view s)
++17
std::vector< std::string_view > split(const string_view s, const string_view delimiters)