rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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. More...
 
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(). More...
 

Detailed Description

Function Documentation

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 1466 of file charconv.hpp.

Referenced by c4::atoi_first(), c4::atox(), c4::from_chars(), and parse_args().

1467 {
1468  C4_STATIC_ASSERT(std::is_integral<T>::value);
1469  C4_STATIC_ASSERT(std::is_signed<T>::value);
1470 
1471  if C4_UNLIKELY(str.len == 0)
1472  return false;
1473  // no need for the assertion, but we leave it here to document
1474  // the expectation:
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_hex(csubstr s, I *v) noexcept
read an hexadecimal integer from a string.
Definition: charconv.hpp:902
const char * str
Definition: node_type.cpp:70
bool read_dec(csubstr s, I *v) noexcept
read a decimal integer from a string.
Definition: charconv.hpp:874
bool read_oct(csubstr s, I *v) noexcept
read an octal integer from a string.
Definition: charconv.hpp:967
bool read_bin(csubstr s, I *v) noexcept
read a binary integer from a string.
Definition: charconv.hpp:937
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.

Referenced by c4::from_chars_first().

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 }
const char * str
Definition: node_type.cpp:70
a null string position
Definition: common.hpp:138
basic_substring< const char > csubstr
an immutable string view
Definition: substr.hpp:2356
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.
Definition: charconv.hpp:1466