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

◆ operator()() [6/13]

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

Definition at line 458 of file json.h.

460 {
461 // NEW INSTRUCTIONS: AUTO CONVERT STRING TO FLOAT
462 // Try to parse the string as a float using FastLED's StringFormatter
463 // Validate by checking if string contains valid float characters
464 bool isValidFloat = true;
465 bool hasDecimal = false;
466 fl::size startPos = 0;
467
468 // Check for sign
469 if (str.length() > 0 && (str[0] == '+' || str[0] == '-')) {
470 startPos = 1;
471 }
472
473 // Check that all remaining characters are valid for a float
474 for (fl::size i = startPos; i < str.length(); i++) {
475 char c = str[i];
476 if (c == '.') {
477 if (hasDecimal) {
478 // Multiple decimal points
479 isValidFloat = false;
480 break;
481 }
482 hasDecimal = true;
483 } else if (!StringFormatter::isDigit(c) && c != 'e' && c != 'E') {
484 isValidFloat = false;
485 break;
486 }
487 }
488
489 // If it looks like a valid float, try to parse it
490 if (isValidFloat && str.length() > 0) {
491 // For simple cases, we can use a more precise approach
492 // Check if it's a simple decimal number
493 bool isSimpleDecimal = true;
494 for (fl::size i = startPos; i < str.length(); i++) {
495 char c = str[i];
496 if (c != '.' && !StringFormatter::isDigit(c)) {
497 isSimpleDecimal = false;
498 break;
499 }
500 }
501
502 if (isSimpleDecimal) {
503 // For simple decimals, we can do a more direct conversion
504 float parsed = StringFormatter::parseFloat(str.c_str(), str.length());
505 result = static_cast<FloatType>(parsed);
506 } else {
507 // For complex floats (with exponents), use the standard approach
508 float parsed = StringFormatter::parseFloat(str.c_str(), str.length());
509 result = static_cast<FloatType>(parsed);
510 }
static float parseFloat(const char *str, fl::size len)
Definition str.cpp:296
fl::optional< double > result
Definition json.h:521