-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.go
54 lines (49 loc) · 1.1 KB
/
funcs.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
package goutils
import (
"reflect"
"strings"
"unsafe"
)
// Isin return true if the element is in the slice.
func Isin(s []string, elem string) bool {
for _, ss := range s {
if ss == elem {
return true
}
}
return false
}
// InplaceStringToSlice transfer string into []byte inplace
func InplaceStringToSlice(s string) []byte {
return *(*[]byte)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&s))))
}
// InplaceSliceToString transfer []byte into string inplace
func InplaceSliceToString(s []byte) string {
return *(*string)(unsafe.Pointer(&s))
}
// ShortcutUTF8 return a valid UTF-8 string with at most `max` + len(suffix) characters.
//
// If `s` has more than `max` characters, cuts it to max and add `suffix`.
// Multibyte characters are handled resonable.
// If `max` is lower than 0, then return `s`.
func ShortcutUTF8(s string, max int, suffix string) string {
if max < 0 {
return s
}
if max == 0 {
return suffix
}
var w int
bs := strings.Builder{}
for _, r := range s {
w++
if w > max {
break
}
bs.WriteRune(r)
}
if w > max {
bs.WriteString(suffix)
}
return bs.String()
}