Skip to content

Commit

Permalink
Replace uniq with slices.Compact (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbandy authored Jan 26, 2025
1 parent 7162778 commit 5dc9499
Showing 1 changed file with 3 additions and 20 deletions.
23 changes: 3 additions & 20 deletions internal/tracetest/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"sort"
"slices"
"strconv"
"strings"
)
Expand Down Expand Up @@ -81,8 +81,8 @@ func (r fileLineReplacer) Replacements() []string {
// Sort the lines in the file, and remove duplicates.
// The result will be a slice of unique line numbers.
// The index of each line in this slice + 1 will be its new line number.
sort.Ints(fileLines)
fileLines = uniq(fileLines)
slices.Sort(fileLines)
fileLines = slices.Compact(fileLines)

for idx, origLine := range fileLines {
replaceLine := idx + 1
Expand All @@ -93,20 +93,3 @@ func (r fileLineReplacer) Replacements() []string {
}
return allReplacements
}

// uniq removes contiguous duplicates from v.
// The slice storage is re-used so the original slice
// should not be used after calling this function.
func uniq[T comparable](items []T) []T {
if len(items) == 0 {
return items
}

newItems := items[:1]
for _, item := range items[1:] {
if item != newItems[len(newItems)-1] {
newItems = append(newItems, item)
}
}
return newItems
}

0 comments on commit 5dc9499

Please sign in to comment.