rapidyaml  0.9.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 2172 of file charconv.hpp.

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

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

Referenced by c4::atod_first(), c4::atox(), and c4::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 2210 of file charconv.hpp.

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

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

Referenced by c4::from_chars_first().