-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
71 lines (63 loc) · 1.44 KB
/
util.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
package gofast
import "reflect"
import "unsafe"
import "strings"
import "strconv"
import "bytes"
import "fmt"
func bytes2str(bytes []byte) string {
if bytes == nil {
return ""
}
sl := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
st := &reflect.StringHeader{Data: sl.Data, Len: sl.Len}
return *(*string)(unsafe.Pointer(st))
}
func str2bytes(str string) []byte {
if str == "" {
return nil
}
st := (*reflect.StringHeader)(unsafe.Pointer(&str))
sl := &reflect.SliceHeader{Data: st.Data, Len: st.Len, Cap: st.Len}
return *(*[]byte)(unsafe.Pointer(sl))
}
func hasString(str string, strs []string) bool {
for _, s := range strs {
if s == str {
return true
}
}
return false
}
func csv2strings(line string, out []string) []string {
for _, str := range strings.Split(line, ",") {
if s := strings.Trim(str, " \n\t\r"); s != "" {
out = append(out, s)
}
}
return out
}
func getStackTrace(skip int, stack []byte) string {
var buf bytes.Buffer
lines := strings.Split(string(stack), "\n")
for _, call := range lines[skip*2:] {
buf.WriteString(fmt.Sprintf("%s\n", call))
}
return buf.String()
}
func hexstring(data []byte) string {
out := "["
for _, b := range data {
out += strconv.FormatInt(int64(b), 16) + ", "
}
out += "]"
return out
}
func fixbuffer(buffer []byte, size int64) []byte {
if size == 0 {
return buffer
} else if buffer == nil || int64(cap(buffer)) < size {
return make([]byte, size)
}
return buffer[:size]
}