1#ifndef C4_YML_SCALAR_CHARCONV_HPP_
2#define C4_YML_SCALAR_CHARCONV_HPP_
6#ifndef C4_YML_COMMON_HPP_
9#ifndef C4_YML_ERROR_HPP_
12#ifndef C4_CHARCONV_HPP_
30 return s.str ==
nullptr ||
31 (s.len == 1 && (s.str[0] ==
'~')) ||
32 (s.len == 4 && ((0 == memcmp(
"null", s.str, 4))
33 || (0 == memcmp(
"Null", s.str, 4))
34 || (0 == memcmp(
"NULL", s.str, 4))));
46 (!(s.len > 1 && s.begins_with(
'0')))
61 case 'i':
return ((s[1] ==
'n') && (s[2] ==
'f'));
62 case 'I':
return ((s[1] ==
'n') && (s[2] ==
'f')) ||
63 ((s[1] ==
'N') && (s[2] ==
'F'));
76 case 'n':
return ((s[1] ==
'a') && (s[2] ==
'n'));
77 case 'N':
return ((s[1] ==
'a') && (s[2] ==
'N')) ||
78 ((s[1] ==
'a') && (s[2] ==
'n')) ||
79 ((s[1] ==
'A') && (s[2] ==
'N'));
92 case 'i':
return ((s[1] ==
'n') && (s[2] ==
'f'));
93 case 'I':
return ((s[1] ==
'n') && (s[2] ==
'f')) ||
94 ((s[1] ==
'N') && (s[2] ==
'F'));
95 case 'n':
return ((s[1] ==
'a') && (s[2] ==
'n'));
96 case 'N':
return ((s[1] ==
'a') && (s[2] ==
'N')) ||
97 ((s[1] ==
'a') && (s[2] ==
'n')) ||
98 ((s[1] ==
'A') && (s[2] ==
'N'));
108 return 0 == memcmp(
"true", s.str, 4)
109 || 0 == memcmp(
"null", s.str, 4)
113 return 0 == memcmp(
"false", s.str, 5)
114 || ((s[0] ==
'-' || s[0] ==
'+') && s[1] ==
'.' &&
scalar_is_inf3(s.str + 2));
126C4_NODISCARD C4_NO_INLINE
bool from_chars_float_yaml_special(
csubstr buf, T *C4_RESTRICT val)
RYML_NOEXCEPT
128 static_assert(std::is_floating_point<T>::value,
"must be floating point");
129 RYML_ASSERT_BASIC_(buf.len);
130 RYML_ASSERT_BASIC_(!buf.begins_with(
'+'));
138 *val = std::numeric_limits<T>::quiet_NaN();
143 *val = std::numeric_limits<T>::infinity();
149 if(buf.len == 5 && buf.str[1] ==
'.' &&
scalar_is_inf3(buf.str + 2))
151 *val = -std::numeric_limits<T>::infinity();
185 static_assert(std::is_floating_point<T>::value,
"must be floating point");
186 if C4_LIKELY(scalar.len > 0)
188 if(scalar.str[0] ==
'+')
189 scalar = scalar.sub(1);
192 else if(scalar.len == 4 || scalar.len == 5)
193 return detail::from_chars_float_yaml_special(scalar, val);
204 static_assert(std::is_floating_point<T>::value,
"must be floating point");
205 C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH(
"-Wfloat-equal");
206 if C4_UNLIKELY(std::isnan(val))
208 else if C4_UNLIKELY(val == std::numeric_limits<T>::infinity())
210 else if C4_UNLIKELY(val == -std::numeric_limits<T>::infinity())
213 C4_SUPPRESS_WARNING_GCC_CLANG_POP
229 if C4_LIKELY(scalar.len > 0)
231 if(scalar.str[0] ==
'+')
232 scalar = scalar.sub(1);
242#if (C4_CPP >= 17) || defined(__DOXYGEN__)
256 if constexpr (std::is_floating_point<T>::value)
258 else if constexpr (std::is_arithmetic<T>::value)
269 ->
typename std::enable_if<std::is_floating_point<T>::value,
bool>::type
277 ->
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_floating_point<T>::value,
bool>::type
285 ->
typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_floating_point<T>::value,
bool>::type
296#if (C4_CPP >= 17) || defined(__DOXYGEN__)
310 if constexpr (std::is_floating_point<T>::value)
321 ->
typename std::enable_if<std::is_floating_point<T>::value,
size_t>::type
329 ->
typename std::enable_if< ! std::is_floating_point<T>::value,
size_t>::type
Lightweight generic type-safe wrappers for converting individual values to/from strings.
Common utilities and infrastructure used by ryml.
#define RYML_NOEXCEPT
Conditionally expands to noexcept when RYML_USE_ASSERT is 0 and is empty otherwise.
Error utilities used by ryml.
bool from_chars(csubstr buf, uint8_t *v) noexcept
size_t scalar_serialize(substr buf, T const &a)
Serialize a scalar to the buffer, dispatching to to_chars() or to_chars_float() as appropriate.
bool scalar_is_inf_or_nan3(const char *s) noexcept
Same as scalar_is_inf3() || scalar_is_nan3().
bool scalar_is_null(csubstr s) noexcept
YAML-sense query of nullity.
bool scalar_is_special_json(csubstr s) noexcept
Query if a scalar is plain, eg, true, false, null, +-.inf or .nan.
bool scalar_deserialize(csubstr str, T *val)
Deserialize a scalar from its string representation, dispatching to one of from_chars(),...
bool scalar_is_inf3(const char *s) noexcept
Query if a scalar is inf (inf, Inf, INF).
bool scalar_is_plain_number_json(csubstr s) noexcept
JSON-sense query of plain number.
bool from_chars_integral(csubstr scalar, T *val) RYML_NOEXCEPT
Deserialize an integral scalar.
size_t to_chars_float(substr buf, T val) RYML_NOEXCEPT
Serialize a floating point value to a string.
bool scalar_is_nan3(const char *s) noexcept
Query if a scalar is nan (nan, NaN, Nan, NAN).
bool from_chars_float(csubstr scalar, T *val) RYML_NOEXCEPT
Deserialize a floating point from string.
basic_substring< char > substr
a mutable string view
basic_substring< const char > csubstr
an immutable string view
size_t to_chars(substr buf, escaped_scalar e)
formatting implementation to escape a scalar with escape_scalar()