/
util.go
54 lines (48 loc) · 1.11 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
package util
import (
"bytes"
log "github.com/cihub/seelog"
)
// SplitHeaderBody separates header and body of buf using provided separator sep, and stores in returnByte.
func SplitHeaderBody(buf, sep *[]byte, returnByte *[][]byte) [][]byte {
if buf == nil {
log.Error("Buf to split passed nil")
return nil
}
if sep == nil {
log.Error("Separator used to split passed nil")
return nil
}
if returnByte == nil {
log.Error("Return Buf to be used to store split passed nil")
return nil
}
separator := *sep
bufVal := *buf
lenSeparator := len(separator)
var header, body []byte
header = *buf
for i := 0; i < len(bufVal); i++ {
if bytes.Equal(bufVal[i:i+lenSeparator], separator) {
header = bufVal[0:i]
body = bufVal[i+lenSeparator:]
break
}
if i == len(bufVal)-1 {
log.Warnf("Missing header: %s", header)
}
}
returnByteVal := *returnByte
return append(returnByteVal[:0], header, body)
}
// GetMinIntValue returns minimum between a and b.
func GetMinIntValue(a, b int) int {
if a < b {
return a
}
return b
}
// Bool return pointer to input parameter
func Bool(b bool) *bool {
return &b
}