rapidyaml  0.9.0
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 1454 of file charconv.hpp.

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

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

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

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

Referenced by c4::from_chars_first().