mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
Use std::chars_format rather than local copy
I believe the Supported Toolchains all now include <charconv> (and std::chars_format) and we can use the stdlib-defined values. Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
This commit is contained in:
parent
ccf3920ce2
commit
1c61082a4c
@ -167,13 +167,6 @@ constexpr bool starts_with(std::basic_string_view<CharT, Traits> prefix,
|
|||||||
return s.substr(0, prefix.size()) == prefix;
|
return s.substr(0, prefix.size()) == prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class chars_format {
|
|
||||||
scientific = 0x1,
|
|
||||||
fixed = 0x2,
|
|
||||||
hex = 0x4,
|
|
||||||
general = fixed | scientific
|
|
||||||
};
|
|
||||||
|
|
||||||
struct consume_hex_prefix_result {
|
struct consume_hex_prefix_result {
|
||||||
bool is_hexadecimal;
|
bool is_hexadecimal;
|
||||||
std::string_view rest;
|
std::string_view rest;
|
||||||
@ -265,46 +258,46 @@ template <class T> inline auto do_strtod(std::string const &s) -> T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T> struct parse_number<T, chars_format::general> {
|
template <class T> struct parse_number<T, std::chars_format::general> {
|
||||||
auto operator()(std::string const &s) -> T {
|
auto operator()(std::string const &s) -> T {
|
||||||
if (auto r = consume_hex_prefix(s); r.is_hexadecimal)
|
if (auto r = consume_hex_prefix(s); r.is_hexadecimal)
|
||||||
throw std::invalid_argument{
|
throw std::invalid_argument{
|
||||||
"chars_format::general does not parse hexfloat"};
|
"std::chars_format::general does not parse hexfloat"};
|
||||||
|
|
||||||
return do_strtod<T>(s);
|
return do_strtod<T>(s);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T> struct parse_number<T, chars_format::hex> {
|
template <class T> struct parse_number<T, std::chars_format::hex> {
|
||||||
auto operator()(std::string const &s) -> T {
|
auto operator()(std::string const &s) -> T {
|
||||||
if (auto r = consume_hex_prefix(s); !r.is_hexadecimal)
|
if (auto r = consume_hex_prefix(s); !r.is_hexadecimal)
|
||||||
throw std::invalid_argument{"chars_format::hex parses hexfloat"};
|
throw std::invalid_argument{"std::chars_format::hex parses hexfloat"};
|
||||||
|
|
||||||
return do_strtod<T>(s);
|
return do_strtod<T>(s);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T> struct parse_number<T, chars_format::scientific> {
|
template <class T> struct parse_number<T, std::chars_format::scientific> {
|
||||||
auto operator()(std::string const &s) -> T {
|
auto operator()(std::string const &s) -> T {
|
||||||
if (auto r = consume_hex_prefix(s); r.is_hexadecimal)
|
if (auto r = consume_hex_prefix(s); r.is_hexadecimal)
|
||||||
throw std::invalid_argument{
|
throw std::invalid_argument{
|
||||||
"chars_format::scientific does not parse hexfloat"};
|
"std::chars_format::scientific does not parse hexfloat"};
|
||||||
if (s.find_first_of("eE") == s.npos)
|
if (s.find_first_of("eE") == s.npos)
|
||||||
throw std::invalid_argument{
|
throw std::invalid_argument{
|
||||||
"chars_format::scientific requires exponent part"};
|
"std::chars_format::scientific requires exponent part"};
|
||||||
|
|
||||||
return do_strtod<T>(s);
|
return do_strtod<T>(s);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T> struct parse_number<T, chars_format::fixed> {
|
template <class T> struct parse_number<T, std::chars_format::fixed> {
|
||||||
auto operator()(std::string const &s) -> T {
|
auto operator()(std::string const &s) -> T {
|
||||||
if (auto r = consume_hex_prefix(s); r.is_hexadecimal)
|
if (auto r = consume_hex_prefix(s); r.is_hexadecimal)
|
||||||
throw std::invalid_argument{
|
throw std::invalid_argument{
|
||||||
"chars_format::fixed does not parse hexfloat"};
|
"std::chars_format::fixed does not parse hexfloat"};
|
||||||
if (s.find_first_of("eE") != s.npos)
|
if (s.find_first_of("eE") != s.npos)
|
||||||
throw std::invalid_argument{
|
throw std::invalid_argument{
|
||||||
"chars_format::fixed does not parse exponent part"};
|
"std::chars_format::fixed does not parse exponent part"};
|
||||||
|
|
||||||
return do_strtod<T>(s);
|
return do_strtod<T>(s);
|
||||||
}
|
}
|
||||||
@ -404,16 +397,16 @@ public:
|
|||||||
action(details::parse_number<T, 16>());
|
action(details::parse_number<T, 16>());
|
||||||
else if constexpr (is_one_of(Shape, 'a', 'A') &&
|
else if constexpr (is_one_of(Shape, 'a', 'A') &&
|
||||||
std::is_floating_point_v<T>)
|
std::is_floating_point_v<T>)
|
||||||
action(details::parse_number<T, details::chars_format::hex>());
|
action(details::parse_number<T, std::chars_format::hex>());
|
||||||
else if constexpr (is_one_of(Shape, 'e', 'E') &&
|
else if constexpr (is_one_of(Shape, 'e', 'E') &&
|
||||||
std::is_floating_point_v<T>)
|
std::is_floating_point_v<T>)
|
||||||
action(details::parse_number<T, details::chars_format::scientific>());
|
action(details::parse_number<T, std::chars_format::scientific>());
|
||||||
else if constexpr (is_one_of(Shape, 'f', 'F') &&
|
else if constexpr (is_one_of(Shape, 'f', 'F') &&
|
||||||
std::is_floating_point_v<T>)
|
std::is_floating_point_v<T>)
|
||||||
action(details::parse_number<T, details::chars_format::fixed>());
|
action(details::parse_number<T, std::chars_format::fixed>());
|
||||||
else if constexpr (is_one_of(Shape, 'g', 'G') &&
|
else if constexpr (is_one_of(Shape, 'g', 'G') &&
|
||||||
std::is_floating_point_v<T>)
|
std::is_floating_point_v<T>)
|
||||||
action(details::parse_number<T, details::chars_format::general>());
|
action(details::parse_number<T, std::chars_format::general>());
|
||||||
else
|
else
|
||||||
static_assert(alignof(T) == 0, "No scan specification for T");
|
static_assert(alignof(T) == 0, "No scan specification for T");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user