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

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

References c4::yml::npos.