Custom integer parsing function for signed 32-bit integers
| 22 | |
| 23 | // Custom integer parsing function for signed 32-bit integers |
| 24 | bool parse_i32(const char* str, fl::i32& result) { |
| 25 | if (!str) return false; |
| 26 | |
| 27 | // Skip leading whitespace |
| 28 | while (*str && isSpace(*str)) { |
| 29 | str++; |
| 30 | } |
| 31 | |
| 32 | if (*str == '\0') return false; |
| 33 | |
| 34 | bool negative = false; |
| 35 | if (*str == '-') { |
| 36 | negative = true; |
| 37 | str++; |
| 38 | } else if (*str == '+') { |
| 39 | str++; |
| 40 | } |
| 41 | |
| 42 | if (*str == '\0' || !isDigit(*str)) return false; |
| 43 | |
| 44 | fl::u32 value = 0; |
| 45 | const fl::u32 max_div_10 = 214748364U; // INT32_MAX / 10 |
| 46 | const fl::u32 max_mod_10 = 7U; // INT32_MAX % 10 |
| 47 | const fl::u32 max_neg_div_10 = 214748364U; // -INT32_MIN / 10 |
| 48 | const fl::u32 max_neg_mod_10 = 8U; // -INT32_MIN % 10 |
| 49 | |
| 50 | while (*str && isDigit(*str)) { |
| 51 | fl::u32 digit = *str - '0'; |
| 52 | |
| 53 | // Check for overflow |
| 54 | if (negative) { |
| 55 | if (value > max_neg_div_10 || (value == max_neg_div_10 && digit > max_neg_mod_10)) { |
| 56 | return false; // Overflow |
| 57 | } |
| 58 | } else { |
| 59 | if (value > max_div_10 || (value == max_div_10 && digit > max_mod_10)) { |
| 60 | return false; // Overflow |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | value = value * 10 + digit; |
| 65 | str++; |
| 66 | } |
| 67 | |
| 68 | // Check if we stopped at a non-digit character (should be end of string for valid parse) |
| 69 | if (*str != '\0') return false; |
| 70 | |
| 71 | if (negative) { |
| 72 | result = -static_cast<fl::i32>(value); |
| 73 | } else { |
| 74 | result = static_cast<fl::i32>(value); |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | // Custom integer parsing function for unsigned 32-bit integers |
| 81 | bool parse_u32(const char* str, fl::u32& result) { |
no test coverage detected