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

Function EscapeBytes

internal/strings.cc:556–589  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

554}
555
556std::string EscapeBytes(absl::string_view str, bool escape_all_bytes,
557 char escape_quote_char) {
558 std::string escaped_bytes;
559 const char* p = str.data();
560 const char* end = p + str.size();
561 for (; p < end; ++p) {
562 unsigned char c = *p;
563 if (escape_all_bytes || !absl::ascii_isprint(c)) {
564 escaped_bytes += "\\x";
565 escaped_bytes += absl::BytesToHexString(absl::string_view(p, 1));
566 } else {
567 switch (c) {
568 // Note that we only handle printable escape characters here. All
569 // unprintable (\n, \r, \t, etc.) are hex escaped above.
570 case '\\':
571 escaped_bytes += "\\\\";
572 break;
573 case '\'':
574 case '"':
575 case '`':
576 // Escape only quote chars that match escape_quote_char.
577 if (escape_quote_char == 0 || c == escape_quote_char) {
578 escaped_bytes += '\\';
579 }
580 escaped_bytes += c;
581 break;
582 default:
583 escaped_bytes += c;
584 break;
585 }
586 }
587 }
588 return escaped_bytes;
589}
590
591absl::StatusOr<std::string> ParseStringLiteral(absl::string_view str) {
592 std::string out;

Callers 5

TestBytesEscapingFunction · 0.85
UnescapeInternalFunction · 0.85
FormatBytesLiteralFunction · 0.85

Calls 1

sizeMethod · 0.45

Tested by 1

TestBytesEscapingFunction · 0.68