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

Function createInsertComponent

parser.go:1074–1122  ·  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

1072
1073// SYS-REQ-009, SYS-REQ-110, SYS-REQ-111
1074func createInsertComponent(keys []string, setValue []byte, comma, object bool) []byte {
1075 // guard: empty key component — not an array index.
1076 isIndex := len(keys[0]) > 0 && string(keys[0][0]) == "["
1077 offset := 0
1078 lk := calcAllocateSpace(keys, setValue, comma, object)
1079 buffer := make([]byte, lk, lk)
1080 if comma {
1081 offset += WriteToBuffer(buffer[offset:], ",")
1082 }
1083 if isIndex && !comma {
1084 offset += WriteToBuffer(buffer[offset:], "[")
1085 } else {
1086 if object {
1087 offset += WriteToBuffer(buffer[offset:], "{")
1088 }
1089 if !isIndex {
1090 offset += WriteToBuffer(buffer[offset:], "\"")
1091 offset += WriteToBuffer(buffer[offset:], keys[0])
1092 offset += WriteToBuffer(buffer[offset:], "\":")
1093 }
1094 }
1095
1096 for i := 1; i < len(keys); i++ {
1097 // guard: empty key component — treat as object key, not array index.
1098 if len(keys[i]) > 0 && string(keys[i][0]) == "[" {
1099 offset += WriteToBuffer(buffer[offset:], "[")
1100 } else {
1101 offset += WriteToBuffer(buffer[offset:], "{\"")
1102 offset += WriteToBuffer(buffer[offset:], keys[i])
1103 offset += WriteToBuffer(buffer[offset:], "\":")
1104 }
1105 }
1106 offset += copy(buffer[offset:], setValue)
1107 for i := len(keys) - 1; i > 0; i-- {
1108 // guard: empty key component — treat as object key, not array index.
1109 if len(keys[i]) > 0 && string(keys[i][0]) == "[" {
1110 offset += WriteToBuffer(buffer[offset:], "]")
1111 } else {
1112 offset += WriteToBuffer(buffer[offset:], "}")
1113 }
1114 }
1115 if isIndex && !comma {
1116 offset += WriteToBuffer(buffer[offset:], "]")
1117 }
1118 if object && !isIndex {
1119 offset += WriteToBuffer(buffer[offset:], "}")
1120 }
1121 return buffer
1122}
1123
1124// SYS-REQ-009, SYS-REQ-111
1125func calcAllocateSpace(keys []string, setValue []byte, comma, object bool) int {

Calls 2

calcAllocateSpaceFunction · 0.85
WriteToBufferFunction · 0.85