rapidyaml  0.7.0
parse and emit YAML, and do it fast
overflows: does a number string overflow a type

Functions

template<class T >
auto c4::overflows (csubstr str) noexcept -> typename std::enable_if< std::is_unsigned< T >::value, bool >::type
 Test if the following string would overflow when converted to associated integral types; this function is dispatched with SFINAE to handle differently signed and unsigned types. More...
 

Detailed Description

Function Documentation

◆ overflows()

template<class T >
auto c4::overflows ( csubstr  str) -> typename std::enable_if<std::is_unsigned<T>::value, bool>::type
noexcept

Test if the following string would overflow when converted to associated integral types; this function is dispatched with SFINAE to handle differently signed and unsigned types.

Returns
true if number will overflow, false if it fits (or doesn't parse)
See also
Check read for overflow for format specifiers to enforce no-overflow reads

Definition at line 1649 of file charconv.hpp.

1651 {
1652  C4_STATIC_ASSERT(std::is_integral<T>::value);
1653 
1654  if(C4_UNLIKELY(str.len == 0))
1655  {
1656  return false;
1657  }
1658  else if(str.str[0] == '0')
1659  {
1660  if (str.len == 1)
1661  return false;
1662  switch (str.str[1])
1663  {
1664  case 'x':
1665  case 'X':
1666  {
1667  size_t fno = str.first_not_of('0', 2);
1668  if (fno == csubstr::npos)
1669  return false;
1670  return !(str.len <= fno + (sizeof(T) * 2));
1671  }
1672  case 'b':
1673  case 'B':
1674  {
1675  size_t fno = str.first_not_of('0', 2);
1676  if (fno == csubstr::npos)
1677  return false;
1678  return !(str.len <= fno +(sizeof(T) * 8));
1679  }
1680  case 'o':
1681  case 'O':
1682  {
1683  size_t fno = str.first_not_of('0', 2);
1684  if(fno == csubstr::npos)
1685  return false;
1686  return detail::charconv_digits<T>::is_oct_overflow(str.sub(fno));
1687  }
1688  default:
1689  {
1690  size_t fno = str.first_not_of('0', 1);
1691  if(fno == csubstr::npos)
1692  return false;
1693  return detail::check_overflow(str.sub(fno), detail::charconv_digits<T>::max_value_dec());
1694  }
1695  }
1696  }
1697  else if(C4_UNLIKELY(str[0] == '-'))
1698  {
1699  return true;
1700  }
1701  else
1702  {
1703  return detail::check_overflow(str, detail::charconv_digits<T>::max_value_dec());
1704  }
1705 }
@ npos
a null string position
Definition: common.hpp:266

References c4::yml::npos.