blob: 45f13ca1389bbe4872f3d85fcc6360c8ae4b8bc2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# BitReader [](https://goreportcard.com/report/github.com/pektezol/bitreader) [](https://github.com/pektezol/bitreader/blob/main/LICENSE)
A simple bit reader with big/little-endian support for golang.\
Reads data from an existing byte array.\
Uses string manipulation (for now).\
Support reading up to 64 bits at one time.\
Checking for overflowing the data.
## Installation
```bash
$ go get github.com/pektezol/bitreader
```
## Usage
```go
import "github.com/pektezol/bitreader"
// data: io.Reader Data to read from a io stream
// le: bool Little-endian(true) or big-endian(false) state
reader := bitreader.Reader(data, le)
// Read First Bit
state, err := reader.ReadBool()
// Skip Bits/Bytes
err := reader.SkipBits(8)
err := reader.SkipBytes(4)
// Read Bits/Bytes
value, err := reader.ReadBytes(4)
value, err := reader.ReadBits(64) // up to 64 bits
// Wrapper functions
state := reader.TryReadBool() // bool
value := reader.TryReadInt1() // uint8
value := reader.TryReadInt8() // uint8
value := reader.TryReadInt16() // uint16
value := reader.TryReadInt32() // uint32
value := reader.TryReadInt64() // uint64
value := reader.TryReadFloat32() // float32
value := reader.TryReadFloat64() // float64
```
## Error Handling
ReadBits(x), ReadBytes(x), ReadBool(), SkipBits(x) and SkipBytes(x) functions returns an error message when they don't work as expected. It is advised to always handle errors. \
Wrapper functions, however, only returns the value and panics if an error is encountered.
## Bug Report / Feature Request
Using [Github Issues](https://github.com/BiSaXa/BitReader/issues/new/choose), you can report a bug that you encountered and/or request a feature that you would like to be added.
## Documentation
Full documentation can be found in https://pkg.go.dev/github.com/bisaxa/bitreader
## License
This project is licensed under [MIT License](LICENSE).
|