blob: d25fa36efcce42be2990d8c65d3a2db9a4fc0e69 (
plain) (
blame)
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
|
package utils
import (
"os"
"unsafe"
"github.com/bisaxa/bitreader"
)
func CheckError(e error) {
if e != nil {
panic(e)
}
}
func ReadByteFromFile(file *os.File, size int32) []byte {
tmp := make([]byte, size)
file.Read(tmp)
return tmp
}
func ReadStringFromFile(file *os.File) string {
var output string
reader := bitreader.Reader(file, true)
for {
value, err := reader.ReadBytes(1)
CheckError(err)
if value == 0 {
break
}
output += string(rune(value))
}
return output
}
func FloatArrFromBytes(byteArr []byte) []float32 {
if len(byteArr) == 0 {
return nil
}
l := len(byteArr) / 4
ptr := unsafe.Pointer(&byteArr[0])
// It is important to keep in mind that the Go garbage collector
// will not interact with this data, and that if src if freed,
// the behavior of any Go code using the slice is nondeterministic.
// Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
return (*[1 << 26]float32)((*[1 << 26]float32)(ptr))[:l:l]
}
|