SYS-REQ-008, SYS-REQ-085, SYS-REQ-111
(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string)
| 528 | |
| 529 | // SYS-REQ-008, SYS-REQ-085, SYS-REQ-111 |
| 530 | func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string) int { |
| 531 | var x struct{} |
| 532 | var level, pathsMatched, i int |
| 533 | ln := len(data) |
| 534 | |
| 535 | pathFlags := make([]bool, stackArraySize)[:] |
| 536 | if len(paths) > cap(pathFlags) { |
| 537 | pathFlags = make([]bool, len(paths))[:] |
| 538 | } |
| 539 | pathFlags = pathFlags[0:len(paths)] |
| 540 | |
| 541 | var maxPath int |
| 542 | for _, p := range paths { |
| 543 | if len(p) > maxPath { |
| 544 | maxPath = len(p) |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | pathsBuf := make([]string, stackArraySize)[:] |
| 549 | if maxPath > cap(pathsBuf) { |
| 550 | pathsBuf = make([]string, maxPath)[:] |
| 551 | } |
| 552 | pathsBuf = pathsBuf[0:maxPath] |
| 553 | |
| 554 | for i < ln { |
| 555 | switch data[i] { |
| 556 | case '"': |
| 557 | i++ |
| 558 | keyBegin := i |
| 559 | |
| 560 | strEnd, keyEscaped := stringEnd(data[i:]) |
| 561 | if strEnd == -1 { |
| 562 | return -1 |
| 563 | } |
| 564 | i += strEnd |
| 565 | |
| 566 | keyEnd := i - 1 |
| 567 | |
| 568 | valueOffset := nextToken(data[i:]) |
| 569 | if valueOffset == -1 { |
| 570 | return -1 |
| 571 | } |
| 572 | |
| 573 | i += valueOffset |
| 574 | |
| 575 | // if string is a key, and key level match |
| 576 | if data[i] == ':' { |
| 577 | match := -1 |
| 578 | key := data[keyBegin:keyEnd] |
| 579 | |
| 580 | // for unescape: if there are no escape sequences, this is cheap; if there are, it is a |
| 581 | // bit more expensive, but causes no allocations unless len(key) > unescapeStackBufSize |
| 582 | var keyUnesc []byte |
| 583 | if !keyEscaped { |
| 584 | keyUnesc = key |
| 585 | } else { |
| 586 | var stackbuf [unescapeStackBufSize]byte |
| 587 | if ku, err := Unescape(key, stackbuf[:]); err != nil { |