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

◆ parse_i32()

bool fl::anonymous_namespace{istream.cpp.hpp}::parse_i32 ( const char * str,
fl::i32 & result )

Definition at line 25 of file istream.cpp.hpp.

25 {
26 if (!str) return false;
27
28 // Skip leading whitespace
29 while (*str && isSpace(*str)) {
30 str++;
31 }
32
33 if (*str == '\0') return false;
34
35 bool negative = false;
36 if (*str == '-') {
37 negative = true;
38 str++;
39 } else if (*str == '+') {
40 str++;
41 }
42
43 if (*str == '\0' || !isDigit(*str)) return false;
44
45 fl::u32 value = 0;
46 const fl::u32 max_div_10 = 214748364U; // INT32_MAX / 10
47 const fl::u32 max_mod_10 = 7U; // INT32_MAX % 10
48 const fl::u32 max_neg_div_10 = 214748364U; // -INT32_MIN / 10
49 const fl::u32 max_neg_mod_10 = 8U; // -INT32_MIN % 10
50
51 while (*str && isDigit(*str)) {
52 fl::u32 digit = *str - '0';
53
54 // Check for overflow
55 if (negative) {
56 if (value > max_neg_div_10 || (value == max_neg_div_10 && digit > max_neg_mod_10)) {
57 return false; // Overflow
58 }
59 } else {
60 if (value > max_div_10 || (value == max_div_10 && digit > max_mod_10)) {
61 return false; // Overflow
62 }
63 }
64
65 value = value * 10 + digit;
66 str++;
67 }
68
69 // Check if we stopped at a non-digit character (should be end of string for valid parse)
70 if (*str != '\0') return false;
71
72 if (negative) {
73 result = -static_cast<fl::i32>(value);
74 } else {
75 result = static_cast<fl::i32>(value);
76 }
77
78 return true;
79 }
constexpr int type_rank< T >::value
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

References fl::FL_NO_EXCEPT, isDigit(), isSpace(), parse_i32(), and fl::type_rank< T >::value.

Referenced by parse_i32().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: