mirror of
https://github.com/red0124/ssp.git
synced 2025-01-23 13:05:20 +01:00
Remove std::from_chars from extract.hpp
This commit is contained in:
parent
634abdd38b
commit
81484f737f
@ -12,7 +12,8 @@
|
|||||||
#ifndef SSP_DISABLE_FAST_FLOAT
|
#ifndef SSP_DISABLE_FAST_FLOAT
|
||||||
#include <fast_float/fast_float.h>
|
#include <fast_float/fast_float.h>
|
||||||
#else
|
#else
|
||||||
#include <charconv>
|
#include <algorithm>
|
||||||
|
#include <cstdlib>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace ss {
|
namespace ss {
|
||||||
@ -40,12 +41,32 @@ std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
||||||
const char* const begin, const char* const end) {
|
const char* const begin, const char* const end) {
|
||||||
T ret;
|
constexpr static auto buff_max = 64;
|
||||||
auto [ptr, ec] = std::from_chars(begin, end, ret);
|
char buff[buff_max];
|
||||||
|
size_t string_range = std::distance(begin, end);
|
||||||
|
|
||||||
if (ec != std::errc() || ptr != end) {
|
if (string_range > buff_max) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::copy_n(begin, string_range, buff);
|
||||||
|
buff[string_range] = '\0';
|
||||||
|
|
||||||
|
T ret;
|
||||||
|
char* parse_end = nullptr;
|
||||||
|
|
||||||
|
if constexpr (std::is_same_v<T, float>) {
|
||||||
|
ret = std::strtof(buff, &parse_end);
|
||||||
|
} else if constexpr (std::is_same_v<T, double>) {
|
||||||
|
ret = std::strtod(buff, &parse_end);
|
||||||
|
} else if constexpr (std::is_same_v<T, long double>) {
|
||||||
|
ret = std::strtold(buff, &parse_end);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parse_end != buff + string_range) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user