Skip to content

Commit

Permalink
Merge pull request #165 from Hakkin/atomfix
Browse files Browse the repository at this point in the history
Fix RemoveAtoms function when multiple atoms are removed.
  • Loading branch information
Kethsar authored Nov 16, 2023
2 parents 08a218f + 04756f4 commit 0f2cfc5
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os/signal"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -533,12 +534,22 @@ func GetAtoms(data []byte) map[string]Atom {
func RemoveAtoms(data []byte, atomList ...string) []byte {
atoms := GetAtoms(data)

var atomsToRemove []Atom
for _, atomName := range atomList {
atom, ok := atoms[atomName]
if !ok {
continue
}
atomsToRemove = append(atomsToRemove, atom)
}

// Sort atoms by byte offset in descending order,
// this lets us remove them in order without affecting the next atom's offset
sort.Slice(atomsToRemove, func(i, j int) bool {
return atomsToRemove[i].Offset > atomsToRemove[j].Offset
})

for _, atom := range atomsToRemove {
ofs := atom.Offset
rlen := atom.Offset + atom.Length
data = append(data[:ofs], data[rlen:]...)
Expand Down

0 comments on commit 0f2cfc5

Please sign in to comment.