Parse an escape sequence (e.g., \n, \{). Sets *s to span the remainder of the string. Sets *rp to the named character.
| 1447 | // Sets *s to span the remainder of the string. |
| 1448 | // Sets *rp to the named character. |
| 1449 | static bool ParseEscape(StringPiece* s, Rune* rp, |
| 1450 | RegexpStatus* status, int rune_max) { |
| 1451 | const char* begin = s->data(); |
| 1452 | if (s->empty() || (*s)[0] != '\\') { |
| 1453 | // Should not happen - caller always checks. |
| 1454 | status->set_code(kRegexpInternalError); |
| 1455 | status->set_error_arg(StringPiece()); |
| 1456 | return false; |
| 1457 | } |
| 1458 | if (s->size() == 1) { |
| 1459 | status->set_code(kRegexpTrailingBackslash); |
| 1460 | status->set_error_arg(StringPiece()); |
| 1461 | return false; |
| 1462 | } |
| 1463 | Rune c, c1; |
| 1464 | s->remove_prefix(1); // backslash |
| 1465 | if (StringPieceToRune(&c, s, status) < 0) |
| 1466 | return false; |
| 1467 | int code; |
| 1468 | switch (c) { |
| 1469 | default: |
| 1470 | if (c < Runeself && !isalpha(c) && !isdigit(c)) { |
| 1471 | // Escaped non-word characters are always themselves. |
| 1472 | // PCRE is not quite so rigorous: it accepts things like |
| 1473 | // \q, but we don't. We once rejected \_, but too many |
| 1474 | // programs and people insist on using it, so allow \_. |
| 1475 | *rp = c; |
| 1476 | return true; |
| 1477 | } |
| 1478 | goto BadEscape; |
| 1479 | |
| 1480 | // Octal escapes. |
| 1481 | case '1': |
| 1482 | case '2': |
| 1483 | case '3': |
| 1484 | case '4': |
| 1485 | case '5': |
| 1486 | case '6': |
| 1487 | case '7': |
| 1488 | // Single non-zero octal digit is a backreference; not supported. |
| 1489 | if (s->empty() || (*s)[0] < '0' || (*s)[0] > '7') |
| 1490 | goto BadEscape; |
| 1491 | FALLTHROUGH_INTENDED; |
| 1492 | case '0': |
| 1493 | // consume up to three octal digits; already have one. |
| 1494 | code = c - '0'; |
| 1495 | if (!s->empty() && '0' <= (c = (*s)[0]) && c <= '7') { |
| 1496 | code = code * 8 + c - '0'; |
| 1497 | s->remove_prefix(1); // digit |
| 1498 | if (!s->empty()) { |
| 1499 | c = (*s)[0]; |
| 1500 | if ('0' <= c && c <= '7') { |
| 1501 | code = code * 8 + c - '0'; |
| 1502 | s->remove_prefix(1); // digit |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | if (code > rune_max) |
no test coverage detected