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

Function scanContainerLen

parser.go:2161–2369  ·  view source on GitHub ↗

SYS-REQ-112

(data []byte, offset int, open byte)

Source from the content-addressed store, hash-verified

2159
2160// SYS-REQ-112
2161func scanContainerLen(data []byte, offset int, open byte) (int, error) {
2162 const (
2163 scanArrayValue = iota
2164 scanArrayPrimitive
2165 scanArrayDelimiter
2166 scanObjectKey
2167 scanObjectColon
2168 scanObjectValue
2169 scanObjectPrimitive
2170 scanObjectDelimiter
2171 )
2172
2173 close := byte(']')
2174 malformedErr := MalformedArrayError
2175 state := scanArrayValue
2176 if open == '{' {
2177 close = '}'
2178 malformedErr = MalformedObjectError
2179 state = scanObjectKey
2180 }
2181
2182 var fixedStack [32]byte
2183 stack := fixedStack[:1]
2184 stack[0] = close
2185
2186 count := 0
2187 primitiveStart := -1
2188
2189 for i := offset + 1; i < len(data); i++ {
2190 c := data[i]
2191
2192 // Only delimiters at the addressed container's top level affect its
2193 // length. Strings and nested containers are skipped structurally.
2194 if len(stack) > 1 {
2195 switch c {
2196 case '"':
2197 stringLength, _ := stringEnd(data[i+1:])
2198 if stringLength == -1 {
2199 return 0, malformedErr
2200 }
2201 i += stringLength
2202 case '[':
2203 stack = append(stack, ']')
2204 case '{':
2205 stack = append(stack, '}')
2206 case ']', '}':
2207 if c != stack[len(stack)-1] {
2208 return 0, malformedErr
2209 }
2210 stack = stack[:len(stack)-1]
2211 }
2212 continue
2213 }
2214
2215 switch state {
2216 case scanArrayValue:
2217 if isContainerWhitespace(c) {
2218 continue

Callers 2

GetArrayLenFunction · 0.85
GetObjectLenFunction · 0.85

Calls 3

stringEndFunction · 0.85
isContainerWhitespaceFunction · 0.85
validContainerPrimitiveFunction · 0.85

Tested by

no test coverage detected