rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
atou: chars to unsigned

Functions

template<class T>
bool c4::atou (csubstr str, T *v) noexcept
 Convert a trimmed string to an unsigned integral value.
template<class T>
size_t c4::atou_first (csubstr str, T *v)
 Select the next range of characters in the string that can be parsed as an unsigned integral value, and convert it using atou().

Detailed Description

Function Documentation

◆ atou()

template<class T>
bool c4::atou ( csubstr str,
T * v )
noexcept

Convert a trimmed string to an unsigned integral value.

The string can be formatted as decimal, binary (prefix 0b or 0B), octal (prefix 0o or 0O) or hexadecimal (prefix 0x or 0X). Every character in the input string is read for the conversion; it must not contain any leading or trailing whitespace.

Returns
true if the conversion was successful.
Note
overflow is not detected: the return status is true even if the conversion would return a value outside of the type's range, in which case the result will wrap around the type's range. See overflows: does a number string overflow a type and Check read for overflow for overflow checking utilities.
If the string has a minus character, the return status will be false.
See also
atou_first() if the string is not trimmed to the value to read.

Definition at line 1558 of file charconv.hpp.

1559{
1560 C4_STATIC_ASSERT(std::is_integral<T>::value);
1561
1562 if(C4_UNLIKELY(str.len == 0 || str.front() == '-'))
1563 return false;
1564
1565 bool parsed_ok = true;
1566 if(str.str[0] != '0')
1567 {
1568 parsed_ok = read_dec(str, v);
1569 }
1570 else
1571 {
1572 if(str.len > 1)
1573 {
1574 const char pfx = str.str[1];
1575 if(pfx == 'x' || pfx == 'X')
1576 parsed_ok = str.len > 2 && read_hex(str.sub(2), v);
1577 else if(pfx == 'b' || pfx == 'B')
1578 parsed_ok = str.len > 2 && read_bin(str.sub(2), v);
1579 else if(pfx == 'o' || pfx == 'O')
1580 parsed_ok = str.len > 2 && read_oct(str.sub(2), v);
1581 else
1582 parsed_ok = read_dec(str, v);
1583 }
1584 else
1585 {
1586 *v = 0; // we know the first character is 0
1587 }
1588 }
1589 return parsed_ok;
1590}
bool read_bin(csubstr s, I *v) noexcept
read a binary integer from a string.
Definition charconv.hpp:938
bool read_oct(csubstr s, I *v) noexcept
read an octal integer from a string.
Definition charconv.hpp:968
bool read_hex(csubstr s, I *v) noexcept
read an hexadecimal integer from a string.
Definition charconv.hpp:903
bool read_dec(csubstr s, I *v) noexcept
read a decimal integer from a string.
Definition charconv.hpp:875
C & front() noexcept
Definition substr.hpp:372
size_t len
the length of the substring
Definition substr.hpp:218
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition substr.hpp:503
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

◆ atou_first()

template<class T>
size_t c4::atou_first ( csubstr str,
T * v )
inline

Select the next range of characters in the string that can be parsed as an unsigned integral value, and convert it using atou().

Leading whitespace (space, newline, tabs) is skipped.

Returns
the number of characters read for conversion, or csubstr::npos if the conversion faileds
See also
atou() if the string is already trimmed to the value to read.
csubstr::first_uint_span()

Definition at line 1600 of file charconv.hpp.

1601{
1602 csubstr trimmed = str.first_uint_span();
1603 if(trimmed.len && atou(trimmed, v))
1604 return static_cast<size_t>(trimmed.end() - str.begin());
1605 return csubstr::npos;
1606}
bool atou(csubstr str, T *v) noexcept
Convert a trimmed string to an unsigned integral value.
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2357
basic_substring first_uint_span() const
get the first span which can be interpreted as an unsigned integer
Definition substr.hpp:1278
iterator begin() noexcept
Definition substr.hpp:360
iterator end() noexcept
Definition substr.hpp:361