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