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

Function unescapeConfig

escape.go:289–336  ·  view source on GitHub ↗

SYS-REQ-115

(config Config, in, out []byte)

Source from the content-addressed store, hash-verified

287
288// SYS-REQ-115
289func unescapeConfig(config Config, in, out []byte) ([]byte, error) {
290 firstBackslash := bytes.IndexByte(in, '\\')
291 if firstBackslash == -1 {
292 return in, nil
293 }
294
295 // Get a buffer of sufficient size (allocate if needed)
296 if cap(out) < len(in) {
297 out = make([]byte, len(in))
298 } else {
299 out = out[0:len(in)]
300 }
301
302 // Copy the first sequence of unescaped bytes to the output and obtain a buffer pointer (subslice)
303 copy(out, in[:firstBackslash])
304 in = in[firstBackslash:]
305 buf := out[firstBackslash:]
306
307 // The loop always exits via break: either on error (MalformedStringEscapeError)
308 // or after copying the final non-escaped tail. The former `for len(in) > 0`
309 // guard was structurally always true on re-entry since the else branch always
310 // leaves at least the backslash character in `in`.
311 for {
312 // Unescape the next escaped character
313 inLen, bufLen := unescapeToUTF8Config(config, in, buf)
314 if inLen == -1 {
315 return nil, MalformedStringEscapeError
316 }
317
318 in = in[inLen:]
319 buf = buf[bufLen:]
320
321 // Copy everything up until the next backslash
322 nextBackslash := bytes.IndexByte(in, '\\')
323 if nextBackslash == -1 {
324 copy(buf, in)
325 buf = buf[len(in):]
326 break
327 } else {
328 copy(buf, in[:nextBackslash])
329 buf = buf[nextBackslash:]
330 in = in[nextBackslash:]
331 }
332 }
333
334 // Trim the out buffer to the amount that was actually emitted
335 return out[:len(out)-len(buf)], nil
336}

Callers 7

GetStringMethod · 0.85
readObjectKeyMethod · 0.85
findKeyStartConfigFunction · 0.85
searchKeysConfigFunction · 0.85
objectEachConfigFunction · 0.85
UnescapeFunction · 0.85
GetStringMethod · 0.85

Calls 1

unescapeToUTF8ConfigFunction · 0.85

Tested by

no test coverage detected