-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
shorten.go
61 lines (52 loc) · 1.17 KB
/
shorten.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
package gstr
import (
"strings"
"unicode/utf8"
)
// TrimSubstrings remove all needles item from haystack.
func TrimSubstrings(haystack string, needles []string) (result string) {
for _, item := range needles {
haystack = strings.ReplaceAll(haystack, item, "")
}
return haystack
}
const (
DefaultShortenSuffix = "…"
)
// Shorten it cuts and concatenates a part of string with ellipsis if its length long than maxLen in unicode.
func Shorten(s string, n int) (rs string) {
strLen := utf8.RuneCountInString(s)
if strLen <= n {
rs = s
} else {
rs = Substring(s, 0, n)
}
return
}
func LenUtf8(s string) (n int) {
return utf8.RuneCountInString(s)
}
func ShortenWith(s string, n int, suffix string) (rs string) {
strLen := utf8.RuneCountInString(s)
if strLen <= n {
rs = s
} else {
rs = Substring(s, 0, n-1) + suffix
}
return
}
// https://stackoverflow.com/questions/28718682/how-to-get-a-substring-from-a-string-of-runes-in-golang
func Substring(s string, start int, end int) string {
start_str_idx := 0
i := 0
for j := range s {
if i == start {
start_str_idx = j
}
if i == end {
return s[start_str_idx:j]
}
i++
}
return s[start_str_idx:]
}