MCPcopy
hub / github.com/valyala/quicktemplate / AppendJSONString

Function AppendJSONString

jsonstring.go:24–45  ·  view source on GitHub ↗

AppendJSONString appends json-encoded string s to dst and returns the result. If addQuotes is true, then the appended json string is wrapped into double quotes.

(dst []byte, s string, addQuotes bool)

Source from the content-addressed store, hash-verified

22//
23// If addQuotes is true, then the appended json string is wrapped into double quotes.
24func AppendJSONString(dst []byte, s string, addQuotes bool) []byte {
25 if !hasSpecialChars(s) {
26 // Fast path - nothing to escape.
27 if !addQuotes {
28 return append(dst, s...)
29 }
30 dst = append(dst, '"')
31 dst = append(dst, s...)
32 dst = append(dst, '"')
33 return dst
34 }
35
36 // Slow path - there are chars to escape.
37 if addQuotes {
38 dst = append(dst, '"')
39 }
40 dst = jsonReplacer.AppendReplace(dst, s)
41 if addQuotes {
42 dst = append(dst, '"')
43 }
44 return dst
45}
46
47var jsonReplacer = newByteReplacer(func() ([]byte, []string) {
48 oldChars := []byte("\n\r\t\b\f\"\\<'")

Callers 4

testAppendJSONStringFunction · 0.85
QMethod · 0.85
JMethod · 0.85

Calls 2

hasSpecialCharsFunction · 0.85
AppendReplaceMethod · 0.80

Tested by 2

testAppendJSONStringFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…