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

Function createInsertComponent

parser.go:1128–1176  ·  view source on GitHub ↗

SYS-REQ-009, SYS-REQ-110, SYS-REQ-111

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

Source from the content-addressed store, hash-verified

1126
1127// SYS-REQ-009, SYS-REQ-110, SYS-REQ-111
1128func createInsertComponent(keys []string, setValue []byte, comma, object bool) []byte {
1129 // guard: empty key component — not an array index.
1130 isIndex := len(keys[0]) > 0 && string(keys[0][0]) == "["
1131 offset := 0
1132 lk := calcAllocateSpace(keys, setValue, comma, object)
1133 buffer := make([]byte, lk, lk)
1134 if comma {
1135 offset += WriteToBuffer(buffer[offset:], ",")
1136 }
1137 if isIndex && !comma {
1138 offset += WriteToBuffer(buffer[offset:], "[")
1139 } else {
1140 if object {
1141 offset += WriteToBuffer(buffer[offset:], "{")
1142 }
1143 if !isIndex {
1144 offset += WriteToBuffer(buffer[offset:], "\"")
1145 offset += WriteToBuffer(buffer[offset:], keys[0])
1146 offset += WriteToBuffer(buffer[offset:], "\":")
1147 }
1148 }
1149
1150 for i := 1; i < len(keys); i++ {
1151 // guard: empty key component — treat as object key, not array index.
1152 if len(keys[i]) > 0 && string(keys[i][0]) == "[" {
1153 offset += WriteToBuffer(buffer[offset:], "[")
1154 } else {
1155 offset += WriteToBuffer(buffer[offset:], "{\"")
1156 offset += WriteToBuffer(buffer[offset:], keys[i])
1157 offset += WriteToBuffer(buffer[offset:], "\":")
1158 }
1159 }
1160 offset += copy(buffer[offset:], setValue)
1161 for i := len(keys) - 1; i > 0; i-- {
1162 // guard: empty key component — treat as object key, not array index.
1163 if len(keys[i]) > 0 && string(keys[i][0]) == "[" {
1164 offset += WriteToBuffer(buffer[offset:], "]")
1165 } else {
1166 offset += WriteToBuffer(buffer[offset:], "}")
1167 }
1168 }
1169 if isIndex && !comma {
1170 offset += WriteToBuffer(buffer[offset:], "]")
1171 }
1172 if object && !isIndex {
1173 offset += WriteToBuffer(buffer[offset:], "}")
1174 }
1175 return buffer
1176}
1177
1178// SYS-REQ-009, SYS-REQ-111
1179func calcAllocateSpace(keys []string, setValue []byte, comma, object bool) int {

Calls 2

calcAllocateSpaceFunction · 0.85
WriteToBufferFunction · 0.85