45 lines
1.5 KiB
Go
45 lines
1.5 KiB
Go
// Package util provide some utility functions
|
|
package util
|
|
|
|
// RemoveTargetsFromSliceSimple define func to remove targets from a slice of strings
|
|
func RemoveTargetsFromSliceSimple(targetsSlice []string, targetsToRemove []string) []string {
|
|
targetsToRemoveSet := make(map[string]struct{}, len(targetsToRemove))
|
|
for _, target := range targetsToRemove {
|
|
targetsToRemoveSet[target] = struct{}{}
|
|
}
|
|
|
|
for i := len(targetsSlice) - 1; i >= 0; i-- {
|
|
if _, found := targetsToRemoveSet[targetsSlice[i]]; found {
|
|
targetsSlice[i] = targetsSlice[len(targetsSlice)-1]
|
|
targetsSlice = targetsSlice[:len(targetsSlice)-1]
|
|
}
|
|
}
|
|
return targetsSlice
|
|
}
|
|
|
|
// SliceToSet define func to convert string slice to set
|
|
func SliceToSet(targetsSlice []string) map[string]struct{} {
|
|
set := make(map[string]struct{}, len(targetsSlice))
|
|
for _, target := range targetsSlice {
|
|
set[target] = struct{}{}
|
|
}
|
|
return set
|
|
}
|
|
|
|
// DeduplicateAndReportDuplicates define func to deduplicate a slice of strings and report duplicates
|
|
func DeduplicateAndReportDuplicates(targetsSlice []string, sourceSlice []string) (deduplicated []string, duplicates []string) {
|
|
targetSet := SliceToSet(targetsSlice)
|
|
deduplicated = make([]string, 0, len(sourceSlice))
|
|
// duplicate items slice
|
|
duplicates = make([]string, 0, len(sourceSlice))
|
|
|
|
for _, source := range sourceSlice {
|
|
if _, found := targetSet[source]; found {
|
|
duplicates = append(duplicates, source)
|
|
continue
|
|
}
|
|
deduplicated = append(deduplicated, source)
|
|
}
|
|
return deduplicated, duplicates
|
|
}
|