| 617 | } |
| 618 | |
| 619 | absl::StatusOr<std::string> ParseBytesLiteral(absl::string_view str) { |
| 620 | std::string out; |
| 621 | bool is_bytes_literal = MayBeBytesLiteral(str); |
| 622 | bool is_raw_bytes_literal = MayBeRawBytesLiteral(str); |
| 623 | if (!is_bytes_literal && !is_raw_bytes_literal) { |
| 624 | return absl::InvalidArgumentError("Invalid bytes literal"); |
| 625 | } |
| 626 | |
| 627 | absl::string_view copy_str = str; |
| 628 | if (is_raw_bytes_literal) { |
| 629 | // Strip off the prefix {"rb", "br"} from the raw bytes content before |
| 630 | copy_str = absl::ClippedSubstr(copy_str, 2); |
| 631 | } else { |
| 632 | // Strip off the prefix 'b' from the bytes content before parsing. |
| 633 | copy_str = absl::ClippedSubstr(copy_str, 1); |
| 634 | } |
| 635 | |
| 636 | bool is_triple_quoted = MayBeTripleQuotedString(copy_str); |
| 637 | // Starts after the opening quotes {""", '''} or {", '}. |
| 638 | int quotes_length = is_triple_quoted ? 3 : 1; |
| 639 | absl::string_view quotes = copy_str.substr(0, quotes_length); |
| 640 | // Includes the closing quotes. |
| 641 | copy_str = absl::ClippedSubstr(copy_str, quotes_length); |
| 642 | std::string error; |
| 643 | if (!UnescapeInternal(copy_str, quotes, is_raw_bytes_literal, true, &out, |
| 644 | &error)) { |
| 645 | return absl::InvalidArgumentError( |
| 646 | absl::StrCat("Invalid bytes literal: ", error)); |
| 647 | } |
| 648 | return out; |
| 649 | } |
| 650 | |
| 651 | std::string FormatStringLiteral(absl::string_view str) { |
| 652 | absl::string_view quote = |