aboutsummaryrefslogtreecommitdiff
path: root/bitreader.go
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2022-11-11 08:59:19 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2022-11-11 08:59:19 +0300
commitf628b40dc66467db2e299f115ea9bac75a297900 (patch)
tree030e558ad9949abff876021f6110fc6262aad427 /bitreader.go
parentadded wrapper funcs for read bits/bytes (diff)
downloadbitreader-f628b40dc66467db2e299f115ea9bac75a297900.tar.gz
bitreader-f628b40dc66467db2e299f115ea9bac75a297900.tar.bz2
bitreader-f628b40dc66467db2e299f115ea9bac75a297900.zip
add ReadBitsToSlice() functionv1.2.4
Diffstat (limited to 'bitreader.go')
-rw-r--r--bitreader.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/bitreader.go b/bitreader.go
index b9ac772..5c7dc04 100644
--- a/bitreader.go
+++ b/bitreader.go
@@ -152,6 +152,29 @@ func (reader *ReaderType) TryReadStringLen(length int) string {
152 return text 152 return text
153} 153}
154 154
155// TryReadBytesToSlice is a wrapper function that reads the specified amount of bits
156// from the parameter and puts each bit into a slice and returns this slice.
157func (reader *ReaderType) TryReadBitsToSlice(bits int) []byte {
158 bytes := (bits / 8) + (bits % 8)
159 out := make([]byte, bytes)
160 for i := 0; i < bytes; i++ {
161 if bits/8 > 0 {
162 val, err := reader.ReadBytes(1)
163 if err != nil {
164 panic(err)
165 }
166 out[i] = byte(val)
167 } else { // Not enough to fill a whole byte
168 val, err := reader.ReadBits(bits % 8)
169 if err != nil {
170 panic(err)
171 }
172 out[i] = byte(val)
173 }
174 }
175 return out
176}
177
155// TryReadBytesToSlice is a wrapper function that reads the specified amount of bytes 178// TryReadBytesToSlice is a wrapper function that reads the specified amount of bytes
156// from the parameter and puts each byte into a slice and returns this slice. 179// from the parameter and puts each byte into a slice and returns this slice.
157func (reader *ReaderType) TryReadBytesToSlice(bytes int) []byte { 180func (reader *ReaderType) TryReadBytesToSlice(bytes int) []byte {
@@ -289,6 +312,31 @@ func (reader *ReaderType) ReadBytes(bytes int) (uint64, error) {
289 return value, nil 312 return value, nil
290} 313}
291 314
315// ReadBitsToSlice is a function that reads the specified amount of bits
316// from the parameter and puts each bit into a slice and returns this slice.
317//
318// Returns an error if there are no remaining bits.
319func (reader *ReaderType) ReadBitsToSlice(bits int) ([]byte, error) {
320 bytes := (bits / 8) + (bits % 8)
321 out := make([]byte, bytes)
322 for i := 0; i < bytes; i++ {
323 if bits/8 > 0 {
324 val, err := reader.ReadBytes(1)
325 if err != nil {
326 return out, err
327 }
328 out[i] = byte(val)
329 } else { // Not enough to fill a whole byte
330 val, err := reader.ReadBits(bits % 8)
331 if err != nil {
332 panic(err)
333 }
334 out[i] = byte(val)
335 }
336 }
337 return out, nil
338}
339
292// ReadBytesToSlice is a function that reads the specified amount of bytes 340// ReadBytesToSlice is a function that reads the specified amount of bytes
293// from the parameter and puts each byte into a slice and returns this slice. 341// from the parameter and puts each byte into a slice and returns this slice.
294// 342//