forked from mlogclub/simple
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr.go
More file actions
122 lines (107 loc) · 2.23 KB
/
Copy pathstr.go
File metadata and controls
122 lines (107 loc) · 2.23 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
package simple
import (
"github.com/sirupsen/logrus"
"strings"
"unicode"
"github.com/PuerkitoBio/goquery"
uuid "github.com/iris-contrib/go.uuid"
)
/*
IsBlank checks if a string is whitespace or empty (""). Observe the following behavior:
goutils.IsBlank("") = true
goutils.IsBlank(" ") = true
goutils.IsBlank("bob") = false
goutils.IsBlank(" bob ") = false
Parameter:
str - the string to check
Returns:
true - if the string is whitespace or empty ("")
*/
func IsBlank(str string) bool {
strLen := len(str)
if str == "" || strLen == 0 {
return true
}
for i := 0; i < strLen; i++ {
if unicode.IsSpace(rune(str[i])) == false {
return false
}
}
return true
}
func IsNotBlank(str string) bool {
return !IsBlank(str)
}
func IsAnyBlank(strs ...string) bool {
for _, str := range strs {
if IsBlank(str) {
return true
}
}
return false
}
func DefaultIfBlank(str, def string) string {
if IsBlank(str) {
return def
} else {
return str
}
}
// IsEmpty checks if a string is empty (""). Returns true if empty, and false otherwise.
func IsEmpty(str string) bool {
return len(str) == 0
}
func IsNotEmpty(str string) bool {
return !IsEmpty(str)
}
// 截取字符串
func Substr(s string, start, length int) string {
bt := []rune(s)
if start < 0 {
start = 0
}
if start > len(bt) {
start = start % len(bt)
}
var end int
if (start + length) > (len(bt) - 1) {
end = len(bt)
} else {
end = start + length
}
return string(bt[start:end])
}
// UUID
func UUID() string {
u, _ := uuid.NewV4()
return strings.ReplaceAll(u.String(), "-", "")
}
func Equals(a, b string) bool {
return a == b
}
func EqualsIgnoreCase(a, b string) bool {
return a == b || strings.ToUpper(a) == strings.ToUpper(b)
}
// RuneLen 字符成长度
func RuneLen(s string) int {
bt := []rune(s)
return len(bt)
}
// GetSummary 获取summary
func GetSummary(s string, length int) string {
s = strings.TrimSpace(s)
summary := Substr(s, 0, length)
if RuneLen(s) > length {
summary += "..."
}
return summary
}
// GetHtmlText 获取html文本
func GetHtmlText(html string) string {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
logrus.Error(err)
return ""
}
return doc.Text()
}