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

◆ operator()() [7/13]

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

Definition at line 546 of file json.h.

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