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

Function DeleteFound

parser.go:1134–1268  ·  view source on GitHub ↗

DeleteFound removes the value addressed by keys and reports whether the value was found and removed. When found is false, result is data unchanged. SYS-REQ-010

(data []byte, keys ...string)

Source from the content-addressed store, hash-verified

1132// value was found and removed. When found is false, result is data unchanged.
1133// SYS-REQ-010
1134func DeleteFound(data []byte, keys ...string) (result []byte, found bool) {
1135 lk := len(keys)
1136 if lk == 0 {
1137 // Deleting the root produces an empty document whose backing array
1138 // must not alias the caller's input.
1139 return make([]byte, 0), true
1140 }
1141
1142 array := false
1143 if len(keys[lk-1]) > 0 && string(keys[lk-1][0]) == "[" {
1144 array = true
1145 }
1146
1147 var startOffset, keyOffset int
1148 endOffset := len(data)
1149 var err error
1150 if !array {
1151 if len(keys) > 1 {
1152 _, _, startOffset, endOffset, err = internalGet(data, keys[:lk-1]...)
1153 if err != nil {
1154 // problem parsing the data
1155 return data, false
1156 }
1157 }
1158
1159 keyOffset, err = findKeyStart(data[startOffset:endOffset], keys[lk-1])
1160 if err == KeyPathNotFoundError {
1161 // problem parsing the data
1162 return data, false
1163 }
1164 keyOffset += startOffset
1165 var subEndOffset int
1166 _, _, _, subEndOffset, err = internalGet(data[startOffset:endOffset], keys[lk-1])
1167 if err != nil {
1168 return data, false
1169 }
1170 endOffset = startOffset + subEndOffset
1171 tokEnd := tokenEnd(data[endOffset:])
1172 tokStart := findTokenStart(data[:keyOffset], ","[0])
1173
1174 if endOffset+tokEnd >= len(data) {
1175 // tokenEnd sentinel: no delimiter found, input is truncated
1176 return data, false
1177 }
1178
1179 // guard: tokenEnd sentinel may return -1 on truncated input; bounds-check before deref.
1180 idx := endOffset + tokEnd
1181 // Scan forward from idx through any JSON whitespace to find the next
1182 // real token. The original check only matched a single ' ' byte
1183 // before the comma, so inputs like '{"a":1,\n"b":2}' or '[0,0 ,0]'
1184 // (multiple whitespace bytes) bypassed the cleanup and left a
1185 // dangling comma sequence. Found by FuzzPathMutation.
1186 nextTokIdx := idx
1187 for nextTokIdx < len(data) && isJSONWhitespace(data[nextTokIdx]) {
1188 nextTokIdx++
1189 }
1190 if len(data) > idx && data[idx] == ',' {
1191 endOffset += tokEnd + 1

Callers 2

DeleteFunction · 0.85
TestDeleteFoundFunction · 0.85

Calls 7

internalGetFunction · 0.85
findKeyStartFunction · 0.85
tokenEndFunction · 0.85
findTokenStartFunction · 0.85
isJSONWhitespaceFunction · 0.85
lastTokenFunction · 0.85
nextTokenFunction · 0.85

Tested by 1

TestDeleteFoundFunction · 0.68