Add FilterListPred.
Test: cd build/soong; go test ./android Change-Id: Ibbdd3cbdb822bd2e843096a22cdd08c827b70526
This commit is contained in:
parent
32312eb75a
commit
1461c4dbca
|
@ -193,6 +193,17 @@ func FilterList(list []string, filter []string) (remainder []string, filtered []
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FilterListPred returns the elements of the given list for which the predicate
|
||||||
|
// returns true. Order is kept.
|
||||||
|
func FilterListPred(list []string, pred func(s string) bool) (filtered []string) {
|
||||||
|
for _, l := range list {
|
||||||
|
if pred(l) {
|
||||||
|
filtered = append(filtered, l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveListFromList removes the strings belonging to the filter list from the
|
// RemoveListFromList removes the strings belonging to the filter list from the
|
||||||
// given list and returns the result
|
// given list and returns the result
|
||||||
func RemoveListFromList(list []string, filter_out []string) (result []string) {
|
func RemoveListFromList(list []string, filter_out []string) (result []string) {
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -299,6 +300,14 @@ func TestFilterList(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFilterListPred(t *testing.T) {
|
||||||
|
pred := func(s string) bool { return strings.HasPrefix(s, "a/") }
|
||||||
|
AssertArrayString(t, "filter", FilterListPred([]string{"a/c", "b/a", "a/b"}, pred), []string{"a/c", "a/b"})
|
||||||
|
AssertArrayString(t, "filter", FilterListPred([]string{"b/c", "a/a", "b/b"}, pred), []string{"a/a"})
|
||||||
|
AssertArrayString(t, "filter", FilterListPred([]string{"c/c", "b/a", "c/b"}, pred), []string{})
|
||||||
|
AssertArrayString(t, "filter", FilterListPred([]string{"a/c", "a/a", "a/b"}, pred), []string{"a/c", "a/a", "a/b"})
|
||||||
|
}
|
||||||
|
|
||||||
func TestRemoveListFromList(t *testing.T) {
|
func TestRemoveListFromList(t *testing.T) {
|
||||||
input := []string{"a", "b", "c", "d", "a", "c", "d"}
|
input := []string{"a", "b", "c", "d", "a", "c", "d"}
|
||||||
filter := []string{"a", "c"}
|
filter := []string{"a", "c"}
|
||||||
|
|
Loading…
Reference in New Issue