| 283 | } |
| 284 | |
| 285 | bool safe_strto32(StringPiece str, int32* value) { |
| 286 | SkipSpaces(&str); |
| 287 | |
| 288 | int64 vmax = kint32max; |
| 289 | int sign = 1; |
| 290 | if (absl::ConsumePrefix(&str, "-")) { |
| 291 | sign = -1; |
| 292 | // Different max for positive and negative integers. |
| 293 | ++vmax; |
| 294 | } |
| 295 | |
| 296 | if (!isdigit(SafeFirstChar(str))) return false; |
| 297 | |
| 298 | int64 result = 0; |
| 299 | do { |
| 300 | result = result * 10 + SafeFirstChar(str) - '0'; |
| 301 | if (result > vmax) { |
| 302 | return false; |
| 303 | } |
| 304 | str.remove_prefix(1); |
| 305 | } while (isdigit(SafeFirstChar(str))); |
| 306 | |
| 307 | SkipSpaces(&str); |
| 308 | |
| 309 | if (!str.empty()) return false; |
| 310 | |
| 311 | *value = static_cast<int32>(result * sign); |
| 312 | return true; |
| 313 | } |
| 314 | |
| 315 | bool safe_strtou32(StringPiece str, uint32* value) { |
| 316 | SkipSpaces(&str); |