| 60 | // success; 'out' is untouched if parsing fails. |
| 61 | template <typename T> |
| 62 | bool ParseInt(const char* s, T* out, |
| 63 | T min = std::numeric_limits<T>::min(), |
| 64 | T max = std::numeric_limits<T>::max()) { |
| 65 | int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10; |
| 66 | errno = 0; |
| 67 | char* end; |
| 68 | long long int result = strtoll(s, &end, base); |
| 69 | if (errno != 0 || s == end || *end != '\0') { |
| 70 | return false; |
| 71 | } |
| 72 | if (result < min || max < result) { |
| 73 | return false; |
| 74 | } |
| 75 | *out = static_cast<T>(result); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | // TODO: string_view |
| 80 | template <typename T> |