Custom integer parsing function for unsigned 32-bit integers
| 79 | |
| 80 | // Custom integer parsing function for unsigned 32-bit integers |
| 81 | bool parse_u32(const char* str, fl::u32& result) { |
| 82 | if (!str) return false; |
| 83 | |
| 84 | // Skip leading whitespace |
| 85 | while (*str && isSpace(*str)) { |
| 86 | str++; |
| 87 | } |
| 88 | |
| 89 | if (*str == '\0') return false; |
| 90 | |
| 91 | // Optional '+' sign (no '-' for unsigned) |
| 92 | if (*str == '+') { |
| 93 | str++; |
| 94 | } else if (*str == '-') { |
| 95 | return false; // Negative not allowed for unsigned |
| 96 | } |
| 97 | |
| 98 | if (*str == '\0' || !isDigit(*str)) return false; |
| 99 | |
| 100 | fl::u32 value = 0; |
| 101 | const fl::u32 max_div_10 = 429496729U; // UINT32_MAX / 10 |
| 102 | const fl::u32 max_mod_10 = 5U; // UINT32_MAX % 10 |
| 103 | |
| 104 | while (*str && isDigit(*str)) { |
| 105 | fl::u32 digit = *str - '0'; |
| 106 | |
| 107 | // Check for overflow |
| 108 | if (value > max_div_10 || (value == max_div_10 && digit > max_mod_10)) { |
| 109 | return false; // Overflow |
| 110 | } |
| 111 | |
| 112 | value = value * 10 + digit; |
| 113 | str++; |
| 114 | } |
| 115 | |
| 116 | // Check if we stopped at a non-digit character (should be end of string for valid parse) |
| 117 | if (*str != '\0') return false; |
| 118 | |
| 119 | result = value; |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS |
no test coverage detected