forked from futzu/cuei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
89 lines (75 loc) · 1.59 KB
/
base.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
package cuei
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"math/big"
)
// chk generic catchall error checking
func chk(e error) {
if e != nil {
fmt.Println(e)
}
}
// decB64 decodes base64 strings.
func decB64(b64 string) []byte {
deb64, err := base64.StdEncoding.DecodeString(b64)
chk(err)
return deb64
}
// encB64 encodes bytes to a Base64 string
func encB64(data []byte) string {
b64 := base64.StdEncoding.EncodeToString(data)
return b64
}
// hex2Int Hexidecimal string to uint64
func hex2Int(str string) uint64 {
i := new(big.Int)
_, err := fmt.Sscan(str, i)
chk(err)
return i.Uint64()
}
// isIn is a test for slice membership
func isIn[T comparable](slice []T, val T) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
func mk90k(raw uint64) float64 {
nk := float64(raw) / 90000.0
return float64(uint64(nk*1000000)) / 1000000
}
// mkJson structs to JSON
func mkJson(i interface{}) string {
jason, err := json.MarshalIndent(&i, "", " ")
chk(err)
return string(jason)
}
// Take a JSON string and return a *Cue
func Json2Cue(s string) *Cue {
b := []byte(s)
cue := NewCue()
json.Unmarshal(b, cue)
cue.Encode()
return cue
}
func parseLen(byte1, byte2 byte) uint16 {
return uint16(byte1&0xf)<<8 | uint16(byte2)
}
func parsePid(byte1, byte2 byte) uint16 {
return uint16(byte1&0x1f)<<8 | uint16(byte2)
}
func parsePrgm(byte1, byte2 byte) uint16 {
return uint16(byte1)<<8 | uint16(byte2)
}
func splitByIdx(payload, sep []byte) []byte {
idx := bytes.Index(payload, sep)
if idx == -1 {
return []byte("")
}
return payload[idx:]
}