forked from leg100/otf
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstrings.go
More file actions
59 lines (52 loc) · 1.31 KB
/
strings.go
File metadata and controls
59 lines (52 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package internal
import "strings"
func NewStringFromPtr(s *string) string {
if s == nil {
return ""
}
return *s
}
// DiffStrings returns the elements in `a` that aren't in `b`.
func DiffStrings(a, b []string) []string {
mb := make(map[string]struct{}, len(b))
for _, x := range b {
mb[x] = struct{}{}
}
var diff []string
for _, x := range a {
if _, found := mb[x]; !found {
diff = append(diff, x)
}
}
return diff
}
// SplitCSV splits a string with a comma delimited (a "comma-separated-value").
// It differs from strings.Split in that if no comma is found an empty slice is
// returned whereas strings.Split would return a single-element slice containing the
// original string.
func SplitCSV(csv string) []string {
return strings.FieldsFunc(csv, func(r rune) bool { return r == ',' })
}
// FromStringCSV splits a comma-separated string into a slice of type T
func FromStringCSV[T ~string](csv string) (to []T) {
from := SplitCSV(csv)
to = make([]T, len(from))
for i, f := range SplitCSV(csv) {
to[i] = T(f)
}
return
}
func FromStringSlice[T ~string](from []string) (to []T) {
to = make([]T, len(from))
for i, f := range from {
to[i] = T(f)
}
return
}
func ToStringSlice[T ~string](from []T) (to []string) {
to = make([]string, len(from))
for i, f := range from {
to[i] = string(f)
}
return
}