rapidyaml  0.16.0
parseandemitYAML,anddoitfast
atof: chars to float32

Functions

bool c4::atof (csubstr str, float *v) noexcept
 Convert a string to a single precision real number. More...
 
size_t c4::atof_first (csubstr str, float *v) noexcept
 Convert a string to a single precision real number. More...
 

Detailed Description

Function Documentation

bool c4::atof ( csubstr  str,
float *  v 
)
inlinenoexcept

Convert a string to a single 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
atof_first() if the string is not trimmed

Definition at line 2121 of file charconv.hpp.

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

2122 {
2123  C4_ASSERT(str.len > 0);
2124  C4_ASSERT(str.triml(" \r\t\n").len == str.len);
2125 #if C4CORE_HAVE_FAST_FLOAT
2126  // fastfloat cannot parse hexadecimal floats
2127  bool isneg = (str.str[0] == '-');
2128  csubstr rem = str.sub(isneg || str.str[0] == '+');
2129  if( ! (rem.len >= 2 && (rem.str[0] == '0' && (rem.str[1] == 'x' || rem.str[1] == 'X'))))
2130  {
2131  fast_float::from_chars_result result;
2132  result = fast_float::from_chars(str.str, str.str + str.len, *v);
2133  return result.ec == std::errc();
2134  }
2135  else if(detail::scan_rhex(rem.sub(2), v))
2136  {
2137  *v *= isneg ? -1.f : 1.f;
2138  return true;
2139  }
2140  return false;
2141 #elif C4CORE_HAVE_STD_FROMCHARS
2142  std::from_chars_result result;
2143  result = std::from_chars(str.str, str.str + str.len, *v);
2144  return result.ec == std::errc();
2145 #else
2146  csubstr rem = str.sub(str.str[0] == '-' || str.str[0] == '+');
2147  if(!(rem.len >= 2 && (rem.str[0] == '0' && (rem.str[1] == 'x' || rem.str[1] == 'X'))))
2148  return detail::scan_one(str, "f", v) != csubstr::npos;
2149  else
2150  return detail::scan_one(str, "a", v) != csubstr::npos;
2151 #endif
2152 }
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::atof_first ( csubstr  str,
float *  v 
)
inlinenoexcept

Convert a string to a single 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 2159 of file charconv.hpp.

Referenced by c4::from_chars_first().

2160 {
2161  csubstr trimmed = str.first_real_span();
2162  if(trimmed.len == 0)
2163  return csubstr::npos;
2164  if(atof(trimmed, v))
2165  return static_cast<size_t>(trimmed.end() - str.begin());
2166  return csubstr::npos;
2167 }
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
bool atof(csubstr str, float *v) noexcept
Convert a string to a single precision real number.
Definition: charconv.hpp:2121