rapidyaml  0.7.2
parse and emit YAML, and do it fast
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

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

1456 {
1457  C4_STATIC_ASSERT(std::is_integral<T>::value);
1458  C4_STATIC_ASSERT(std::is_signed<T>::value);
1459 
1460  if(C4_UNLIKELY(str.len == 0))
1461  return false;
1462 
1463  C4_ASSERT(str.str[0] != '+');
1464 
1465  T sign = 1;
1466  size_t start = 0;
1467  if(str.str[0] == '-')
1468  {
1469  if(C4_UNLIKELY(str.len == ++start))
1470  return false;
1471  sign = -1;
1472  }
1473 
1474  bool parsed_ok = true;
1475  if(str.str[start] != '0') // this should be the common case, so put it first
1476  {
1477  parsed_ok = read_dec(str.sub(start), v);
1478  }
1479  else if(str.len > start + 1)
1480  {
1481  // starts with 0: is it 0x, 0o, 0b?
1482  const char pfx = str.str[start + 1];
1483  if(pfx == 'x' || pfx == 'X')
1484  parsed_ok = str.len > start + 2 && read_hex(str.sub(start + 2), v);
1485  else if(pfx == 'b' || pfx == 'B')
1486  parsed_ok = str.len > start + 2 && read_bin(str.sub(start + 2), v);
1487  else if(pfx == 'o' || pfx == 'O')
1488  parsed_ok = str.len > start + 2 && read_oct(str.sub(start + 2), v);
1489  else
1490  parsed_ok = read_dec(str.sub(start + 1), v);
1491  }
1492  else
1493  {
1494  parsed_ok = read_dec(str.sub(start), v);
1495  }
1496  if(C4_LIKELY(parsed_ok))
1497  *v *= sign;
1498  return parsed_ok;
1499 }
bool read_bin(csubstr s, I *v) noexcept
read a binary integer from a string.
Definition: charconv.hpp:928
bool read_oct(csubstr s, I *v) noexcept
read an octal integer from a string.
Definition: charconv.hpp:958
bool read_hex(csubstr s, I *v) noexcept
read an hexadecimal integer from a string.
Definition: charconv.hpp:893
bool read_dec(csubstr s, I *v) noexcept
read a decimal integer from a string.
Definition: charconv.hpp:865

References c4::read_bin(), c4::read_dec(), c4::read_hex(), and c4::read_oct().

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

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

1510 {
1511  csubstr trimmed = str.first_int_span();
1512  if(trimmed.len == 0)
1513  return csubstr::npos;
1514  if(atoi(trimmed, v))
1515  return static_cast<size_t>(trimmed.end() - str.begin());
1516  return csubstr::npos;
1517 }
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.
Definition: charconv.hpp:1455
@ npos
a null string position
Definition: common.hpp:266

References c4::atoi(), and c4::yml::npos.

Referenced by c4::from_chars_first().