12 #ifndef PLSSVM_DETAIL_STRING_CONVERSION_HPP_
13 #define PLSSVM_DETAIL_STRING_CONVERSION_HPP_
20 #include "fast_float/fast_float.h"
26 #include <string_view>
27 #include <system_error>
28 #include <type_traits>
46 template <
typename T,
typename Exception = std::runtime_error, PLSSVM_REQUIRES((std::is_arithmetic_v<T> || std::is_same_v<remove_cvref_t<T>, std::
string>) )>
47 [[nodiscard]]
inline T
convert_to(
const std::string_view str) {
50 return std::string{
trim(str) };
54 if (lower_case_str ==
"true") {
58 if (lower_case_str ==
"false") {
62 return static_cast<bool>(convert_to<long long, Exception>(str));
64 const std::string_view trimmed =
trim(str);
65 if (trimmed.size() != 1) {
66 throw Exception{ fmt::format(
"Can't convert '{}' to a value of type char!", str) };
68 return trimmed.front();
70 return std::stold(std::string{ str });
73 const auto convert_from_chars = [](
const std::string_view sv,
auto &val) {
74 if constexpr (std::is_floating_point_v<T>) {
76 return fast_float::from_chars(sv.data(), sv.data() + sv.size(), val);
79 return std::from_chars(sv.data(), sv.data() + sv.size(), val);
83 return std::conditional_t<std::is_floating_point_v<T>, fast_float::from_chars_result, std::from_chars_result>{};
87 const std::string_view trimmed_str =
trim_left(str);
91 auto res = convert_from_chars(trimmed_str, val);
92 if (res.ec != std::errc{}) {
93 throw Exception{ fmt::format(
"Can't convert '{}' to a value of type {}!", str, arithmetic_type_name<T>()) };
109 template <
typename T>
111 const std::string_view::size_type n = str.find_first_of(
"0123456789");
112 if (n != std::string_view::npos) {
113 const std::string_view::size_type m = str.find_first_not_of(
"0123456789", n);
114 return convert_to<T>(str.substr(n, m != std::string_view::npos ? m - n : m));
116 throw std::runtime_error{ fmt::format(
"String \"{}\" doesn't contain any integer!", str) };
126 template <
typename T>
127 [[nodiscard]]
inline std::vector<T>
split_as(
const std::string_view str,
const char delim =
' ') {
128 std::vector<T> split_str;
135 std::string_view::size_type pos = 0;
136 std::string_view::size_type next = 0;
137 while (next != std::string_view::npos) {
138 next = str.find_first_of(delim, pos);
139 split_str.emplace_back(convert_to<T>(next == std::string_view::npos ? str.substr(pos) : str.substr(pos, next - pos)));
Implements conversion functions from arithmetic types to their name as string representation.
Namespace containing implementation details. Should not directly be used by users.
Definition: csvm.hpp:27
std::vector< T > split_as(const std::string_view str, const char delim=' ')
Split the string str at the positions with delimiter delim and return the sub-strings converted to th...
Definition: string_conversion.hpp:127
T extract_first_integer_from_string(std::string_view str)
Extract the first integer from the given string str and converts it to T ignoring a potential sign.
Definition: string_conversion.hpp:110
T convert_to(const std::string_view str)
Converts the string str to a value of type T.
Definition: string_conversion.hpp:47
std::string_view trim(std::string_view str) noexcept
Returns a new std::string_view equal to str where all leading and trailing whitespaces are removed.
std::string_view trim_left(std::string_view str) noexcept
Returns a new std::string_view equal to str where all leading whitespaces are removed.
std::remove_cv_t< std::remove_reference_t< T > > remove_cvref_t
Remove the topmost reference- and cv-qualifiers.
Definition: type_traits.hpp:46
std::string as_lower_case(std::string_view str)
Return a new string with the same content as str but all lower case.
Implements utility functions for string manipulation and querying.
Defines some generic type traits used in the PLSSVM library.