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

Function createInsertComponent

parser.go:1018–1066  ·  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

1016
1017// SYS-REQ-009, SYS-REQ-110, SYS-REQ-111
1018func createInsertComponent(keys []string, setValue []byte, comma, object bool) []byte {
1019 // guard: empty key component — not an array index.
1020 isIndex := len(keys[0]) > 0 && string(keys[0][0]) == "["
1021 offset := 0
1022 lk := calcAllocateSpace(keys, setValue, comma, object)
1023 buffer := make([]byte, lk, lk)
1024 if comma {
1025 offset += WriteToBuffer(buffer[offset:], ",")
1026 }
1027 if isIndex && !comma {
1028 offset += WriteToBuffer(buffer[offset:], "[")
1029 } else {
1030 if object {
1031 offset += WriteToBuffer(buffer[offset:], "{")
1032 }
1033 if !isIndex {
1034 offset += WriteToBuffer(buffer[offset:], "\"")
1035 offset += WriteToBuffer(buffer[offset:], keys[0])
1036 offset += WriteToBuffer(buffer[offset:], "\":")
1037 }
1038 }
1039
1040 for i := 1; i < len(keys); i++ {
1041 // guard: empty key component — treat as object key, not array index.
1042 if len(keys[i]) > 0 && string(keys[i][0]) == "[" {
1043 offset += WriteToBuffer(buffer[offset:], "[")
1044 } else {
1045 offset += WriteToBuffer(buffer[offset:], "{\"")
1046 offset += WriteToBuffer(buffer[offset:], keys[i])
1047 offset += WriteToBuffer(buffer[offset:], "\":")
1048 }
1049 }
1050 offset += copy(buffer[offset:], setValue)
1051 for i := len(keys) - 1; i > 0; i-- {
1052 // guard: empty key component — treat as object key, not array index.
1053 if len(keys[i]) > 0 && string(keys[i][0]) == "[" {
1054 offset += WriteToBuffer(buffer[offset:], "]")
1055 } else {
1056 offset += WriteToBuffer(buffer[offset:], "}")
1057 }
1058 }
1059 if isIndex && !comma {
1060 offset += WriteToBuffer(buffer[offset:], "]")
1061 }
1062 if object && !isIndex {
1063 offset += WriteToBuffer(buffer[offset:], "}")
1064 }
1065 return buffer
1066}
1067
1068// SYS-REQ-009, SYS-REQ-111
1069func calcAllocateSpace(keys []string, setValue []byte, comma, object bool) int {

Calls 2

calcAllocateSpaceFunction · 0.85
WriteToBufferFunction · 0.85