SYS-REQ-001, SYS-REQ-020, SYS-REQ-024
(data []byte, key string)
| 53 | |
| 54 | // SYS-REQ-001, SYS-REQ-020, SYS-REQ-024 |
| 55 | func findKeyStart(data []byte, key string) (int, error) { |
| 56 | i := nextToken(data) |
| 57 | if i == -1 { |
| 58 | return i, KeyPathNotFoundError |
| 59 | } |
| 60 | ln := len(data) |
| 61 | // Note: nextToken returning non-negative (checked above) guarantees ln > 0, |
| 62 | // so the former ln > 0 guard was tautological and has been removed. |
| 63 | if data[i] == '{' || data[i] == '[' { |
| 64 | i += 1 |
| 65 | } |
| 66 | var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings |
| 67 | |
| 68 | if ku, err := Unescape(StringToBytes(key), stackbuf[:]); err == nil { |
| 69 | key = bytesToString(&ku) |
| 70 | } |
| 71 | |
| 72 | for i < ln { |
| 73 | switch data[i] { |
| 74 | case '"': |
| 75 | i++ |
| 76 | keyBegin := i |
| 77 | |
| 78 | strEnd, keyEscaped := stringEnd(data[i:]) |
| 79 | if strEnd == -1 { |
| 80 | break |
| 81 | } |
| 82 | i += strEnd |
| 83 | keyEnd := i - 1 |
| 84 | |
| 85 | valueOffset := nextToken(data[i:]) |
| 86 | if valueOffset == -1 { |
| 87 | break |
| 88 | } |
| 89 | |
| 90 | i += valueOffset |
| 91 | |
| 92 | // if string is a key, and key level match |
| 93 | k := data[keyBegin:keyEnd] |
| 94 | // for unescape: if there are no escape sequences, this is cheap; if there are, it is a |
| 95 | // bit more expensive, but causes no allocations unless len(key) > unescapeStackBufSize |
| 96 | if keyEscaped { |
| 97 | if ku, err := Unescape(k, stackbuf[:]); err != nil { |
| 98 | break |
| 99 | } else { |
| 100 | k = ku |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if data[i] == ':' && len(key) == len(k) && bytesToString(&k) == key { |
| 105 | return keyBegin - 1, nil |
| 106 | } |
| 107 | |
| 108 | case '[': |
| 109 | end := blockEnd(data[i:], data[i], ']') |
| 110 | if end != -1 { |
| 111 | i = i + end |
| 112 | } |
searching dependent graphs…