-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.go
58 lines (49 loc) · 1.3 KB
/
helper.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
package strutil
import (
"html"
"regexp"
"strings"
"github.com/miku/span/container"
)
// ISSNPattern is a regular expression matching standard ISSN.
var ISSNPattern = regexp.MustCompile(`[0-9]{4,4}-[0-9]{3,3}[0-9X]`)
// Truncate truncates a string.
func Truncate(s string, length int) string {
if len(s) < length || length < 0 {
return s
}
return s[:length] + "..."
}
// UnescapeTrim unescapes HTML character references and trims the space of a given string.
func UnescapeTrim(s string) string {
return strings.TrimSpace(html.UnescapeString(s))
}
// StringSliceContains returns true, if a given string is contained in a slice.
func StringSliceContains(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}
// RemoveEach returns a new slice with elements not contained in a drop list.
func RemoveEach(ss []string, drop []string) (result []string) {
for _, s := range ss {
if !StringSliceContains(drop, s) {
result = append(result, s)
}
}
return
}
// Intersection returns strings contained in boths given slices.
func Intersection(a, b []string) []string {
var (
A = container.NewStringSet(a...)
B = container.NewStringSet(b...)
)
return A.Intersection(B).SortedValues()
}
func Overlap(a, b []string) bool {
return len(Intersection(a, b)) > 0
}