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

Functions

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

Detailed Description

Function Documentation

◆ atoi()

template<class T>
bool c4::atoi ( csubstr str,
T * v )
inlinenoexcept

Convert a trimmed string to a signed integral value.

The input string can be formatted as decimal, binary (prefix 0b or 0B), octal (prefix 0o or 0O) or hexadecimal (prefix 0x or 0X). Strings with leading zeroes are considered as decimal and not octal (unlike the C/C++ convention). Every character in the input string is read for the conversion; the input string must not contain any leading or trailing whitespace.

Returns
true if the conversion was successful.
Note
a positive sign is not accepted. ie, the string must not start with '+'
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. This is similar to native behavior. See overflows: does a number string overflow a type and Check read for overflow for overflow checking utilities.
See also
atoi_first() if the string is not trimmed to the value to read.

Definition at line 1467 of file charconv.hpp.

1468{
1469 C4_STATIC_ASSERT(std::is_integral<T>::value);
1470 C4_STATIC_ASSERT(std::is_signed<T>::value);
1471
1472 if(C4_UNLIKELY(str.len == 0))
1473 return false;
1474
1475 C4_ASSERT(str.str[0] != '+');
1476
1477 T sign = 1;
1478 size_t start = 0;
1479 if(str.str[0] == '-')
1480 {
1481 if(C4_UNLIKELY(str.len == ++start))
1482 return false;
1483 sign = -1;
1484 }
1485
1486 bool parsed_ok = true;
1487 if(str.str[start] != '0') // this should be the common case, so put it first
1488 {
1489 parsed_ok = read_dec(str.sub(start), v);
1490 }
1491 else if(str.len > start + 1)
1492 {
1493 // starts with 0: is it 0x, 0o, 0b?
1494 const char pfx = str.str[start + 1];
1495 if(pfx == 'x' || pfx == 'X')
1496 parsed_ok = str.len > start + 2 && read_hex(str.sub(start + 2), v);
1497 else if(pfx == 'b' || pfx == 'B')
1498 parsed_ok = str.len > start + 2 && read_bin(str.sub(start + 2), v);
1499 else if(pfx == 'o' || pfx == 'O')
1500 parsed_ok = str.len > start + 2 && read_oct(str.sub(start + 2), v);
1501 else
1502 parsed_ok = read_dec(str.sub(start + 1), v);
1503 }
1504 else
1505 {
1506 parsed_ok = read_dec(str.sub(start), v);
1507 }
1508 if(C4_LIKELY(parsed_ok))
1509 *v *= sign;
1510 return parsed_ok;
1511}
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
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

◆ atoi_first()

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

Select the next range of characters in the string that can be parsed as a signed integral value, and convert it using atoi().

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

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

Definition at line 1521 of file charconv.hpp.

1522{
1523 csubstr trimmed = str.first_int_span();
1524 if(trimmed.len && atoi(trimmed, v))
1525 return static_cast<size_t>(trimmed.end() - str.begin());
1526 return csubstr::npos;
1527}
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2357
basic_substring first_int_span() const
get the first span which can be interpreted as a signed integer
Definition substr.hpp:1290
iterator begin() noexcept
Definition substr.hpp:360
iterator end() noexcept
Definition substr.hpp:361