| 428 | } |
| 429 | |
| 430 | bool ParseInt32(const std::string& str, int32_t *out) |
| 431 | { |
| 432 | if (!ParsePrechecks(str)) |
| 433 | return false; |
| 434 | char *endp = NULL; |
| 435 | errno = 0; // strtol will not set errno if valid |
| 436 | long int n = strtol(str.c_str(), &endp, 10); |
| 437 | if(out) *out = (int32_t)n; |
| 438 | // Note that strtol returns a *long int*, so even if strtol doesn't report a over/underflow |
| 439 | // we still have to check that the returned value is within the range of an *int32_t*. On 64-bit |
| 440 | // platforms the size of these types may be different. |
| 441 | return endp && *endp == 0 && !errno && |
| 442 | n >= std::numeric_limits<int32_t>::min() && |
| 443 | n <= std::numeric_limits<int32_t>::max(); |
| 444 | } |
| 445 | |
| 446 | bool ParseInt64(const std::string& str, int64_t *out) |
| 447 | { |