| 104 | // boolean success; 'out' is untouched if parsing fails. |
| 105 | template <typename T> |
| 106 | LIBBASE_ALWAYS_INLINE bool ParseInt(const char* s, T* out, T min = std::numeric_limits<T>::min(), |
| 107 | T max = std::numeric_limits<T>::max()) { |
| 108 | static_assert(std::is_signed<T>::value, "ParseInt can only be used with signed types"); |
| 109 | while (isspace(*s)) { |
| 110 | s++; |
| 111 | } |
| 112 | |
| 113 | int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10; |
| 114 | errno = 0; |
| 115 | char* end; |
| 116 | long long int result = strtoll(s, &end, base); |
| 117 | if (errno != 0) { |
| 118 | return false; |
| 119 | } |
| 120 | if (s == end || *end != '\0') { |
| 121 | errno = EINVAL; |
| 122 | return false; |
| 123 | } |
| 124 | if (result < min || max < result) { |
| 125 | errno = ERANGE; |
| 126 | return false; |
| 127 | } |
| 128 | if (out != nullptr) { |
| 129 | *out = static_cast<T>(result); |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | // TODO: string_view |
| 135 | template <typename T> |