Parses a decimal integer, storing it in *np. Sets *s to span the remainder of the string.
| 1324 | // Parses a decimal integer, storing it in *np. |
| 1325 | // Sets *s to span the remainder of the string. |
| 1326 | static bool ParseInteger(StringPiece* s, int* np) { |
| 1327 | if (s->empty() || !isdigit((*s)[0] & 0xFF)) |
| 1328 | return false; |
| 1329 | // Disallow leading zeros. |
| 1330 | if (s->size() >= 2 && (*s)[0] == '0' && isdigit((*s)[1] & 0xFF)) |
| 1331 | return false; |
| 1332 | int n = 0; |
| 1333 | int c; |
| 1334 | while (!s->empty() && isdigit(c = (*s)[0] & 0xFF)) { |
| 1335 | // Avoid overflow. |
| 1336 | if (n >= 100000000) |
| 1337 | return false; |
| 1338 | n = n*10 + c - '0'; |
| 1339 | s->remove_prefix(1); // digit |
| 1340 | } |
| 1341 | *np = n; |
| 1342 | return true; |
| 1343 | } |
| 1344 | |
| 1345 | // Parses a repetition suffix like {1,2} or {2} or {2,}. |
| 1346 | // Sets *s to span the remainder of the string on success. |
no test coverage detected