rapidyaml  0.11.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 1453 of file charconv.hpp.

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

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

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

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

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