MCPcopy Create free account
hub / github.com/buger/jsonparser / findKeyStartConfig

Function findKeyStartConfig

parser.go:101–173  ·  view source on GitHub ↗

SYS-REQ-115

(config Config, data []byte, key string)

Source from the content-addressed store, hash-verified

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

Callers 2

findKeyStartFunction · 0.85
deleteFoundConfigFunction · 0.85

Calls 6

nextTokenConfigFunction · 0.85
unescapeConfigFunction · 0.85
stringEndConfigFunction · 0.85
blockEndConfigFunction · 0.85
StringToBytesFunction · 0.70
bytesToStringFunction · 0.70

Tested by

no test coverage detected