rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
atof: chars to float32

Functions

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

Detailed Description

Function Documentation

◆ atof()

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

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

◆ atof_first()

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

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