2017-07-09 22:17:20 +02:00
|
|
|
package utils
|
2017-07-02 01:42:48 +02:00
|
|
|
|
2017-09-01 15:40:27 +02:00
|
|
|
// StringList adds methods to []string
|
|
|
|
type StringList []string
|
|
|
|
|
|
|
|
// Reject returns a copy of StringList without elements that its argument tests true on.
|
|
|
|
func (sl StringList) Reject(p func(string) bool) StringList {
|
|
|
|
result := make([]string, 0, len(sl))
|
|
|
|
for _, s := range sl {
|
|
|
|
if !p(s) {
|
|
|
|
result = append(result, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-07-02 01:42:48 +02:00
|
|
|
// SearchStrings returns a bool indicating whether array contains the string.
|
|
|
|
// Unlike sort.SearchStrings, it does not require a sorted array.
|
|
|
|
// This is useful when the array length is low; else consider sorting it and using
|
|
|
|
// sort.SearchStrings, or creating a map[string]bool.
|
|
|
|
func SearchStrings(array []string, s string) bool {
|
2017-07-02 02:49:15 +02:00
|
|
|
for _, item := range array {
|
|
|
|
if item == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|