| 589 | } |
| 590 | |
| 591 | absl::StatusOr<std::string> ParseStringLiteral(absl::string_view str) { |
| 592 | std::string out; |
| 593 | bool is_string_literal = MayBeStringLiteral(str); |
| 594 | bool is_raw_string_literal = MayBeRawStringLiteral(str); |
| 595 | if (!is_string_literal && !is_raw_string_literal) { |
| 596 | return absl::InvalidArgumentError("Invalid string literal"); |
| 597 | } |
| 598 | |
| 599 | absl::string_view copy_str = str; |
| 600 | if (is_raw_string_literal) { |
| 601 | // Strip off the prefix 'r' from the raw string content before parsing. |
| 602 | copy_str = absl::ClippedSubstr(copy_str, 1); |
| 603 | } |
| 604 | |
| 605 | bool is_triple_quoted = MayBeTripleQuotedString(copy_str); |
| 606 | // Starts after the opening quotes {""", '''} or {", '}. |
| 607 | int quotes_length = is_triple_quoted ? 3 : 1; |
| 608 | absl::string_view quotes = copy_str.substr(0, quotes_length); |
| 609 | copy_str = absl::ClippedSubstr(copy_str, quotes_length); |
| 610 | std::string error; |
| 611 | if (!UnescapeInternal(copy_str, quotes, is_raw_string_literal, false, &out, |
| 612 | &error)) { |
| 613 | return absl::InvalidArgumentError( |
| 614 | absl::StrCat("Invalid string literal: ", error)); |
| 615 | } |
| 616 | return out; |
| 617 | } |
| 618 | |
| 619 | absl::StatusOr<std::string> ParseBytesLiteral(absl::string_view str) { |
| 620 | std::string out; |