MCPcopy Create free account
hub / github.com/buger/jsonparser / Escape

Function Escape

escape.go:230–264  ·  view source on GitHub ↗

Escape returns in as a JSON string literal, including the surrounding quotation marks. SYS-REQ-014

(in string)

Source from the content-addressed store, hash-verified

228// quotation marks.
229// SYS-REQ-014
230func Escape(in string) []byte {
231 var stackbuf [unescapeStackBufSize]byte
232 out := stackbuf[:0]
233 out = append(out, '"')
234
235 start := 0
236 for i := 0; i < len(in); i++ {
237 c := in[i]
238 if c >= 0x20 && c != '"' && c != '\\' {
239 continue
240 }
241
242 out = append(out, in[start:i]...)
243 switch c {
244 case '"', '\\':
245 out = append(out, '\\', c)
246 case '\b':
247 out = append(out, '\\', 'b')
248 case '\f':
249 out = append(out, '\\', 'f')
250 case '\n':
251 out = append(out, '\\', 'n')
252 case '\r':
253 out = append(out, '\\', 'r')
254 case '\t':
255 out = append(out, '\\', 't')
256 default:
257 out = append(out, '\\', 'u', '0', '0', lowerHex[c>>4], lowerHex[c&0x0f])
258 }
259 start = i + 1
260 }
261
262 out = append(out, in[start:]...)
263 return append(out, '"')
264}
265
266// SetString replaces the value at keys with val encoded as a JSON string.
267// SYS-REQ-009

Callers 6

TestAppendStringFunction · 0.85
TestEscapeFunction · 0.85
TestEscapeControlCharsFunction · 0.85
TestEscapeUnicodeFunction · 0.85
TestEscapeEmptyStringFunction · 0.85
SetStringFunction · 0.85

Calls

no outgoing calls

Tested by 5

TestAppendStringFunction · 0.68
TestEscapeFunction · 0.68
TestEscapeControlCharsFunction · 0.68
TestEscapeUnicodeFunction · 0.68
TestEscapeEmptyStringFunction · 0.68