rapidyaml 0.14.0
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 2181 of file charconv.hpp.

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

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

2224{
2225 csubstr trimmed = str.first_real_span();
2226 if(trimmed.len == 0)
2227 return csubstr::npos;
2228 if(atod(trimmed, v))
2229 return static_cast<size_t>(trimmed.end() - str.begin());
2230 return csubstr::npos;
2231}
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:1365
iterator begin() noexcept
Definition substr.hpp:360
iterator end() noexcept
Definition substr.hpp:361