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

Function Delete

parser.go:874–1006  ·  view source on GitHub ↗

* Del - Receives existing data structure, path to delete. Returns: `data` - return modified data */ SYS-REQ-010, SYS-REQ-033, SYS-REQ-034, SYS-REQ-035, SYS-REQ-048, SYS-REQ-049, SYS-REQ-050, SYS-REQ-056

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

Source from the content-addressed store, hash-verified

872*/
873// SYS-REQ-010, SYS-REQ-033, SYS-REQ-034, SYS-REQ-035, SYS-REQ-048, SYS-REQ-049, SYS-REQ-050, SYS-REQ-056
874func Delete(data []byte, keys ...string) []byte {
875 lk := len(keys)
876 if lk == 0 {
877 return data[:0]
878 }
879
880 array := false
881 if len(keys[lk-1]) > 0 && string(keys[lk-1][0]) == "[" {
882 array = true
883 }
884
885 var startOffset, keyOffset int
886 endOffset := len(data)
887 var err error
888 if !array {
889 if len(keys) > 1 {
890 _, _, startOffset, endOffset, err = internalGet(data, keys[:lk-1]...)
891 if err != nil {
892 // problem parsing the data
893 return data
894 }
895 }
896
897 keyOffset, err = findKeyStart(data[startOffset:endOffset], keys[lk-1])
898 if err == KeyPathNotFoundError {
899 // problem parsing the data
900 return data
901 }
902 keyOffset += startOffset
903 var subEndOffset int
904 _, _, _, subEndOffset, err = internalGet(data[startOffset:endOffset], keys[lk-1])
905 if err != nil {
906 return data
907 }
908 endOffset = startOffset + subEndOffset
909 tokEnd := tokenEnd(data[endOffset:])
910 tokStart := findTokenStart(data[:keyOffset], ","[0])
911
912 if endOffset+tokEnd >= len(data) {
913 // tokenEnd sentinel: no delimiter found, input is truncated
914 return data
915 }
916
917 // guard: tokenEnd sentinel may return -1 on truncated input; bounds-check before deref.
918 idx := endOffset + tokEnd
919 // Scan forward from idx through any JSON whitespace to find the next
920 // real token. The original check only matched a single ' ' byte
921 // before the comma, so inputs like '{"a":1,\n"b":2}' or '[0,0 ,0]'
922 // (multiple whitespace bytes) bypassed the cleanup and left a
923 // dangling comma sequence. Found by FuzzPathMutation.
924 nextTokIdx := idx
925 for nextTokIdx < len(data) && isJSONWhitespace(data[nextTokIdx]) {
926 nextTokIdx++
927 }
928 if len(data) > idx && data[idx] == ',' {
929 endOffset += tokEnd + 1
930 } else if len(data) > nextTokIdx && data[nextTokIdx] == ',' {
931 // Symmetric with the array-branch case below: when the bytes

Callers 15

BenchmarkDeleteSmallFunction · 0.92
BenchmarkDeleteNestedFunction · 0.92
BenchmarkDeleteLargeFunction · 0.92
TestMutationNilSafetyFunction · 0.85
TestMutationUnicodeKeysFunction · 0.85
FuzzPathMutationFunction · 0.85
applySequenceStepFunction · 0.85
FuzzDeleteFunction · 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 15

BenchmarkDeleteSmallFunction · 0.74
BenchmarkDeleteNestedFunction · 0.74
BenchmarkDeleteLargeFunction · 0.74
TestMutationNilSafetyFunction · 0.68
TestMutationUnicodeKeysFunction · 0.68
FuzzPathMutationFunction · 0.68
applySequenceStepFunction · 0.68
TestDeleteFunction · 0.68