rapidyaml  0.7.0
parse and emit YAML, and do it fast
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

◆ read_dec()

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

866 {
867  C4_STATIC_ASSERT(std::is_integral<I>::value);
868  C4_ASSERT(!s.empty());
869  *v = 0;
870  for(char c : s)
871  {
872  if(C4_UNLIKELY(c < '0' || c > '9'))
873  return false;
874  *v = (*v) * I(10) + (I(c) - I('0'));
875  }
876  return true;
877 }

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

◆ read_hex()

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

894 {
895  C4_STATIC_ASSERT(std::is_integral<I>::value);
896  C4_ASSERT(!s.empty());
897  *v = 0;
898  for(char c : s)
899  {
900  I cv;
901  if(c >= '0' && c <= '9')
902  cv = I(c) - I('0');
903  else if(c >= 'a' && c <= 'f')
904  cv = I(10) + (I(c) - I('a'));
905  else if(c >= 'A' && c <= 'F')
906  cv = I(10) + (I(c) - I('A'));
907  else
908  return false;
909  *v = (*v) * I(16) + cv;
910  }
911  return true;
912 }

Referenced by c4::atoi(), c4::atou(), and c4::yml::TagDirective::transform().

◆ read_bin()

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

929 {
930  C4_STATIC_ASSERT(std::is_integral<I>::value);
931  C4_ASSERT(!s.empty());
932  *v = 0;
933  for(char c : s)
934  {
935  *v <<= 1;
936  if(c == '1')
937  *v |= 1;
938  else if(c != '0')
939  return false;
940  }
941  return true;
942 }

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

◆ read_oct()

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

959 {
960  C4_STATIC_ASSERT(std::is_integral<I>::value);
961  C4_ASSERT(!s.empty());
962  *v = 0;
963  for(char c : s)
964  {
965  if(C4_UNLIKELY(c < '0' || c > '7'))
966  return false;
967  *v = (*v) * I(8) + (I(c) - I('0'));
968  }
969  return true;
970 }

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