2020-05-27 16:47:32 +00:00
|
|
|
package stringslice
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2020-05-27 16:47:32 +00:00
|
|
|
// StrContains => Contains
|
|
|
|
// StringSliceEqual => Equal
|
|
|
|
// StringSliceMergeSorted => MergeSorted
|
|
|
|
|
|
|
|
// Contains checks if a list contains a string
|
|
|
|
func Contains(l []string, s string) bool {
|
|
|
|
for _, v := range l {
|
|
|
|
if v == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal compares two string slices for equality. Both the existence
|
2020-03-09 20:59:02 +00:00
|
|
|
// of the elements and the order of those elements matter for equality. Empty
|
|
|
|
// slices are treated identically to nil slices.
|
2020-05-27 16:47:32 +00:00
|
|
|
func Equal(a, b []string) bool {
|
2020-03-09 20:59:02 +00:00
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2020-05-27 16:31:10 +00:00
|
|
|
|
2020-05-27 16:47:32 +00:00
|
|
|
// MergeSorted takes two string slices that are assumed to be sorted
|
2020-05-27 16:31:10 +00:00
|
|
|
// and does a zipper merge of the two sorted slices, removing any cross-slice
|
|
|
|
// duplicates. If any individual slice contained duplicates those will be
|
|
|
|
// retained.
|
2020-05-27 16:47:32 +00:00
|
|
|
func MergeSorted(a, b []string) []string {
|
2020-05-27 16:31:10 +00:00
|
|
|
if len(a) == 0 && len(b) == 0 {
|
|
|
|
return nil
|
|
|
|
} else if len(a) == 0 {
|
|
|
|
return b
|
|
|
|
} else if len(b) == 0 {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]string, 0, len(a)+len(b))
|
|
|
|
|
|
|
|
i, j := 0, 0
|
|
|
|
for i < len(a) && j < len(b) {
|
|
|
|
switch {
|
|
|
|
case a[i] < b[j]:
|
|
|
|
out = append(out, a[i])
|
|
|
|
i++
|
|
|
|
case a[i] > b[j]:
|
|
|
|
out = append(out, b[j])
|
|
|
|
j++
|
|
|
|
default:
|
|
|
|
out = append(out, a[i])
|
|
|
|
i++
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i < len(a) {
|
|
|
|
out = append(out, a[i:]...)
|
|
|
|
}
|
|
|
|
if j < len(b) {
|
|
|
|
out = append(out, b[j:]...)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|