-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.go
154 lines (125 loc) · 2.88 KB
/
utils.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package http2utils
import (
"bytes"
"crypto/rand"
"fmt"
"log"
"path/filepath"
"reflect"
"runtime"
"testing"
"text/tabwriter"
"unsafe"
"github.com/valyala/fastrand"
)
func Uint24ToBytes(b []byte, n uint32) {
_ = b[2] // bound checking
b[0] = byte(n >> 16)
b[1] = byte(n >> 8)
b[2] = byte(n)
}
func BytesToUint24(b []byte) uint32 {
_ = b[2] // bound checking
return uint32(b[0])<<16 |
uint32(b[1])<<8 |
uint32(b[2])
}
func AppendUint32Bytes(dst []byte, n uint32) []byte {
return append(dst, byte(n>>24), byte(n>>16), byte(n>>8), byte(n))
}
func Uint32ToBytes(b []byte, n uint32) {
_ = b[3] // bound checking
b[0] = byte(n >> 24)
b[1] = byte(n >> 16)
b[2] = byte(n >> 8)
b[3] = byte(n)
}
func BytesToUint32(b []byte) uint32 {
_ = b[3] // bound checking
n := uint32(b[0])<<24 |
uint32(b[1])<<16 |
uint32(b[2])<<8 |
uint32(b[3])
return n
}
func EqualsFold(a, b []byte) bool {
n := len(a)
if n != len(b) {
return false
}
for i := 0; i < n; i++ {
if a[i]|0x20 != b[i]|0x20 {
return false
}
}
return true
}
func Resize(b []byte, neededLen int) []byte {
b = b[:cap(b)]
if n := neededLen - len(b); n > 0 {
b = append(b, make([]byte, n)...)
}
return b[:neededLen]
}
// CutPadding cuts the padding if the frame has FlagPadded
// from the payload and returns the new payload as byte slice.
func CutPadding(payload []byte, length int) ([]byte, error) {
pad := int(payload[0])
if len(payload) < length-pad-1 || length-pad < 1 {
return nil, fmt.Errorf("out of range: %d < %d", len(payload), length-pad-1)
}
payload = payload[1 : length-pad]
return payload, nil
}
func AddPadding(b []byte) []byte {
n := int(fastrand.Uint32n(256-9)) + 9
nn := len(b)
b = Resize(b, nn+n)
b = append(b[:1], b...)
b[0] = uint8(n)
_, _ = rand.Read(b[nn+1 : nn+n])
return b
}
func FastBytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// AssertEqual checks if values are equal.
func AssertEqual(tb testing.TB, expected, actual interface{}, description ...string) {
tb.Helper()
if reflect.DeepEqual(expected, actual) {
return
}
aType := "<nil>"
bType := "<nil>"
if expected != nil {
aType = reflect.TypeOf(expected).String()
}
if actual != nil {
bType = reflect.TypeOf(actual).String()
}
testName := "AssertEqual"
if tb != nil {
testName = tb.Name()
}
_, file, line, _ := runtime.Caller(1)
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 5, ' ', 0)
fmt.Fprintf(w, "\nTest:\t%s", testName)
fmt.Fprintf(w, "\nTrace:\t%s:%d", filepath.Base(file), line)
if len(description) > 0 {
fmt.Fprintf(w, "\nDescription:\t%s", description[0])
}
fmt.Fprintf(w, "\nExpect:\t%v\t(%s)", expected, aType)
fmt.Fprintf(w, "\nResult:\t%v\t(%s)", actual, bType)
var result string
if err := w.Flush(); err != nil {
result = err.Error()
} else {
result = buf.String()
}
if tb != nil {
tb.Fatal(result)
} else {
log.Fatal(result)
}
}