MCPcopy
hub / github.com/apache/casbin / EscapeStringLiterals

Function EscapeStringLiterals

util/util.go:283–312  ·  view source on GitHub ↗

EscapeStringLiterals escapes backslashes in string literals within an expression to ensure consistent handling between govaluate (which interprets escape sequences) and CSV parsing (which treats backslashes as literal characters). This function doubles all backslashes within single-quoted and double

(expr string)

Source from the content-addressed store, hash-verified

281// and CSV parsing (which treats backslashes as literal characters).
282// This function doubles all backslashes within single-quoted and double-quoted strings.
283func EscapeStringLiterals(expr string) string {
284 var result strings.Builder
285 inString := false
286 var quote rune
287
288 for i := 0; i < len(expr); i++ {
289 ch := rune(expr[i])
290
291 if inString {
292 result.WriteRune(ch)
293 if ch == '\\' {
294 // Found a backslash inside a string - double it
295 result.WriteRune('\\')
296 } else if ch == quote {
297 // End of string literal
298 inString = false
299 }
300 continue
301 }
302
303 // Not inside a string literal
304 if ch == '\'' || ch == '"' {
305 inString = true
306 quote = ch
307 }
308 result.WriteRune(ch)
309 }
310
311 return result.String()
312}
313
314func RemoveDuplicateElement(s []string) []string {
315 result := make([]string, 0, len(s))

Callers 3

enforceMethod · 0.92
AddDefMethod · 0.92
testEscapeStringLiteralsFunction · 0.85

Calls 1

StringMethod · 0.65

Tested by 1

testEscapeStringLiteralsFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…