MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / ParseBytesLiteral

Function ParseBytesLiteral

internal/strings.cc:619–649  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

617}
618
619absl::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
651std::string FormatStringLiteral(absl::string_view str) {
652 absl::string_view quote =

Callers 4

visitBytesMethod · 0.85
TestParseBytesFunction · 0.85
TestInvalidBytesFunction · 0.85
ParseConstantValueFunction · 0.85

Calls 4

MayBeBytesLiteralFunction · 0.85
MayBeRawBytesLiteralFunction · 0.85
MayBeTripleQuotedStringFunction · 0.85
UnescapeInternalFunction · 0.85

Tested by 2

TestParseBytesFunction · 0.68
TestInvalidBytesFunction · 0.68