subroutine that parses only digits
| 23 | |
| 24 | // subroutine that parses only digits |
| 25 | static bool _parseUInt(const char *str NONNULL, uint64_t &result, bool allowTrailing) { |
| 26 | uint64_t n = 0; |
| 27 | if (!isdigit(*str)) |
| 28 | return false; |
| 29 | while (isdigit(*str)) { |
| 30 | int digit = (*str++ - '0'); |
| 31 | if (_usuallyFalse(n > UINT64_MAX / 10)) |
| 32 | return false; |
| 33 | n *= 10; |
| 34 | if (_usuallyFalse(n > UINT64_MAX - digit)) |
| 35 | return false; |
| 36 | n += digit; |
| 37 | } |
| 38 | if (!allowTrailing) { |
| 39 | while (isspace(*str)) |
| 40 | ++str; |
| 41 | if (_usuallyFalse(*str != '\0')) |
| 42 | return false; |
| 43 | } |
| 44 | result = n; |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | // Unsigned version: |
| 49 | bool ParseInteger(const char *str NONNULL, uint64_t &result, bool allowTrailing) { |