rapidyaml  0.9.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 862 of file charconv.hpp.

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

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

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

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

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

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

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

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