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

◆ atoff()

static float fl::string_functions::atoff ( const char * str,
size_t len )
static

Definition at line 45 of file str.cpp.

45 {
46 float result = 0.0f; // The resulting number
47 float sign = 1.0f; // Positive or negative
48 float fraction = 0.0f; // Fractional part
49 float divisor = 1.0f; // Divisor for the fractional part
50 int isFractional = 0; // Whether the current part is fractional
51
52 size_t pos = 0; // Current position in the string
53
54 // Handle empty input
55 if (len == 0) {
56 return 0.0f;
57 }
58
59 // Skip leading whitespace (manual check instead of isspace)
60 while (pos < len && (str[pos] == ' ' || str[pos] == '\t' || str[pos] == '\n' || str[pos] == '\r' || str[pos] == '\f' || str[pos] == '\v')) {
61 pos++;
62 }
63
64 // Handle optional sign
65 if (pos < len && str[pos] == '-') {
66 sign = -1.0f;
67 pos++;
68 } else if (pos < len && str[pos] == '+') {
69 pos++;
70 }
71
72 // Main parsing loop
73 while (pos < len) {
74 if (str[pos] >= '0' && str[pos] <= '9') {
75 if (isFractional) {
76 divisor *= 10.0f;
77 fraction += (str[pos] - '0') / divisor;
78 } else {
79 result = result * 10.0f + (str[pos] - '0');
80 }
81 } else if (str[pos] == '.' && !isFractional) {
82 isFractional = 1;
83 } else {
84 // Stop parsing at invalid characters
85 break;
86 }
87 pos++;
88 }
89
90 // Combine integer and fractional parts
91 result = result + fraction;
92
93 // Apply the sign
94 return sign * result;
95}
uint8_t pos
Definition Blur.ino:11

References pos.

Referenced by fl::StringFormatter::parseFloat().

+ Here is the caller graph for this function: