diff options
| author | BiSaXa <1669855+BiSaXa@users.noreply.github.com> | 2022-09-07 23:08:58 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-16 21:39:36 +0300 |
| commit | 0b8d982acae2ae102e6dee29afff3b621f32cd8f (patch) | |
| tree | 5754719ec8f17745bc6413f621974f1120443788 /utils | |
| parent | first rewrite commit using bisaxa/bitreader (diff) | |
| download | sdp.go-0b8d982acae2ae102e6dee29afff3b621f32cd8f.tar.gz sdp.go-0b8d982acae2ae102e6dee29afff3b621f32cd8f.tar.bz2 sdp.go-0b8d982acae2ae102e6dee29afff3b621f32cd8f.zip | |
class parses and other stuff
Diffstat (limited to 'utils')
| -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 | } | ||