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