rapidyaml  0.16.0
parseandemitYAML,anddoitfast
Read a value

Functions

template<class I >
bool c4::read_dec (csubstr s, I *v) noexcept
 read a decimal integer from a string. More...
 
template<class I >
bool c4::read_hex (csubstr s, I *v) noexcept
 read an hexadecimal integer from a string. More...
 
template<class I >
bool c4::read_bin (csubstr s, I *v) noexcept
 read a binary integer from a string. More...
 
template<class I >
bool c4::read_oct (csubstr s, I *v) noexcept
 read an octal integer from a string. More...
 

Detailed Description

Function Documentation

template<class I >
bool c4::read_dec ( csubstr  s,
I *  v 
)
inlinenoexcept

read a decimal integer from a string.

This is the lowest level (and the fastest) function to do this task.

Note
does not accept negative numbers
The string must be trimmed. Whitespace is not accepted.
the string must not be empty
there is no check for overflow; the value wraps around in a way similar to the standard C/C++ overflow behavior. For example, read_dec<int8_t>("128", &val) returns true and val will be set to 0 because 127 is the max i8 value.
See also
overflows<T>() to find out if a number string overflows a type range
Returns
true if the conversion was successful (no overflow check)

Definition at line 874 of file charconv.hpp.

Referenced by c4::atoi(), and c4::atou().

875 {
876  C4_STATIC_ASSERT(std::is_integral<I>::value);
877  C4_ASSERT(!s.empty());
878  *v = 0;
879  for(char c : s)
880  {
881  if C4_UNLIKELY(c < '0' || c > '9')
882  return false;
883  *v = ((*v) * I(10)) + (I(c) - I('0'));
884  }
885  return true;
886 }
template<class I >
bool c4::read_hex ( csubstr  s,
I *  v 
)
inlinenoexcept

read an hexadecimal integer from a string.

This is the lowest level (and the fastest) function to do this task.

Note
does not accept negative numbers
does not accept leading 0x or 0X
the string must not be empty
the string must be trimmed. Whitespace is not accepted.
there is no check for overflow; the value wraps around in a way similar to the standard C/C++ overflow behavior. For example, read_hex<int8_t>("80", &val) returns true and val will be set to 0 because 7f is the max i8 value.
See also
overflows<T>() to find out if a number string overflows a type range
Returns
true if the conversion was successful (no overflow check)

Definition at line 902 of file charconv.hpp.

Referenced by c4::atoi(), c4::atou(), and c4::decode_code_point().

903 {
904  C4_STATIC_ASSERT(std::is_integral<I>::value);
905  C4_ASSERT(!s.empty());
906  *v = 0;
907  for(char c : s)
908  {
909  I cv;
910  if(c >= '0' && c <= '9')
911  cv = I(c) - I('0');
912  else if(c >= 'a' && c <= 'f')
913  cv = I(10) + (I(c) - I('a'));
914  else if(c >= 'A' && c <= 'F')
915  cv = I(10) + (I(c) - I('A'));
916  else
917  return false;
918  *v = ((*v) * I(16)) + cv;
919  }
920  return true;
921 }
template<class I >
bool c4::read_bin ( csubstr  s,
I *  v 
)
inlinenoexcept

read a binary integer from a string.

This is the lowest level (and the fastest) function to do this task.

Note
does not accept negative numbers
does not accept leading 0b or 0B
the string must not be empty
the string must be trimmed. Whitespace is not accepted.
there is no check for overflow; the value wraps around in a way similar to the standard C/C++ overflow behavior. For example, read_bin<int8_t>("10000000", &val) returns true and val will be set to 0 because 1111111 is the max i8 value.
See also
overflows<T>() to find out if a number string overflows a type range
Returns
true if the conversion was successful (no overflow check)

Definition at line 937 of file charconv.hpp.

Referenced by c4::atoi(), and c4::atou().

938 {
939  C4_STATIC_ASSERT(std::is_integral<I>::value);
940  C4_ASSERT(!s.empty());
941  *v = 0;
942  for(char c : s)
943  {
944  *v <<= 1;
945  if(c == '1')
946  *v |= 1;
947  else if(c != '0')
948  return false;
949  }
950  return true;
951 }
template<class I >
bool c4::read_oct ( csubstr  s,
I *  v 
)
inlinenoexcept

read an octal integer from a string.

This is the lowest level (and the fastest) function to do this task.

Note
does not accept negative numbers
does not accept leading 0o or 0O
the string must not be empty
the string must be trimmed. Whitespace is not accepted.
there is no check for overflow; the value wraps around in a way similar to the standard C/C++ overflow behavior. For example, read_oct<int8_t>("200", &val) returns true and val will be set to 0 because 177 is the max i8 value.
See also
overflows<T>() to find out if a number string overflows a type range
Returns
true if the conversion was successful (no overflow check)

Definition at line 967 of file charconv.hpp.

Referenced by c4::atoi(), and c4::atou().

968 {
969  C4_STATIC_ASSERT(std::is_integral<I>::value);
970  C4_ASSERT(!s.empty());
971  *v = 0;
972  for(char c : s)
973  {
974  if C4_UNLIKELY(c < '0' || c > '7')
975  return false;
976  *v = ((*v) * I(8)) + (I(c) - I('0'));
977  }
978  return true;
979 }