rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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

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.

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

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 }
const char * str
Definition: node_type.cpp:70
bool from_chars(csubstr buf, substr *v) noexcept
Definition: charconv.hpp:2615
a null string position
Definition: common.hpp:138
basic_substring< const char > csubstr
an immutable string view
Definition: substr.hpp:2356
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.

Referenced by c4::from_chars_first().

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.
Definition: charconv.hpp:2182
const char * str
Definition: node_type.cpp:70
a null string position
Definition: common.hpp:138
basic_substring< const char > csubstr
an immutable string view
Definition: substr.hpp:2356