MCPcopy Create free account
hub / github.com/cel-expr/cel-go / quote

Function quote

ext/strings.go:822–850  ·  view source on GitHub ↗

quote implements a string quoting function. The string will be wrapped in double quotes, and all valid CEL escape sequences will be escaped to show up literally if printed. If the input contains any invalid UTF-8, the invalid runes will be replaced with utf8.RuneError.

(s string)

Source from the content-addressed store, hash-verified

820// literally if printed. If the input contains any invalid UTF-8, the invalid runes
821// will be replaced with utf8.RuneError.
822func quote(s string) (string, error) {
823 var quotedStrBuilder strings.Builder
824 for _, c := range sanitize(s) {
825 switch c {
826 case '\a':
827 quotedStrBuilder.WriteString("\\a")
828 case '\b':
829 quotedStrBuilder.WriteString("\\b")
830 case '\f':
831 quotedStrBuilder.WriteString("\\f")
832 case '\n':
833 quotedStrBuilder.WriteString("\\n")
834 case '\r':
835 quotedStrBuilder.WriteString("\\r")
836 case '\t':
837 quotedStrBuilder.WriteString("\\t")
838 case '\v':
839 quotedStrBuilder.WriteString("\\v")
840 case '\\':
841 quotedStrBuilder.WriteString("\\\\")
842 case '"':
843 quotedStrBuilder.WriteString("\\\"")
844 default:
845 quotedStrBuilder.WriteRune(c)
846 }
847 }
848 escapedStr := quotedStrBuilder.String()
849 return "\"" + escapedStr + "\"", nil
850}
851
852// sanitize replaces all invalid runes in the given string with utf8.RuneError.
853func sanitize(s string) string {

Callers 3

CompileOptionsMethod · 0.85
TestQuoteUnquoteFunction · 0.85
FuzzQuoteFunction · 0.85

Calls 2

sanitizeFunction · 0.85
StringMethod · 0.65

Tested by 2

TestQuoteUnquoteFunction · 0.68
FuzzQuoteFunction · 0.68