From 0b8d982acae2ae102e6dee29afff3b621f32cd8f Mon Sep 17 00:00:00 2001 From: BiSaXa <1669855+BiSaXa@users.noreply.github.com> Date: Wed, 7 Sep 2022 23:08:58 +0300 Subject: class parses and other stuff --- utils/utils.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'utils/utils.go') 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 @@ package utils -import "os" +import ( + "os" + "unsafe" + + "github.com/bisaxa/bitreader" +) func CheckError(e error) { if e != nil { @@ -13,3 +18,30 @@ func ReadByteFromFile(file *os.File, size int32) []byte { 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] +} -- cgit v1.2.3