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

Function searchKeys

parser.go:360–509  ·  view source on GitHub ↗

SYS-REQ-001, SYS-REQ-020, SYS-REQ-021, SYS-REQ-022, SYS-REQ-023, SYS-REQ-047, SYS-REQ-111

(data []byte, keys ...string)

Source from the content-addressed store, hash-verified

358
359// SYS-REQ-001, SYS-REQ-020, SYS-REQ-021, SYS-REQ-022, SYS-REQ-023, SYS-REQ-047, SYS-REQ-111
360func searchKeys(data []byte, keys ...string) int {
361 keyLevel := 0
362 level := 0
363 i := 0
364 ln := len(data)
365 lk := len(keys)
366 lastMatched := true
367
368 if lk == 0 {
369 return 0
370 }
371
372 var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
373
374 for i < ln {
375 switch data[i] {
376 case '"':
377 i++
378 keyBegin := i
379
380 strEnd, keyEscaped := stringEnd(data[i:])
381 if strEnd == -1 {
382 return -1
383 }
384 i += strEnd
385 keyEnd := i - 1
386
387 valueOffset := nextToken(data[i:])
388 if valueOffset == -1 {
389 return -1
390 }
391
392 i += valueOffset
393
394 // if string is a key
395 if data[i] == ':' {
396 if level < 1 {
397 return -1
398 }
399
400 key := data[keyBegin:keyEnd]
401
402 // for unescape: if there are no escape sequences, this is cheap; if there are, it is a
403 // bit more expensive, but causes no allocations unless len(key) > unescapeStackBufSize
404 var keyUnesc []byte
405 if !keyEscaped {
406 keyUnesc = key
407 } else if ku, err := Unescape(key, stackbuf[:]); err != nil {
408 return -1
409 } else {
410 keyUnesc = ku
411 }
412
413 if level <= len(keys) {
414 if equalStr(&keyUnesc, keys[level-1]) {
415 lastMatched = true
416
417 // if key level match

Calls 6

stringEndFunction · 0.85
nextTokenFunction · 0.85
UnescapeFunction · 0.85
blockEndFunction · 0.85
ArrayEachFunction · 0.85
equalStrFunction · 0.70