FastLED 3.9.15
Loading...
Searching...
No Matches

◆ parseFloat()

float fl::parseFloat ( const char * str,
fl::size len )

Parse a floating point number from a character buffer.

Parameters
strThe character buffer to parse
lenThe length of the buffer
Returns
The parsed float value (0.0f if parsing fails)

Definition at line 199 of file charconv.cpp.hpp.

199 {
200 float result = 0.0f; // The resulting number
201 float sign = 1.0f; // Positive or negative
202 float fraction = 0.0f; // Fractional part
203 float divisor = 1.0f; // Divisor for the fractional part
204 int isFractional = 0; // Whether the current part is fractional
205
206 fl::size pos = 0; // Current position in the string
207
208 // Handle empty input
209 if (len == 0) {
210 return 0.0f;
211 }
212
213 // Skip leading whitespace (manual check instead of isspace)
214 while (pos < len &&
215 (str[pos] == ' ' || str[pos] == '\t' || str[pos] == '\n' ||
216 str[pos] == '\r' || str[pos] == '\f' || str[pos] == '\v')) {
217 pos++;
218 }
219
220 // Handle optional sign
221 if (pos < len && str[pos] == '-') {
222 sign = -1.0f;
223 pos++;
224 } else if (pos < len && str[pos] == '+') {
225 pos++;
226 }
227
228 // Main parsing loop
229 while (pos < len) {
230 if (str[pos] >= '0' && str[pos] <= '9') {
231 if (isFractional) {
232 divisor *= 10.0f;
233 fraction += (str[pos] - '0') / divisor;
234 } else {
235 result = result * 10.0f + (str[pos] - '0');
236 }
237 } else if (str[pos] == '.' && !isFractional) {
238 isFractional = 1;
239 } else {
240 // Stop parsing at invalid characters
241 break;
242 }
243 pos++;
244 }
245
246 // Combine integer and fractional parts
247 result = result + fraction;
248
249 // Apply the sign
250 return sign * result;
251}
uint8_t pos
Definition Blur.ino:11
constexpr enable_if< is_fixed_point< T >::value, int >::type sign(T x) FL_NOEXCEPT
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

References pos, and sign().

Referenced by fl::float_conversion_visitor< FloatType >::operator()(), fl::float_conversion_visitor< double >::operator()(), and fl::basic_string::toFloat().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: