rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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

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

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