SYS-REQ-008, SYS-REQ-085, SYS-REQ-111
(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string)
| 585 | |
| 586 | // SYS-REQ-008, SYS-REQ-085, SYS-REQ-111 |
| 587 | func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string) int { |
| 588 | var x struct{} |
| 589 | var level, pathsMatched, i int |
| 590 | ln := len(data) |
| 591 | |
| 592 | pathFlags := make([]bool, stackArraySize)[:] |
| 593 | if len(paths) > cap(pathFlags) { |
| 594 | pathFlags = make([]bool, len(paths))[:] |
| 595 | } |
| 596 | pathFlags = pathFlags[0:len(paths)] |
| 597 | |
| 598 | var maxPath int |
| 599 | for _, p := range paths { |
| 600 | if len(p) > maxPath { |
| 601 | maxPath = len(p) |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | pathsBuf := make([]string, stackArraySize)[:] |
| 606 | if maxPath > cap(pathsBuf) { |
| 607 | pathsBuf = make([]string, maxPath)[:] |
| 608 | } |
| 609 | pathsBuf = pathsBuf[0:maxPath] |
| 610 | |
| 611 | for i < ln { |
| 612 | switch data[i] { |
| 613 | case '"': |
| 614 | i++ |
| 615 | keyBegin := i |
| 616 | |
| 617 | strEnd, keyEscaped := stringEnd(data[i:]) |
| 618 | if strEnd == -1 { |
| 619 | return -1 |
| 620 | } |
| 621 | i += strEnd |
| 622 | |
| 623 | keyEnd := i - 1 |
| 624 | |
| 625 | valueOffset := nextToken(data[i:]) |
| 626 | if valueOffset == -1 { |
| 627 | return -1 |
| 628 | } |
| 629 | |
| 630 | i += valueOffset |
| 631 | |
| 632 | // if string is a key, and key level match |
| 633 | if data[i] == ':' { |
| 634 | match := -1 |
| 635 | key := data[keyBegin:keyEnd] |
| 636 | |
| 637 | // for unescape: if there are no escape sequences, this is cheap; if there are, it is a |
| 638 | // bit more expensive, but causes no allocations unless len(key) > unescapeStackBufSize |
| 639 | var keyUnesc []byte |
| 640 | if !keyEscaped { |
| 641 | keyUnesc = key |
| 642 | } else { |
| 643 | var stackbuf [unescapeStackBufSize]byte |
| 644 | if ku, err := Unescape(key, stackbuf[:]); err != nil { |