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

◆ operator()() [6/13]

void fl::float_conversion_visitor< double >::operator() ( const fl::string & str)
inline

Definition at line 443 of file types.h.

443 {
444 // NEW INSTRUCTIONS: AUTO CONVERT STRING TO FLOAT
445 // Try to parse the string as a float using FastLED's StringFormatter
446 // Validate by checking if string contains valid float characters
447 bool isValidFloat = true;
448 bool hasDecimal = false;
449 fl::size startPos = 0;
450
451 // Check for sign
452 if (str.length() > 0 && (str[0] == '+' || str[0] == '-')) {
453 startPos = 1;
454 }
455
456 // Check that all remaining characters are valid for a float
457 for (fl::size i = startPos; i < str.length(); i++) {
458 char c = str[i];
459 if (c == '.') {
460 if (hasDecimal) {
461 // Multiple decimal points
462 isValidFloat = false;
463 break;
464 }
465 hasDecimal = true;
466 } else if (!fl::isdigit(c) && c != 'e' && c != 'E') {
467 isValidFloat = false;
468 break;
469 }
470 }
471
472 // If it looks like a valid float, try to parse it
473 if (isValidFloat && str.length() > 0) {
474 // For simple cases, we can use a more precise approach
475 // Check if it's a simple decimal number
476 bool isSimpleDecimal = true;
477 for (fl::size i = startPos; i < str.length(); i++) {
478 char c = str[i];
479 if (c != '.' && !fl::isdigit(c)) {
480 isSimpleDecimal = false;
481 break;
482 }
483 }
484
485 if (isSimpleDecimal) {
486 // For simple decimals, we can do a more direct conversion
487 float parsed = fl::parseFloat(str.c_str(), str.length());
488 result = static_cast<FloatType>(parsed);
489 } else {
490 // For complex floats (with exponents), use the standard approach
491 float parsed = fl::parseFloat(str.c_str(), str.length());
492 result = static_cast<FloatType>(parsed);
493 }
494 }
495 }
float parseFloat(const char *str, fl::size len)
Parse a floating point number from a character buffer.
fl::optional< double > result
Definition types.h:506