-
Notifications
You must be signed in to change notification settings - Fork 4
/
string.go
90 lines (79 loc) · 1.8 KB
/
string.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
package goutil
import (
"crypto/rand"
r "math/rand"
"strings"
"time"
"unicode"
)
type is func(rune) bool
// StringIs checkes whether each runes of s satisfy f
func StringIs(s string, f is) bool {
for _, c := range s {
if !f(c) {
return false
}
}
return true
}
// IsASCII checkes whether r is within ASCII or not
func IsASCII(r rune) bool {
return r <= unicode.MaxASCII
}
// IsLatin1 checkes whether r is within Latin1 or not
func IsLatin1(r rune) bool {
return r <= unicode.MaxLatin1
}
// IsCJK checkes whether r is a CJK rune or not
func IsCJK(r rune) bool {
return unicode.Is(unicode.Scripts["Han"], r)
}
var noSpaceScripts = []string{"Han", "Lao", "Thai", "Tibetan"}
// NoSpaceWriting checkes whether r is within Han, Lao, Thai and Tibetan
func NoSpaceWriting(r rune) bool {
if unicode.IsPunct(r) {
return true
}
for _, s := range noSpaceScripts {
if unicode.Is(unicode.Scripts[s], r) {
return true
}
}
return false
}
// Join concates s smartly
func Join(s []string) string {
if len(s) == 0 {
return ""
}
var ss []string
pre := false
for _, str := range s {
cur := StringIs(str, func(r rune) bool { return !NoSpaceWriting(r) })
if pre && cur {
ss = append(ss, " ")
}
ss = append(ss, str)
pre = cur
}
return strings.Join(ss, "")
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// GenerateRandomString generate random strings of given length n
func GenerateRandomString(n int) string {
var bytes = make([]byte, n)
var randBy bool
if num, err := rand.Read(bytes); num != n || err != nil {
r.Seed(time.Now().UnixNano())
randBy = true
}
letterLen := len(letterBytes)
for i, b := range bytes {
if randBy {
bytes[i] = letterBytes[r.Intn(letterLen)]
} else {
bytes[i] = letterBytes[b%byte(letterLen)]
}
}
return string(bytes)
}