Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new function DeleteOnOrig #256

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,13 +708,29 @@ func WriteToBuffer(buffer []byte, str string) int {

/*

Del - Receives existing data structure, path to delete.
Delete - Receives existing data structure, path to delete.

Returns:
`data` - return modified data
`data` - return modified data on new slice

*/
func Delete(data []byte, keys ...string) []byte {
return delete(false, data, keys...)
}

/*

DeleteOnOrig - Receives existing data structure, path to delete.

Returns:
`data` - return modified data on original slice

*/
func DeleteOnOrig(data []byte, keys ...string) []byte {
return delete(true, data, keys...)
}

func delete(flag bool, data []byte, keys ...string) []byte {
lk := len(keys)
if lk == 0 {
return data[:0]
Expand Down Expand Up @@ -783,11 +799,16 @@ func Delete(data []byte, keys ...string) []byte {
newOffset = prevTok + 1
}

// We have to make a copy here if we don't want to mangle the original data, because byte slices are
// accessed by reference and not by value
dataCopy := make([]byte, len(data))
copy(dataCopy, data)
data = append(dataCopy[:newOffset], dataCopy[endOffset:]...)
if !flag {
// We have to make a copy here if we don't want to mangle the original data, because byte slices are
// accessed by reference and not by value
dataCopy := make([]byte, len(data))
copy(dataCopy, data)
data = append(dataCopy[:newOffset], dataCopy[endOffset:]...)
} else {
// delete on original slice
data = append(data[:newOffset], data[endOffset:]...)
}

return data
}
Expand Down