16 #define STRING_CONV_HAVE_CHAR_CONV
19 #if defined __has_include
20 #if __has_include (<charconv>)
22 #ifdef __cpp_lib_to_chars
23 #define STRING_CONV_HAVE_CHAR_CONV
29 namespace adtf::base::string_conversion
32 enum class float_format
43 #ifdef STRING_CONV_HAVE_CHAR_CONV
46 inline std::string float_to_string(T value, float_format
format)
48 std::chars_format target_format = std::chars_format::fixed;
51 case float_format::general: target_format = std::chars_format::general;
break;
52 case float_format::fixed: target_format = std::chars_format::fixed;
break;
53 case float_format::scientific: target_format = std::chars_format::scientific;
break;
54 case float_format::hex: target_format = std::chars_format::hex;
break;
57 std::array<char, std::numeric_limits<T>::max_exponent10 + 2> buffer {};
58 const auto conversion_result = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value, target_format);
59 if (conversion_result.ec != std::errc())
61 throw std::runtime_error(
"cannot convert number to string: " + std::make_error_code(conversion_result.ec).message());
65 return std::string(buffer.data(), conversion_result.ptr);
70 inline T string_to_float(std::string_view value)
73 const auto conversion_result = std::from_chars(value.data(), value.data() + value.size(), result);
74 if (conversion_result.ec != std::errc())
76 throw std::runtime_error(
"cannot convert string '" + std::string(value) +
"' to number: " +
77 std::make_error_code(conversion_result.ec).message());
84 const std::locale& get_c_locale();
87 inline std::string float_to_string(T value, float_format
format)
89 std::ostringstream oStream;
90 oStream.imbue(get_c_locale());
94 case float_format::general:
break;
95 case float_format::fixed: oStream << std::fixed ;
break;
96 case float_format::scientific: oStream << std::scientific;
break;
97 case float_format::hex: oStream << std::hexfloat;
break;
102 return oStream.str();
105 template <
typename T>
106 inline T string_to_float(std::string_view value)
109 std::istringstream oStream {std::string(value)};
110 oStream.imbue(get_c_locale());
114 throw std::invalid_argument(
"not a number");
123 template <
typename T>
124 inline std::string
to_string(T value, float_format = float_format::fixed)
126 return std::to_string(value);
132 return detail::float_to_string(value,
format);
138 return detail::float_to_string(value,
format);
141 std::pair<int, size_t> get_base(std::string_view value);
143 template <
typename T>
144 inline T to_number(std::string_view value,
int base = 0)
148 #ifdef STRING_CONV_HAVE_CHAR_CONV
151 const auto base_helper = get_base(value);
152 value = value.substr(base_helper.second);
153 base = base_helper.first;
155 const auto conversion_result = std::from_chars(value.data(), value.data() + value.size(), result, base);
157 if (conversion_result.ec != std::errc())
159 const auto strMessage =
"cannot convert string '" + std::string(value) +
"' to number: " +
160 std::make_error_code(conversion_result.ec).message();
162 if (conversion_result.ec == std::errc::result_out_of_range)
164 throw std::overflow_error(strMessage);
167 throw std::runtime_error(strMessage);
172 if constexpr(std::is_signed_v<T>)
174 const auto helper = std::stoll(std::string(value), 0, base);
175 if (helper > std::numeric_limits<T>::max() ||
176 helper < std::numeric_limits<T>::lowest())
178 throw std::out_of_range(
"out of range");
180 result =
static_cast<T
>(helper);
184 const auto helper = std::stoull(std::string(value), 0, base);
185 if (helper > std::numeric_limits<T>::max())
187 throw std::out_of_range(
"out of range");
189 result =
static_cast<T
>(helper);
192 catch (
const std::out_of_range& oError)
194 const auto strMessage =
"cannot convert string '" + std::string(value) +
"' to number: " + oError.what();
195 throw std::overflow_error(strMessage);
203 inline float to_number(std::string_view value,
int )
205 return detail::string_to_float<float>(value);
209 inline double to_number(std::string_view value,
int )
211 return detail::string_to_float<double>(value);
cString to_string(const tResult &i_oResult, eResultFormatFlags i_eFormatFlags=eResultFormatFlags::RFF_DisableNone, const tChar *i_strFormat=nullptr)
Copy all information of an assigned result object to a (formatted) string.
std::string format(const char *str_format,...)
printf()-like formatting of an input string.