Skip to content

Commit 4c01fa6

Browse files
committed
feat: split string list elem
1 parent 4e17813 commit 4c01fa6

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

string.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package kutils
2+
3+
import (
4+
"strings"
5+
)
6+
7+
// SplitListElem split each element in a string list with the given separator
8+
func SplitListElem(lst []string, sep string) []string {
9+
ret := make([]string, 0, len(lst))
10+
for _, elem := range lst {
11+
ret = append(ret, strings.Split(elem, sep)...)
12+
}
13+
return ret
14+
}

string_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package kutils
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
var (
11+
sep = ","
12+
splitElem = strings.Repeat("0", 40)
13+
splittableElem = splitElem + sep + splitElem + sep + splitElem
14+
strSplitInMiddle = []string{splitElem, splitElem, splittableElem, splitElem, splittableElem}
15+
strNoSplit = []string{splitElem, splitElem, splitElem, splitElem, splitElem}
16+
strAllSplittable = []string{splittableElem, splittableElem, splittableElem, splittableElem}
17+
)
18+
19+
// BenchmarkSplitListElem/split_in_middle
20+
// BenchmarkSplitListElem/split_in_middle-16 3464385 3
21+
// 39.5 ns/op vs 51.0 ns/op using strings.Contains
22+
// BenchmarkSplitListElem/no_split
23+
// BenchmarkSplitListElem/no_split-16 5432838 2
24+
// 22.2 ns/op vs 67.71 ns/op using strings.Contains
25+
// BenchmarkSplitListElem/all_splittable
26+
// BenchmarkSplitListElem/all_splittable-16 2987763 3
27+
// 95.3 ns/op vs 16.8 ns/op using strings.Contains
28+
func BenchmarkSplitListElem(b *testing.B) {
29+
b.ResetTimer()
30+
b.Run("split in middle", func(b *testing.B) {
31+
for i := 0; i < b.N; i++ {
32+
SplitListElem(strSplitInMiddle, sep)
33+
}
34+
})
35+
b.Run("no split", func(b *testing.B) {
36+
for i := 0; i < b.N; i++ {
37+
SplitListElem(strNoSplit, sep)
38+
}
39+
})
40+
b.Run("all splittable", func(b *testing.B) {
41+
for i := 0; i < b.N; i++ {
42+
SplitListElem(strAllSplittable, sep)
43+
}
44+
})
45+
}
46+
47+
func TestSplitListElem(t *testing.T) {
48+
type args struct {
49+
lst []string
50+
sep string
51+
}
52+
tests := []struct {
53+
name string
54+
args args
55+
want []string
56+
}{
57+
{
58+
"split in middle",
59+
args{
60+
lst: strSplitInMiddle,
61+
sep: sep,
62+
},
63+
[]string{splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem},
64+
},
65+
{
66+
"no split",
67+
args{
68+
lst: strNoSplit,
69+
sep: sep,
70+
},
71+
[]string{splitElem, splitElem, splitElem, splitElem, splitElem},
72+
},
73+
{
74+
"split in middle",
75+
args{
76+
lst: strAllSplittable,
77+
sep: sep,
78+
},
79+
[]string{splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem,
80+
splitElem, splitElem, splitElem},
81+
},
82+
}
83+
for _, tt := range tests {
84+
t.Run(tt.name, func(t *testing.T) {
85+
assert.Equalf(t, tt.want, SplitListElem(tt.args.lst, tt.args.sep), "SplitListElem(%v, %v)", tt.args.lst,
86+
tt.args.sep)
87+
})
88+
}
89+
}

0 commit comments

Comments
 (0)