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

Function EachKey

parser.go:641–852  ·  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

639
640// SYS-REQ-008, SYS-REQ-085, SYS-REQ-111
641func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string) int {
642 var x struct{}
643 var level, pathsMatched, i int
644 ln := len(data)
645
646 pathFlags := make([]bool, stackArraySize)[:]
647 if len(paths) > cap(pathFlags) {
648 pathFlags = make([]bool, len(paths))[:]
649 }
650 pathFlags = pathFlags[0:len(paths)]
651
652 var maxPath int
653 for _, p := range paths {
654 if len(p) > maxPath {
655 maxPath = len(p)
656 }
657 }
658
659 pathsBuf := make([]string, stackArraySize)[:]
660 if maxPath > cap(pathsBuf) {
661 pathsBuf = make([]string, maxPath)[:]
662 }
663 pathsBuf = pathsBuf[0:maxPath]
664
665 for i < ln {
666 switch data[i] {
667 case '"':
668 i++
669 keyBegin := i
670
671 strEnd, keyEscaped := stringEnd(data[i:])
672 if strEnd == -1 {
673 return -1
674 }
675 i += strEnd
676
677 keyEnd := i - 1
678
679 valueOffset := nextToken(data[i:])
680 if valueOffset == -1 {
681 return -1
682 }
683
684 i += valueOffset
685
686 // if string is a key, and key level match
687 if data[i] == ':' {
688 match := -1
689 key := data[keyBegin:keyEnd]
690
691 // for unescape: if there are no escape sequences, this is cheap; if there are, it is a
692 // bit more expensive, but causes no allocations unless len(key) > unescapeStackBufSize
693 var keyUnesc []byte
694 if !keyEscaped {
695 keyUnesc = key
696 } else {
697 var stackbuf [unescapeStackBufSize]byte
698 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