-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring_slice.go
More file actions
142 lines (119 loc) · 2.83 KB
/
Copy pathstring_slice.go
File metadata and controls
142 lines (119 loc) · 2.83 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package helpers
import (
"sort"
"strings"
)
// CleanStringSlice takes an input and will attempt to "clean" the input by removing "empty" or "whitespace-only"
// values. You will always get a []string response, even if you pass in a nil
func CleanStringSlice(in []string) (out []string) {
var inVal, outVal string
// if in nil or empty...
if nil == in || 0 == len(in) {
return
}
for _, inVal = range in {
outVal = strings.TrimSpace(inVal)
if "" != outVal {
out = append(out, outVal)
}
}
return
}
// UniqueStringSlice takes a string array and returns a slice with duplicates removed
// This also strips "empty" values out. You will always get a []string response, even if you pass in a nil
func UniqueStringSlice(in []string) (out []string) {
var inVal, outVal string
// if in nil or empty...
if nil == in || 0 == len(in) {
return
}
UniqueLoop:
for _, inVal = range in {
// see if empty, but don't change values for non-empty
inVal = strings.TrimSpace(inVal)
if "" == inVal {
continue UniqueLoop
}
for _, outVal = range out {
if outVal == inVal {
continue UniqueLoop
}
}
out = append(out, inVal)
}
return
}
// CombineStringSlices takes 1..* string arrays and combines them into a single slice.
// This will also unique and strip "empty" values out, so y'know...there you go.
func CombineStringSlices(ins ...[]string) (out []string, delta int) {
var i int
var in []string
var inVal, outVal string
// if there was no input...
if 0 == len(ins) {
return
}
for i, in = range ins {
// if "empty", just move on
if nil == in || 0 == len(in) {
continue
}
ValueLoop:
for _, inVal = range UniqueStringSlice(in) {
for _, outVal = range out {
if outVal == inVal {
continue ValueLoop
}
}
out = append(out, inVal)
if 0 < i {
delta++
}
}
}
return
}
// RemoveStringsFromSlice will attempt to remove values present in "remove" from "root"
func RemoveStringsFromSlice(root, remove []string) (out []string, delta int) {
var rootVal, removeVal string
// if root is nil, just return.
if nil == root {
return
}
// if remove list empty, copy root to out
if nil == remove || 0 == len(remove) {
out = make([]string, len(root))
copy(out, root)
return
}
RootLoop:
for _, rootVal = range root {
for _, removeVal = range remove {
if rootVal == removeVal {
delta++
continue RootLoop
}
}
out = append(out, rootVal)
}
return
}
// StringSlicesEqual will attempt to determine if both provided string slices contain the same values. The original
// slices are not modified.
func StringSlicesEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
ac := make([]string, len(a))
bc := make([]string, len(b))
copy(ac, a)
copy(bc, b)
sort.Strings(ac)
sort.Strings(bc)
for i, v := range ac {
if bc[i] != v {
return false
}
}
return true
}