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

Function stringEnd

parser.go:281–326  ·  view source on GitHub ↗

SYS-REQ-045 Tries to find the end of string Support if string contains escaped quote symbols.

(data []byte)

Source from the content-addressed store, hash-verified

279// Tries to find the end of string
280// Support if string contains escaped quote symbols.
281func stringEnd(data []byte) (int, bool) {
282 // fast path: SIMD-scan for the first '"' or '\'. If no '\' precedes the
283 // first '"' (the overwhelmingly common case for JSON string values),
284 // the quote is unescaped and we return directly — skipping the per-byte
285 // escape-tracking loop. Bound: bytes.IndexByte finds the first match in
286 // either direction, so firstBackslash > firstQuote (or == -1) is exactly
287 // the condition "no backslash precedes the closing quote", which is
288 // equivalent to the slow loop's `escaped == false` state at the quote.
289 firstQuote := bytes.IndexByte(data, '"')
290 if firstQuote == -1 {
291 // Slow path's tail semantics: return -1 with escaped flag true iff
292 // at least one '\' was encountered before end-of-input.
293 return -1, bytes.IndexByte(data, '\\') != -1
294 }
295 firstBackslash := bytes.IndexByte(data, '\\')
296 if firstBackslash == -1 || firstBackslash > firstQuote {
297 return firstQuote + 1, false
298 }
299 // Slow path: at least one '\' precedes the first '"' — the per-byte
300 // walker is needed to disambiguate escaped vs. unescaped quotes.
301 escaped := false
302 for i, c := range data {
303 if c == '"' {
304 if !escaped {
305 return i + 1, false
306 } else {
307 j := i - 1
308 for {
309 if j < 0 || data[j] != '\\' {
310 return i + 1, true // even number of backslashes
311 }
312 j--
313 if j < 0 || data[j] != '\\' {
314 break // odd number of backslashes
315 }
316 j--
317
318 }
319 }
320 } else if c == '\\' {
321 escaped = true
322 }
323 }
324
325 return -1, escaped
326}
327
328// SYS-REQ-046
329// Find end of the data structure, array or object.

Callers 15

findKeyStartFunction · 0.85
blockEndFunction · 0.85
searchKeysFunction · 0.85
EachKeyFunction · 0.85
EachKeyErrFunction · 0.85
getTypeFunction · 0.85
ObjectEachFunction · 0.85
scanContainerLenFunction · 0.85

Calls

no outgoing calls