This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extstr.go
108 lines (98 loc) · 2.42 KB
/
extstr.go
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
package extstr
import (
"math/rand"
"strconv"
"strings"
)
// Join concatenates the elements of its first argument to create a single string. The separator
// string sep is placed between elements in the resulting string.
func Join(elems []int64, sep string) string {
switch len(elems) {
case 0:
return ""
case 1:
return strconv.FormatInt(elems[0], 10)
}
strElems := make([]string, 0, len(elems))
n := len(sep) * (len(elems) - 1)
for i := 0; i < len(elems); i++ {
v := strconv.FormatInt(elems[i], 10)
strElems = Append(strElems, v)
n += len(v)
}
var b strings.Builder
b.Grow(n)
b.WriteString(strElems[0])
for _, s := range strElems[1:] {
b.WriteString(sep)
b.WriteString(s)
}
return b.String()
}
// JoinInt concatenates the elements of its first argument to create a single string. The separator
// string sep is placed between elements in the resulting string.
func JoinInt(elems []int, sep string) string {
switch len(elems) {
case 0:
return ""
case 1:
return strconv.Itoa(elems[0])
}
strElems := make([]string, 0, len(elems))
n := len(sep) * (len(elems) - 1)
for i := 0; i < len(elems); i++ {
v := strconv.Itoa(elems[i])
strElems = Append(strElems, v)
n += len(v)
}
var b strings.Builder
b.Grow(n)
b.WriteString(strElems[0])
for _, s := range strElems[1:] {
b.WriteString(sep)
b.WriteString(s)
}
return b.String()
}
// Split Split slices s into all substrings separated by sep and returns a slice of
// the int64 between those separators.
func Split(s, sep string) []int64 {
if s == "" {
return []int64{}
}
ss := strings.Split(s, sep)
res := make([]int64, 0, len(ss))
for i := 0; i < len(ss); i++ {
v, err := strconv.ParseInt(strings.TrimSpace(ss[i]), 10, 64)
if err != nil {
continue
}
res = append(res, v)
}
return res
}
// SplitInt Split slices s into all substrings separated by sep and returns a slice of
// the int between those separators.
func SplitInt(s, sep string) []int {
if s == "" {
return []int{}
}
ss := strings.Split(s, sep)
res := make([]int, 0, len(ss))
for i := 0; i < len(ss); i++ {
v, err := strconv.Atoi(strings.TrimSpace(ss[i]))
if err != nil {
continue
}
res = append(res, v)
}
return res
}
// Shuffle pseudo-randomizes the order of elements using the default Source.
func Shuffle(str string) string {
runes := []rune(str)
rand.Shuffle(len(runes), func(i, j int) {
runes[i], runes[j] = runes[j], runes[i]
})
return string(runes)
}