mirror of
https://codeberg.org/comaps/comaps
synced 2025-12-20 05:13:58 +00:00
[base] Added to_uint base for string_view.
Signed-off-by: Viktor Govako <viktor.govako@gmail.com>
This commit is contained in:
committed by
Konstantin Pastbin
parent
1d29e7816a
commit
72ff90defd
@@ -269,7 +269,11 @@ UNIT_TEST(to_uint)
|
||||
TEST(strings::to_uint("AF", i, 16), ());
|
||||
TEST_EQUAL(175, i, ());
|
||||
|
||||
TEST(strings::to_uint(std::string_view("C8"), i, 16), ());
|
||||
TEST_EQUAL(200, i, ());
|
||||
|
||||
TEST(!strings::to_uint("AXF", i, 16), ());
|
||||
TEST(!strings::to_uint(std::string_view("AXF"), i, 16), ());
|
||||
|
||||
uint8_t i8;
|
||||
TEST(!strings::to_uint(std::string_view("256"), i8), ());
|
||||
|
||||
@@ -538,19 +538,19 @@ bool ToInteger(char const * start, T & result, int base = 10)
|
||||
namespace impl
|
||||
{
|
||||
template <typename T>
|
||||
bool from_sv(std::string_view sv, T & t)
|
||||
bool from_sv(std::string_view sv, T & t, int base = 10)
|
||||
{
|
||||
auto const end = sv.data() + sv.size();
|
||||
auto const res = std::from_chars(sv.data(), end, t);
|
||||
auto const res = std::from_chars(sv.data(), end, t, base);
|
||||
return (res.ec != std::errc::invalid_argument && res.ec != std::errc::result_out_of_range && res.ptr == end);
|
||||
}
|
||||
} // namespace impl
|
||||
|
||||
template <class T>
|
||||
inline bool to_uint(std::string_view sv, T & i)
|
||||
inline bool to_uint(std::string_view sv, T & i, int base = 10)
|
||||
{
|
||||
static_assert(std::is_unsigned<T>::value, "");
|
||||
return impl::from_sv(sv, i);
|
||||
return impl::from_sv(sv, i, base);
|
||||
}
|
||||
|
||||
inline bool to_double(std::string_view sv, double & d)
|
||||
|
||||
Reference in New Issue
Block a user