rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
Read a value

Functions

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

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

876{
877 C4_STATIC_ASSERT(std::is_integral<I>::value);
878 C4_ASSERT(!s.empty());
879 *v = 0;
880 for(char c : s)
881 {
882 if(C4_UNLIKELY(c < '0' || c > '9'))
883 return false;
884 *v = ((*v) * I(10)) + (I(c) - I('0'));
885 }
886 return true;
887}
bool empty() const noexcept
Definition substr.hpp:356

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

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

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

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

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

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