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

Function findKeyStartConfig

parser.go:100–172  ·  view source on GitHub ↗

SYS-REQ-115

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

Source from the content-addressed store, hash-verified

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