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

Function scanContainerLen

parser.go:2255–2463  ·  view source on GitHub ↗

SYS-REQ-112

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

Source from the content-addressed store, hash-verified

2253
2254// SYS-REQ-112
2255func scanContainerLen(data []byte, offset int, open byte) (int, error) {
2256 const (
2257 scanArrayValue = iota
2258 scanArrayPrimitive
2259 scanArrayDelimiter
2260 scanObjectKey
2261 scanObjectColon
2262 scanObjectValue
2263 scanObjectPrimitive
2264 scanObjectDelimiter
2265 )
2266
2267 close := byte(']')
2268 malformedErr := MalformedArrayError
2269 state := scanArrayValue
2270 if open == '{' {
2271 close = '}'
2272 malformedErr = MalformedObjectError
2273 state = scanObjectKey
2274 }
2275
2276 var fixedStack [32]byte
2277 stack := fixedStack[:1]
2278 stack[0] = close
2279
2280 count := 0
2281 primitiveStart := -1
2282
2283 for i := offset + 1; i < len(data); i++ {
2284 c := data[i]
2285
2286 // Only delimiters at the addressed container's top level affect its
2287 // length. Strings and nested containers are skipped structurally.
2288 if len(stack) > 1 {
2289 switch c {
2290 case '"':
2291 stringLength, _ := stringEnd(data[i+1:])
2292 if stringLength == -1 {
2293 return 0, malformedErr
2294 }
2295 i += stringLength
2296 case '[':
2297 stack = append(stack, ']')
2298 case '{':
2299 stack = append(stack, '}')
2300 case ']', '}':
2301 if c != stack[len(stack)-1] {
2302 return 0, malformedErr
2303 }
2304 stack = stack[:len(stack)-1]
2305 }
2306 continue
2307 }
2308
2309 switch state {
2310 case scanArrayValue:
2311 if isContainerWhitespace(c) {
2312 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