SYS-REQ-001, SYS-REQ-020, SYS-REQ-024
(data []byte, key string)
| 92 | |
| 93 | // SYS-REQ-001, SYS-REQ-020, SYS-REQ-024 |
| 94 | func findKeyStart(data []byte, key string) (int, error) { |
| 95 | i := nextToken(data) |
| 96 | if i == -1 { |
| 97 | return i, KeyPathNotFoundError |
| 98 | } |
| 99 | ln := len(data) |
| 100 | // Note: nextToken returning non-negative (checked above) guarantees ln > 0, |
| 101 | // so the former ln > 0 guard was tautological and has been removed. |
| 102 | if data[i] == '{' || data[i] == '[' { |
| 103 | i += 1 |
| 104 | } |
| 105 | var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings |
| 106 | |
| 107 | if ku, err := Unescape(StringToBytes(key), stackbuf[:]); err == nil { |
| 108 | key = bytesToString(&ku) |
| 109 | } |
| 110 | |
| 111 | for i < ln { |
| 112 | switch data[i] { |
| 113 | case '"': |
| 114 | i++ |
| 115 | keyBegin := i |
| 116 | |
| 117 | strEnd, keyEscaped := stringEnd(data[i:]) |
| 118 | if strEnd == -1 { |
| 119 | break |
| 120 | } |
| 121 | i += strEnd |
| 122 | keyEnd := i - 1 |
| 123 | |
| 124 | valueOffset := nextToken(data[i:]) |
| 125 | if valueOffset == -1 { |
| 126 | break |
| 127 | } |
| 128 | |
| 129 | i += valueOffset |
| 130 | |
| 131 | // if string is a key, and key level match |
| 132 | k := data[keyBegin:keyEnd] |
| 133 | // for unescape: if there are no escape sequences, this is cheap; if there are, it is a |
| 134 | // bit more expensive, but causes no allocations unless len(key) > unescapeStackBufSize |
| 135 | if keyEscaped { |
| 136 | if ku, err := Unescape(k, stackbuf[:]); err != nil { |
| 137 | break |
| 138 | } else { |
| 139 | k = ku |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if data[i] == ':' && len(key) == len(k) && bytesToString(&k) == key { |
| 144 | return keyBegin - 1, nil |
| 145 | } |
| 146 | |
| 147 | case '[': |
| 148 | end := blockEnd(data[i:], data[i], ']') |
| 149 | if end != -1 { |
| 150 | i = i + end |
| 151 | } |