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

Function EachKey

parser.go:568–779  ·  view source on GitHub ↗

SYS-REQ-008, SYS-REQ-085, SYS-REQ-111

(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string)

Source from the content-addressed store, hash-verified

566
567// SYS-REQ-008, SYS-REQ-085, SYS-REQ-111
568func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string) int {
569 var x struct{}
570 var level, pathsMatched, i int
571 ln := len(data)
572
573 pathFlags := make([]bool, stackArraySize)[:]
574 if len(paths) > cap(pathFlags) {
575 pathFlags = make([]bool, len(paths))[:]
576 }
577 pathFlags = pathFlags[0:len(paths)]
578
579 var maxPath int
580 for _, p := range paths {
581 if len(p) > maxPath {
582 maxPath = len(p)
583 }
584 }
585
586 pathsBuf := make([]string, stackArraySize)[:]
587 if maxPath > cap(pathsBuf) {
588 pathsBuf = make([]string, maxPath)[:]
589 }
590 pathsBuf = pathsBuf[0:maxPath]
591
592 for i < ln {
593 switch data[i] {
594 case '"':
595 i++
596 keyBegin := i
597
598 strEnd, keyEscaped := stringEnd(data[i:])
599 if strEnd == -1 {
600 return -1
601 }
602 i += strEnd
603
604 keyEnd := i - 1
605
606 valueOffset := nextToken(data[i:])
607 if valueOffset == -1 {
608 return -1
609 }
610
611 i += valueOffset
612
613 // if string is a key, and key level match
614 if data[i] == ':' {
615 match := -1
616 key := data[keyBegin:keyEnd]
617
618 // for unescape: if there are no escape sequences, this is cheap; if there are, it is a
619 // bit more expensive, but causes no allocations unless len(key) > unescapeStackBufSize
620 var keyUnesc []byte
621 if !keyEscaped {
622 keyUnesc = key
623 } else {
624 var stackbuf [unescapeStackBufSize]byte
625 if ku, err := Unescape(key, stackbuf[:]); err != nil {

Calls 10

stringEndFunction · 0.85
nextTokenFunction · 0.85
UnescapeFunction · 0.85
sameTreeFunction · 0.85
GetFunction · 0.85
ArrayEachFunction · 0.85
searchKeysFunction · 0.85
blockEndFunction · 0.85
bytesToStringFunction · 0.70
equalStrFunction · 0.70