rapidyaml  0.13.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 1640 of file charconv.hpp.

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