| 221 | } // namespace |
| 222 | |
| 223 | bool safe_strto64(StringPiece str, int64* value) { |
| 224 | SkipSpaces(&str); |
| 225 | |
| 226 | int64 vlimit = kint64max; |
| 227 | int sign = 1; |
| 228 | if (absl::ConsumePrefix(&str, "-")) { |
| 229 | sign = -1; |
| 230 | // Different limit for positive and negative integers. |
| 231 | vlimit = kint64min; |
| 232 | } |
| 233 | |
| 234 | if (!isdigit(SafeFirstChar(str))) return false; |
| 235 | |
| 236 | int64 result = 0; |
| 237 | if (sign == 1) { |
| 238 | do { |
| 239 | int digit = SafeFirstChar(str) - '0'; |
| 240 | if ((vlimit - digit) / 10 < result) { |
| 241 | return false; |
| 242 | } |
| 243 | result = result * 10 + digit; |
| 244 | str.remove_prefix(1); |
| 245 | } while (isdigit(SafeFirstChar(str))); |
| 246 | } else { |
| 247 | do { |
| 248 | int digit = SafeFirstChar(str) - '0'; |
| 249 | if ((vlimit + digit) / 10 > result) { |
| 250 | return false; |
| 251 | } |
| 252 | result = result * 10 - digit; |
| 253 | str.remove_prefix(1); |
| 254 | } while (isdigit(SafeFirstChar(str))); |
| 255 | } |
| 256 | |
| 257 | SkipSpaces(&str); |
| 258 | if (!str.empty()) return false; |
| 259 | |
| 260 | *value = result; |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | bool safe_strtou64(StringPiece str, uint64* value) { |
| 265 | SkipSpaces(&str); |