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

Function scanContainerLen

parser.go:2142–2350  ·  view source on GitHub ↗

SYS-REQ-112

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

Source from the content-addressed store, hash-verified

2140
2141// SYS-REQ-112
2142func scanContainerLen(data []byte, offset int, open byte) (int, error) {
2143 const (
2144 scanArrayValue = iota
2145 scanArrayPrimitive
2146 scanArrayDelimiter
2147 scanObjectKey
2148 scanObjectColon
2149 scanObjectValue
2150 scanObjectPrimitive
2151 scanObjectDelimiter
2152 )
2153
2154 close := byte(']')
2155 malformedErr := MalformedArrayError
2156 state := scanArrayValue
2157 if open == '{' {
2158 close = '}'
2159 malformedErr = MalformedObjectError
2160 state = scanObjectKey
2161 }
2162
2163 var fixedStack [32]byte
2164 stack := fixedStack[:1]
2165 stack[0] = close
2166
2167 count := 0
2168 primitiveStart := -1
2169
2170 for i := offset + 1; i < len(data); i++ {
2171 c := data[i]
2172
2173 // Only delimiters at the addressed container's top level affect its
2174 // length. Strings and nested containers are skipped structurally.
2175 if len(stack) > 1 {
2176 switch c {
2177 case '"':
2178 stringLength, _ := stringEnd(data[i+1:])
2179 if stringLength == -1 {
2180 return 0, malformedErr
2181 }
2182 i += stringLength
2183 case '[':
2184 stack = append(stack, ']')
2185 case '{':
2186 stack = append(stack, '}')
2187 case ']', '}':
2188 if c != stack[len(stack)-1] {
2189 return 0, malformedErr
2190 }
2191 stack = stack[:len(stack)-1]
2192 }
2193 continue
2194 }
2195
2196 switch state {
2197 case scanArrayValue:
2198 if isContainerWhitespace(c) {
2199 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