FastLED 3.10.8
Loading...
Searching...
No Matches

◆ operator()() [7/13]

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

Definition at line 593 of file types.h.

593 {
594 // NEW INSTRUCTIONS: AUTO CONVERT STRING TO FLOAT
595 // Try to parse the string as a float using FastLED's StringFormatter
596 // Validate by checking if string contains valid float characters
597 bool isValidFloat = true;
598 bool hasDecimal = false;
599 fl::size startPos = 0;
600
601 // Check for sign
602 if (str.length() > 0 && (str[0] == '+' || str[0] == '-')) {
603 startPos = 1;
604 }
605
606 // Check that all remaining characters are valid for a float
607 for (fl::size i = startPos; i < str.length(); i++) {
608 char c = str[i];
609 if (c == '.') {
610 if (hasDecimal) {
611 // Multiple decimal points
612 isValidFloat = false;
613 break;
614 }
615 hasDecimal = true;
616 } else if (!fl::isdigit(c) && c != 'e' && c != 'E') {
617 isValidFloat = false;
618 break;
619 }
620 }
621
622 // If it looks like a valid float, try to parse it
623 if (isValidFloat && str.length() > 0) {
624 // For simple cases, we can use a more precise approach
625 // Check if it's a simple decimal number
626 bool isSimpleDecimal = true;
627 for (fl::size i = startPos; i < str.length(); i++) {
628 char c = str[i];
629 if (c != '.' && !fl::isdigit(c)) {
630 isSimpleDecimal = false;
631 break;
632 }
633 }
634
635#if FL_PLATFORM_HAS_LARGE_MEMORY
636 if (isSimpleDecimal) {
637 // For simple decimals, we can do a more direct conversion
638 float parsed = fl::parseFloat(str.c_str(), str.length());
639 result = static_cast<double>(parsed);
640 } else {
641 // For complex floats (with exponents), use the standard approach
642 float parsed = fl::parseFloat(str.c_str(), str.length());
643 result = static_cast<double>(parsed);
644 }
645#else
646 // Low-memory gate per FastLED #3224 Tier 1A; see scalar specialization.
648 result = 0.0;
649#endif
650 }
651 }
float parseFloat(const char *str, fl::size len) FL_NO_EXCEPT
Parse a floating point number from a character buffer.
fl::optional< double > result
Definition types.h:553

References fl::FL_NO_EXCEPT, fl::isdigit(), fl::parseFloat(), and result.

+ Here is the call graph for this function: