rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
atod: chars to float64

Functions

bool c4::atod (csubstr str, double *v) noexcept
 Convert a string to a double precision real number.
size_t c4::atod_first (csubstr str, double *v) noexcept
 Convert a string to a double precision real number.

Detailed Description

Function Documentation

◆ atod()

bool c4::atod ( csubstr str,
double * v )
inlinenoexcept

Convert a string to a double precision real number.

The input string must be trimmed to the value, ie no leading or trailing whitespace can be present.

Returns
true iff the conversion succeeded
See also
atod_first() if the string is not trimmed

Definition at line 2182 of file charconv.hpp.

2183{
2184 C4_ASSERT(str.len > 0);
2185 C4_ASSERT(str.triml(" \r\t\n").len == str.len);
2186#if C4CORE_HAVE_FAST_FLOAT
2187 // fastfloat cannot parse hexadecimal floats
2188 bool isneg = (str.str[0] == '-');
2189 csubstr rem = str.sub(isneg || str.str[0] == '+');
2190 if( ! (rem.len >= 2 && (rem.str[0] == '0' && (rem.str[1] == 'x' || rem.str[1] == 'X'))))
2191 {
2192 fast_float::from_chars_result result;
2193 #ifndef CLANG_TIDY // suppress a false-positive error (cannot be done with NOLINT: https://stackoverflow.com/questions/62838193/ )
2194 result = fast_float::from_chars(str.str, str.str + str.len, *v);
2195 #else
2196 result = {};
2197 #endif
2198 return result.ec == std::errc();
2199 }
2200 else if(detail::scan_rhex(rem.sub(2), v))
2201 {
2202 *v *= isneg ? -1. : 1.;
2203 return true;
2204 }
2205 return false;
2206#elif C4CORE_HAVE_STD_FROMCHARS
2207 std::from_chars_result result;
2208 result = std::from_chars(str.str, str.str + str.len, *v);
2209 return result.ec == std::errc();
2210#else
2211 csubstr rem = str.sub(str.str[0] == '-' || str.str[0] == '+');
2212 if(!(rem.len >= 2 && (rem.str[0] == '0' && (rem.str[1] == 'x' || rem.str[1] == 'X'))))
2213 return detail::scan_one(str, "lf", v) != csubstr::npos;
2214 else
2215 return detail::scan_one(str, "la", v) != csubstr::npos;
2216#endif
2217}
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2356
basic_substring triml(const C c) const
trim left
Definition substr.hpp:629
size_t len
the length of the substring
Definition substr.hpp:218
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition substr.hpp:502
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

Referenced by atod_first(), atox(), and from_chars().

◆ atod_first()

size_t c4::atod_first ( csubstr str,
double * v )
inlinenoexcept

Convert a string to a double precision real number.

Leading whitespace is skipped until valid characters are found.

Returns
the number of characters read from the string, or npos if conversion was not successful or if the string was empty

Definition at line 2224 of file charconv.hpp.

2225{
2226 csubstr trimmed = str.first_real_span();
2227 if(trimmed.len == 0)
2228 return csubstr::npos;
2229 if(atod(trimmed, v))
2230 return static_cast<size_t>(trimmed.end() - str.begin());
2231 return csubstr::npos;
2232}
bool atod(csubstr str, double *v) noexcept
Convert a string to a double precision real number.
basic_substring first_real_span() const
get the first span which can be interpreted as a real (floating-point) number
Definition substr.hpp:1364
iterator begin() noexcept
Definition substr.hpp:360
iterator end() noexcept
Definition substr.hpp:361

Referenced by from_chars_first().