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

Function calcAllocateSpace

parser.go:1069–1109  ·  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

1067
1068// SYS-REQ-009, SYS-REQ-111
1069func calcAllocateSpace(keys []string, setValue []byte, comma, object bool) int {
1070 // guard: empty key component — not an array index.
1071 isIndex := len(keys[0]) > 0 && string(keys[0][0]) == "["
1072 lk := 0
1073 if comma {
1074 // ,
1075 lk += 1
1076 }
1077 if isIndex && !comma {
1078 // []
1079 lk += 2
1080 } else {
1081 if object {
1082 // {
1083 lk += 1
1084 }
1085 if !isIndex {
1086 // "keys[0]"
1087 lk += len(keys[0]) + 3
1088 }
1089 }
1090
1091 lk += len(setValue)
1092 for i := 1; i < len(keys); i++ {
1093 // guard: empty key component — treat as object key, not array index.
1094 if len(keys[i]) > 0 && string(keys[i][0]) == "[" {
1095 // []
1096 lk += 2
1097 } else {
1098 // {"keys[i]":setValue}
1099 lk += len(keys[i]) + 5
1100 }
1101 }
1102
1103 if object && !isIndex {
1104 // }
1105 lk += 1
1106 }
1107
1108 return lk
1109}
1110
1111// SYS-REQ-009
1112func WriteToBuffer(buffer []byte, str string) int {

Calls

no outgoing calls