diff options
Diffstat (limited to 'utils/utils.go')
| -rw-r--r-- | utils/utils.go | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/utils/utils.go b/utils/utils.go index 46b707c..d25fa36 100644 --- a/utils/utils.go +++ b/utils/utils.go | |||
| @@ -1,6 +1,11 @@ | |||
| 1 | package utils | 1 | package utils |
| 2 | 2 | ||
| 3 | import "os" | 3 | import ( |
| 4 | "os" | ||
| 5 | "unsafe" | ||
| 6 | |||
| 7 | "github.com/bisaxa/bitreader" | ||
| 8 | ) | ||
| 4 | 9 | ||
| 5 | func CheckError(e error) { | 10 | func CheckError(e error) { |
| 6 | if e != nil { | 11 | if e != nil { |
| @@ -13,3 +18,30 @@ func ReadByteFromFile(file *os.File, size int32) []byte { | |||
| 13 | file.Read(tmp) | 18 | file.Read(tmp) |
| 14 | return tmp | 19 | return tmp |
| 15 | } | 20 | } |
| 21 | |||
| 22 | func ReadStringFromFile(file *os.File) string { | ||
| 23 | var output string | ||
| 24 | reader := bitreader.Reader(file, true) | ||
| 25 | for { | ||
| 26 | value, err := reader.ReadBytes(1) | ||
| 27 | CheckError(err) | ||
| 28 | if value == 0 { | ||
| 29 | break | ||
| 30 | } | ||
| 31 | output += string(rune(value)) | ||
| 32 | } | ||
| 33 | return output | ||
| 34 | } | ||
| 35 | |||
| 36 | func FloatArrFromBytes(byteArr []byte) []float32 { | ||
| 37 | if len(byteArr) == 0 { | ||
| 38 | return nil | ||
| 39 | } | ||
| 40 | l := len(byteArr) / 4 | ||
| 41 | ptr := unsafe.Pointer(&byteArr[0]) | ||
| 42 | // It is important to keep in mind that the Go garbage collector | ||
| 43 | // will not interact with this data, and that if src if freed, | ||
| 44 | // the behavior of any Go code using the slice is nondeterministic. | ||
| 45 | // Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices | ||
| 46 | return (*[1 << 26]float32)((*[1 << 26]float32)(ptr))[:l:l] | ||
| 47 | } | ||