rapidyaml  0.8.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 1645 of file charconv.hpp.

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

References c4::yml::npos.