diff options
| author | Arda Serdar Pektezol <1669855+BiSaXa@users.noreply.github.com> | 2022-09-11 10:39:11 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-11 10:39:11 +0300 |
| commit | defb067368f8031ed59d720f6fb44e6770db7cea (patch) | |
| tree | c82e3562990365cfe54b3f55e56847c22b9a05ee | |
| parent | Merge pull request #2 from BiSaXa/dev (diff) | |
| parent | slight change for go doc (diff) | |
| download | bitreader-defb067368f8031ed59d720f6fb44e6770db7cea.tar.gz bitreader-defb067368f8031ed59d720f6fb44e6770db7cea.tar.bz2 bitreader-defb067368f8031ed59d720f6fb44e6770db7cea.zip | |
Merge pull request #3 from BiSaXa/devv1.1.1
changed readme for new version and slight change for go doc
| -rw-r--r-- | README.md | 36 | ||||
| -rw-r--r-- | bitreader.go | 13 |
2 files changed, 36 insertions, 13 deletions
| @@ -1,4 +1,4 @@ | |||
| 1 | # BitReader [](https://goreportcard.com/report/github.com/bisaxa/bitreader) [](https://github.com/bisaxa/bitreader/blob/main/LICENSE) | 1 | # BitReader [](https://goreportcard.com/report/github.com/bisaxa/bitreader) [](https://github.com/bisaxa/bitreader/blob/main/LICENSE) [](https://pkg.go.dev/github.com/bisaxa/bitreader) |
| 2 | A simple bit reader with big/little-endian support for golang.\ | 2 | A simple bit reader with big/little-endian support for golang.\ |
| 3 | Reads data from an existing byte array.\ | 3 | Reads data from an existing byte array.\ |
| 4 | Uses string manipulation (for now).\ | 4 | Uses string manipulation (for now).\ |
| @@ -15,24 +15,42 @@ $ go get github.com/bisaxa/bitreader | |||
| 15 | ```go | 15 | ```go |
| 16 | import "github.com/bisaxa/bitreader" | 16 | import "github.com/bisaxa/bitreader" |
| 17 | 17 | ||
| 18 | // data: []byte Data to read from byte array | 18 | // data: io.Reader Data to read from a io stream |
| 19 | // le: bool Little-endian(true) or big-endian(false) state | 19 | // le: bool Little-endian(true) or big-endian(false) state |
| 20 | reader := bitreader.Reader(data, le) | 20 | reader := bitreader.Reader(data, le) |
| 21 | 21 | ||
| 22 | // read first bit | 22 | // Read First Bit |
| 23 | state, err := reader.ReadBit() | 23 | state, err := reader.ReadBool() |
| 24 | 24 | ||
| 25 | // skip bits/bytes | 25 | // Skip Bits/Bytes |
| 26 | err := reader.SkipBits(8) | 26 | err := reader.SkipBits(8) |
| 27 | err := reader.SkipBytes(4) | 27 | err := reader.SkipBytes(4) |
| 28 | 28 | ||
| 29 | // read bits | 29 | // Read Bits/Bytes |
| 30 | value, err := reader.ReadBits(11) | 30 | value, err := reader.ReadBytes(4) |
| 31 | value, err := reader.ReadBits(64) // up to 64 bits | 31 | value, err := reader.ReadBits(64) // up to 64 bits |
| 32 | |||
| 33 | // Wrapper functions | ||
| 34 | state := reader.TryReadBool() // bool | ||
| 35 | value := reader.TryReadInt1() // uint8 | ||
| 36 | value := reader.TryReadInt8() // uint8 | ||
| 37 | value := reader.TryReadInt16() // uint16 | ||
| 38 | value := reader.TryReadInt32() // uint32 | ||
| 39 | value := reader.TryReadInt64() // uint64 | ||
| 40 | value := reader.TryReadFloat32() // float32 | ||
| 41 | value := reader.TryReadFloat64() // float64 | ||
| 32 | ``` | 42 | ``` |
| 33 | 43 | ||
| 34 | ## Error Handling | 44 | ## Error Handling |
| 35 | ReadBits(x), ReadBit(), SkipBits(x) and SkipBytes(x) functions returns an error message when they don't work as expected. It is advised to always handle errors. | 45 | 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. \ |
| 46 | Wrapper functions, however, only returns the value and panics if an error is encountered. | ||
| 47 | |||
| 48 | ## Bug Report / Feature Request | ||
| 49 | 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. | ||
| 50 | |||
| 51 | ## Documentation | ||
| 52 | |||
| 53 | Full documentation can be found in https://pkg.go.dev/github.com/bisaxa/bitreader | ||
| 36 | 54 | ||
| 37 | ## License | 55 | ## License |
| 38 | This project is licensed under [MIT License](LICENSE). \ No newline at end of file | 56 | This project is licensed under [MIT License](LICENSE). \ No newline at end of file |
diff --git a/bitreader.go b/bitreader.go index 3fbc221..a56aab8 100644 --- a/bitreader.go +++ b/bitreader.go | |||
| @@ -15,11 +15,16 @@ import ( | |||
| 15 | 15 | ||
| 16 | // ReaderType is the main structure of our Reader. | 16 | // ReaderType is the main structure of our Reader. |
| 17 | // Whenever index == 0, we need to read a new byte from stream into curByte | 17 | // Whenever index == 0, we need to read a new byte from stream into curByte |
| 18 | // | ||
| 19 | // stream io.Reader The underlying stream we're reading bytes from | ||
| 20 | // index uint18 The current index into the byte [0-7] | ||
| 21 | // curByte byte The byte we're currently reading from | ||
| 22 | // le bool Whether to read in little-endian order | ||
| 18 | type ReaderType struct { | 23 | type ReaderType struct { |
| 19 | stream io.Reader // The underlying stream we're reading bytes from | 24 | stream io.Reader |
| 20 | index uint8 // The current index into the byte [0-7] | 25 | index uint8 |
| 21 | curByte byte // The byte we're currently reading from | 26 | curByte byte |
| 22 | le bool // Whether to read in little-endian order | 27 | le bool |
| 23 | } | 28 | } |
| 24 | 29 | ||
| 25 | // Reader is the main constructor that creates the ReaderType object | 30 | // Reader is the main constructor that creates the ReaderType object |