rapidyaml 0.15.2
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 2121 of file charconv.hpp.

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}
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2356
basic_substring triml(const C c) const
trim left
Definition substr.hpp:629
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:502
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

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

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

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}
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:1364
iterator begin() noexcept
Definition substr.hpp:360
iterator end() noexcept
Definition substr.hpp:361

Referenced by from_chars_first().