diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2022-11-11 08:59:19 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2022-11-11 08:59:19 +0300 |
| commit | f628b40dc66467db2e299f115ea9bac75a297900 (patch) | |
| tree | 030e558ad9949abff876021f6110fc6262aad427 | |
| parent | added wrapper funcs for read bits/bytes (diff) | |
| download | bitreader-f628b40dc66467db2e299f115ea9bac75a297900.tar.gz bitreader-f628b40dc66467db2e299f115ea9bac75a297900.tar.bz2 bitreader-f628b40dc66467db2e299f115ea9bac75a297900.zip | |
add ReadBitsToSlice() functionv1.2.4
Diffstat (limited to '')
| -rw-r--r-- | README.md | 16 | ||||
| -rw-r--r-- | bitreader.go | 48 |
2 files changed, 59 insertions, 5 deletions
| @@ -28,16 +28,22 @@ err := reader.SkipBits(8) | |||
| 28 | err := reader.SkipBytes(4) | 28 | err := reader.SkipBytes(4) |
| 29 | 29 | ||
| 30 | // Read Bits/Bytes | 30 | // Read Bits/Bytes |
| 31 | value, err := reader.ReadBytes(4) | 31 | value, err := reader.ReadBytes(4) // up to 8 bytes |
| 32 | value, err := reader.ReadBits(64) // up to 64 bits | 32 | value, err := reader.ReadBits(64) // up to 64 bits |
| 33 | 33 | ||
| 34 | // Read String | 34 | // Read String |
| 35 | text, err := reader.ReadString() // null-terminated | 35 | text, err := reader.ReadString() // null-terminated |
| 36 | text, err := reader.ReadStringLen(256) // length-specified | 36 | text, err := reader.ReadStringLen(256) // length-specified |
| 37 | |||
| 38 | // Read Bits/Bytes into Slice | ||
| 39 | arr, err := reader.ReadBitsToSlice(128) | ||
| 40 | arr, err := reader.ReadBytesToSlice(64) | ||
| 37 | 41 | ||
| 38 | // Wrapper functions | 42 | // Wrapper functions |
| 39 | text := reader.TryReadString() // string | 43 | text := reader.TryReadString() // string |
| 40 | text := reader.TryReadStringLen(64) // string | 44 | text := reader.TryReadStringLen(64) // string |
| 45 | arr := reader.ReadBitsToSlice(128) // []byte | ||
| 46 | arr := reader.ReadBytesToSlice(64) // []byte | ||
| 41 | state := reader.TryReadBool() // bool | 47 | state := reader.TryReadBool() // bool |
| 42 | value := reader.TryReadInt1() // uint8 | 48 | value := reader.TryReadInt1() // uint8 |
| 43 | value := reader.TryReadInt8() // uint8 | 49 | value := reader.TryReadInt8() // uint8 |
| @@ -51,7 +57,7 @@ value := reader.TryReadBytes(8) // uint64 | |||
| 51 | ``` | 57 | ``` |
| 52 | 58 | ||
| 53 | ## Error Handling | 59 | ## Error Handling |
| 54 | ReadBits(x), ReadBytes(x), ReadBool(), ReadString(), ReadStringLen(x) SkipBits(x) and SkipBytes(x) functions returns an error message when they don't work as expected. It is advised to always handle errors. \ | 60 | ReadBits(x), ReadBytes(x), ReadBool(), ReadString(), ReadStringLen(x), ReadBitsToSlice(x), ReadBytesToSlice(x), SkipBits(x) and SkipBytes(x) functions returns an error message when they don't work as expected. It is advised to always handle errors. \ |
| 55 | Wrapper functions, however, only returns the value and panics if an error is encountered. | 61 | Wrapper functions, however, only returns the value and panics if an error is encountered. |
| 56 | 62 | ||
| 57 | ## Bug Report / Feature Request | 63 | ## Bug Report / Feature Request |
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. | ||
| 157 | func (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. |
| 157 | func (reader *ReaderType) TryReadBytesToSlice(bytes int) []byte { | 180 | func (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. | ||
| 319 | func (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 | // |