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

Function calcAllocateSpace

parser.go:1179–1219  ·  view source on GitHub ↗

SYS-REQ-009, SYS-REQ-111

(keys []string, setValue []byte, comma, object bool)

Source from the content-addressed store, hash-verified

1177
1178// SYS-REQ-009, SYS-REQ-111
1179func calcAllocateSpace(keys []string, setValue []byte, comma, object bool) int {
1180 // guard: empty key component — not an array index.
1181 isIndex := len(keys[0]) > 0 && string(keys[0][0]) == "["
1182 lk := 0
1183 if comma {
1184 // ,
1185 lk += 1
1186 }
1187 if isIndex && !comma {
1188 // []
1189 lk += 2
1190 } else {
1191 if object {
1192 // {
1193 lk += 1
1194 }
1195 if !isIndex {
1196 // "keys[0]"
1197 lk += len(keys[0]) + 3
1198 }
1199 }
1200
1201 lk += len(setValue)
1202 for i := 1; i < len(keys); i++ {
1203 // guard: empty key component — treat as object key, not array index.
1204 if len(keys[i]) > 0 && string(keys[i][0]) == "[" {
1205 // []
1206 lk += 2
1207 } else {
1208 // {"keys[i]":setValue}
1209 lk += len(keys[i]) + 5
1210 }
1211 }
1212
1213 if object && !isIndex {
1214 // }
1215 lk += 1
1216 }
1217
1218 return lk
1219}
1220
1221// SYS-REQ-009
1222func WriteToBuffer(buffer []byte, str string) int {

Calls

no outgoing calls