3#ifndef TETL_STRINGS_TO_INTEGER_HPP
4#define TETL_STRINGS_TO_INTEGER_HPP
23template <
integral Int>
24struct nop_overflow_checker {
25 explicit constexpr nop_overflow_checker(Int )
noexcept { }
27 [[nodiscard]]
constexpr auto operator()(Int , Int )
const noexcept ->
bool {
return false; }
30template <
integral Int>
31struct unsigned_overflow_checker {
32 explicit constexpr unsigned_overflow_checker(Int base) noexcept
37 [[nodiscard]]
constexpr auto operator()(Int value, Int digit)
const noexcept ->
bool
39 return value > _maxDivBase or (value == _maxDivBase and digit > _maxModBase);
48template <
integral Int>
49struct signed_overflow_checker {
50 explicit constexpr signed_overflow_checker(Int base) noexcept
55 [[nodiscard]]
constexpr auto operator()(Int value, Int digit)
const noexcept ->
bool
57 return value < _minDivBase or (value == _minDivBase and digit > _minModBase);
66template <
integral Int,
bool Check>
70 nop_overflow_checker<Int>>;
85template <
integral Int>
87 char const*
end{
nullptr};
92template <
integral Int, to_integer_options Options = to_integer_options{}>
95 auto const length = str.
size();
96 auto const wouldOverflow = detail::overflow_checker<Int, Options.check_overflow>{base};
98 auto const parseDigit = [](
int ch) -> Int {
100 return static_cast<Int
>(ch -
int{
'0'});
103 return static_cast<Int
>(
static_cast<Int
>(
etl::tolower(ch)) - Int{
'a'} + Int{10});
109 if constexpr (Options.skip_whitespace) {
110 while (pos != length and
etl::isspace(
static_cast<int>(str[pos]))) {
121 [[maybe_unused]]
auto positive =
true;
123 if (str[pos] ==
'-') {
125 if (++pos == length) {
134 auto const ch =
static_cast<int>(str[pos++]);
135 auto const digit =
static_cast<Int
>(parseDigit(ch));
137 return static_cast<Int
>(-digit);
148 for (; pos != length; ++pos) {
149 auto const digit = parseDigit(
static_cast<int>(str[pos]));
154 if (wouldOverflow(value, digit)) {
159 value =
static_cast<Int
>(value * base - digit);
161 value =
static_cast<Int
>(value * base + digit);
The concept integral<T> is satisfied if and only if T is an integral type.
Definition integral.hpp:13
The concept signed_integral<T> is satisfied if and only if T is an integral type and is_signed_v<T> i...
Definition signed_integral.hpp:15
constexpr auto isalpha(int ch) noexcept -> int
Checks if the given character is an alphabetic character as classified by the default C locale.
Definition isalpha.hpp:18
constexpr auto isdigit(int ch) noexcept -> int
Checks if the given character is one of the 10 decimal digits: 0123456789.
Definition isdigit.hpp:15
constexpr auto isspace(int ch) noexcept -> int
Checks if the given character is whitespace character as classified by the default C locale.
Definition isspace.hpp:19
constexpr auto tolower(int ch) noexcept -> int
Converts the given character to lowercase according to the character conversion rules defined by the ...
Definition tolower.hpp:26
constexpr auto abs(complex< T > const &z) -> T
Definition abs.hpp:13
constexpr auto end(C &c) -> decltype(c.end())
Returns an iterator to the end (i.e. the element after the last element) of the given container c or ...
Definition end.hpp:14
constexpr auto next(InputIt it, typename iterator_traits< InputIt >::difference_type n=1) -> InputIt
Return the nth successor of iterator it.
Definition next.hpp:14
basic_string_view< char, etl::char_traits< char > > string_view
Typedef for common character type char
Definition basic_string_view.hpp:704
to_integer_error
Definition to_integer.hpp:79
@ overflow
Definition to_integer.hpp:82
@ invalid_input
Definition to_integer.hpp:81
@ none
Definition to_integer.hpp:80
@ overflow
Definition from_floating_point.hpp:20
@ none
Definition from_floating_point.hpp:19
constexpr auto to_integer(string_view str, Int base=Int(10)) noexcept -> to_integer_result< Int >
Definition to_integer.hpp:93
@ invalid_input
Definition to_floating_point.hpp:15
typename conditional< B, T, F >::type conditional_t
Definition conditional.hpp:21
TETL_BUILTIN_PTRDIFF ptrdiff_t
etl::ptrdiff_t is the signed integer type of the result of subtracting two pointers.
Definition ptrdiff_t.hpp:14
constexpr auto data() const noexcept -> const_pointer
Returns a pointer to the underlying character array. The pointer is such that the range [data(); data...
Definition basic_string_view.hpp:171
constexpr auto size() const noexcept -> size_type
Returns the number of Char elements in the view, i.e. etl::distance(begin(), end()).
Definition basic_string_view.hpp:174
static constexpr auto max() noexcept
Definition numeric_limits.hpp:21
static constexpr auto min() noexcept
Definition numeric_limits.hpp:20
Definition to_integer.hpp:74
bool check_overflow
Definition to_integer.hpp:76
bool skip_whitespace
Definition to_integer.hpp:75
Definition to_integer.hpp:86
to_integer_error error
Definition to_integer.hpp:88
Int value
Definition to_integer.hpp:89
char const * end
Definition to_integer.hpp:87