rapidyaml  0.11.0
parse and emit YAML, and do it fast
atod: chars to float64

Functions

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

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

2172 {
2173  C4_ASSERT(str.len > 0);
2174  C4_ASSERT(str.triml(" \r\t\n").len == str.len);
2175 #if C4CORE_HAVE_FAST_FLOAT
2176  // fastfloat cannot parse hexadecimal floats
2177  bool isneg = (str.str[0] == '-');
2178  csubstr rem = str.sub(isneg || str.str[0] == '+');
2179  if(!(rem.len >= 2 && (rem.str[0] == '0' && (rem.str[1] == 'x' || rem.str[1] == 'X'))))
2180  {
2181  fast_float::from_chars_result result;
2182  #ifndef CLANG_TIDY // suppress a false-positive error (cannot be done with NOLINT: https://stackoverflow.com/questions/62838193/ )
2183  result = fast_float::from_chars(str.str, str.str + str.len, *v);
2184  #else
2185  result = {};
2186  #endif
2187  return result.ec == std::errc();
2188  }
2189  else if(detail::scan_rhex(rem.sub(2), v))
2190  {
2191  *v *= isneg ? -1. : 1.;
2192  return true;
2193  }
2194  return false;
2195 #elif C4CORE_HAVE_STD_FROMCHARS
2196  std::from_chars_result result;
2197  result = std::from_chars(str.str, str.str + str.len, *v);
2198  return result.ec == std::errc();
2199 #else
2200  csubstr rem = str.sub(str.str[0] == '-' || str.str[0] == '+');
2201  if(!(rem.len >= 2 && (rem.str[0] == '0' && (rem.str[1] == 'x' || rem.str[1] == 'X'))))
2202  return detail::scan_one(str, "lf", v) != csubstr::npos;
2203  else
2204  return detail::scan_one(str, "la", v) != csubstr::npos;
2205 #endif
2206 }
bool from_chars(csubstr buf, substr *v) noexcept
Definition: charconv.hpp:2599
@ npos
a null string position
Definition: common.hpp:258

References c4::from_chars(), and c4::yml::npos.

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

2214 {
2215  csubstr trimmed = str.first_real_span();
2216  if(trimmed.len == 0)
2217  return csubstr::npos;
2218  if(atod(trimmed, v))
2219  return static_cast<size_t>(trimmed.end() - str.begin());
2220  return csubstr::npos;
2221 }
bool atod(csubstr str, double *v) noexcept
Convert a string to a double precision real number.
Definition: charconv.hpp:2171

References c4::atod(), and c4::yml::npos.