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

◆ operator()() [7/13]

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

Definition at line 540 of file types.h.

540 {
541 // NEW INSTRUCTIONS: AUTO CONVERT STRING TO FLOAT
542 // Try to parse the string as a float using FastLED's StringFormatter
543 // Validate by checking if string contains valid float characters
544 bool isValidFloat = true;
545 bool hasDecimal = false;
546 fl::size startPos = 0;
547
548 // Check for sign
549 if (str.length() > 0 && (str[0] == '+' || str[0] == '-')) {
550 startPos = 1;
551 }
552
553 // Check that all remaining characters are valid for a float
554 for (fl::size i = startPos; i < str.length(); i++) {
555 char c = str[i];
556 if (c == '.') {
557 if (hasDecimal) {
558 // Multiple decimal points
559 isValidFloat = false;
560 break;
561 }
562 hasDecimal = true;
563 } else if (!fl::isdigit(c) && c != 'e' && c != 'E') {
564 isValidFloat = false;
565 break;
566 }
567 }
568
569 // If it looks like a valid float, try to parse it
570 if (isValidFloat && str.length() > 0) {
571 // For simple cases, we can use a more precise approach
572 // Check if it's a simple decimal number
573 bool isSimpleDecimal = true;
574 for (fl::size i = startPos; i < str.length(); i++) {
575 char c = str[i];
576 if (c != '.' && !fl::isdigit(c)) {
577 isSimpleDecimal = false;
578 break;
579 }
580 }
581
582 if (isSimpleDecimal) {
583 // For simple decimals, we can do a more direct conversion
584 float parsed = fl::parseFloat(str.c_str(), str.length());
585 result = static_cast<double>(parsed);
586 } else {
587 // For complex floats (with exponents), use the standard approach
588 float parsed = fl::parseFloat(str.c_str(), str.length());
589 result = static_cast<double>(parsed);
590 }
591 }
592 }
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

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

+ Here is the call graph for this function: