diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2022-11-11 22:18:27 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2022-11-11 22:18:27 +0300 |
| commit | 12c9959dbdfa8e22069c0fe8d7a2220e2c41633d (patch) | |
| tree | 5fc2f20edce69572d6badbd457c906ee993289d9 /bitreader.go | |
| parent | add ReadBitsToSlice() function (diff) | |
| download | bitreader-12c9959dbdfa8e22069c0fe8d7a2220e2c41633d.tar.gz bitreader-12c9959dbdfa8e22069c0fe8d7a2220e2c41633d.tar.bz2 bitreader-12c9959dbdfa8e22069c0fe8d7a2220e2c41633d.zip | |
fix ReadBitsToSlice logicv1.2.5
Diffstat (limited to 'bitreader.go')
| -rw-r--r-- | bitreader.go | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/bitreader.go b/bitreader.go index 5c7dc04..267ff5e 100644 --- a/bitreader.go +++ b/bitreader.go | |||
| @@ -155,22 +155,25 @@ func (reader *ReaderType) TryReadStringLen(length int) string { | |||
| 155 | // TryReadBytesToSlice is a wrapper function that reads the specified amount of bits | 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. | 156 | // from the parameter and puts each bit into a slice and returns this slice. |
| 157 | func (reader *ReaderType) TryReadBitsToSlice(bits int) []byte { | 157 | func (reader *ReaderType) TryReadBitsToSlice(bits int) []byte { |
| 158 | bytes := (bits / 8) + (bits % 8) | 158 | bytes := (bits / 8) |
| 159 | if bits%8 != 0 { | ||
| 160 | bytes++ | ||
| 161 | } | ||
| 159 | out := make([]byte, bytes) | 162 | out := make([]byte, bytes) |
| 160 | for i := 0; i < bytes; i++ { | 163 | for i := 0; i < bytes; i++ { |
| 161 | if bits/8 > 0 { | 164 | if i == bytes-1 { // Not enough to fill a whole byte |
| 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) | 165 | val, err := reader.ReadBits(bits % 8) |
| 169 | if err != nil { | 166 | if err != nil { |
| 170 | panic(err) | 167 | panic(err) |
| 171 | } | 168 | } |
| 172 | out[i] = byte(val) | 169 | out[i] = byte(val) |
| 170 | break | ||
| 173 | } | 171 | } |
| 172 | val, err := reader.ReadBytes(1) | ||
| 173 | if err != nil { | ||
| 174 | panic(err) | ||
| 175 | } | ||
| 176 | out[i] = byte(val) | ||
| 174 | } | 177 | } |
| 175 | return out | 178 | return out |
| 176 | } | 179 | } |
| @@ -317,22 +320,25 @@ func (reader *ReaderType) ReadBytes(bytes int) (uint64, error) { | |||
| 317 | // | 320 | // |
| 318 | // Returns an error if there are no remaining bits. | 321 | // Returns an error if there are no remaining bits. |
| 319 | func (reader *ReaderType) ReadBitsToSlice(bits int) ([]byte, error) { | 322 | func (reader *ReaderType) ReadBitsToSlice(bits int) ([]byte, error) { |
| 320 | bytes := (bits / 8) + (bits % 8) | 323 | bytes := (bits / 8) |
| 324 | if bits%8 != 0 { | ||
| 325 | bytes++ | ||
| 326 | } | ||
| 321 | out := make([]byte, bytes) | 327 | out := make([]byte, bytes) |
| 322 | for i := 0; i < bytes; i++ { | 328 | for i := 0; i < bytes; i++ { |
| 323 | if bits/8 > 0 { | 329 | if i == bytes-1 { // Not enough to fill a whole byte |
| 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) | 330 | val, err := reader.ReadBits(bits % 8) |
| 331 | if err != nil { | 331 | if err != nil { |
| 332 | panic(err) | 332 | return out, err |
| 333 | } | 333 | } |
| 334 | out[i] = byte(val) | 334 | out[i] = byte(val) |
| 335 | break | ||
| 336 | } | ||
| 337 | val, err := reader.ReadBytes(1) | ||
| 338 | if err != nil { | ||
| 339 | return out, err | ||
| 335 | } | 340 | } |
| 341 | out[i] = byte(val) | ||
| 336 | } | 342 | } |
| 337 | return out, nil | 343 | return out, nil |
| 338 | } | 344 | } |