rapidyaml  0.12.1
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 861 of file charconv.hpp.

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

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

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

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

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

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

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