diff options
| -rw-r--r-- | README.md | 13 | ||||
| -rw-r--r-- | bitreader.go | 37 |
2 files changed, 42 insertions, 8 deletions
| @@ -1,9 +1,10 @@ | |||
| 1 | # BitReader [](https://goreportcard.com/report/github.com/pektezol/bitreader) [](https://github.com/pektezol/bitreader/blob/main/LICENSE) [](https://pkg.go.dev/github.com/pektezol/bitreader) | 1 | # BitReader [](https://goreportcard.com/report/github.com/pektezol/bitreader) [](https://github.com/pektezol/bitreader/blob/main/LICENSE) [](https://pkg.go.dev/github.com/pektezol/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 stream data from an io.Reader; can read from os.File and a byte array with bytes.NewReader(array).\ |
| 4 | Uses string manipulation (for now).\ | 4 | Uses bitwise operations.\ |
| 5 | Support reading up to 64 bits at one time.\ | 5 | Support reading up to 64 bits at one time.\ |
| 6 | Checking for overflowing the data. | 6 | Includes wrapper functions for most used data types.\ |
| 7 | Error checking on all but wrapper functions.\ | ||
| 7 | 8 | ||
| 8 | ## Installation | 9 | ## Installation |
| 9 | ```bash | 10 | ```bash |
| @@ -30,7 +31,11 @@ err := reader.SkipBytes(4) | |||
| 30 | value, err := reader.ReadBytes(4) | 31 | value, err := reader.ReadBytes(4) |
| 31 | value, err := reader.ReadBits(64) // up to 64 bits | 32 | value, err := reader.ReadBits(64) // up to 64 bits |
| 32 | 33 | ||
| 34 | // Read String | ||
| 35 | value, err := reader.ReadString() // null-terminated | ||
| 36 | |||
| 33 | // Wrapper functions | 37 | // Wrapper functions |
| 38 | text := reader.ReadString() // string | ||
| 34 | state := reader.TryReadBool() // bool | 39 | state := reader.TryReadBool() // bool |
| 35 | value := reader.TryReadInt1() // uint8 | 40 | value := reader.TryReadInt1() // uint8 |
| 36 | value := reader.TryReadInt8() // uint8 | 41 | value := reader.TryReadInt8() // uint8 |
| @@ -42,7 +47,7 @@ value := reader.TryReadFloat64() // float64 | |||
| 42 | ``` | 47 | ``` |
| 43 | 48 | ||
| 44 | ## Error Handling | 49 | ## Error Handling |
| 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. \ | 50 | ReadBits(x), ReadBytes(x), ReadBool(), ReadString(), 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. | 51 | Wrapper functions, however, only returns the value and panics if an error is encountered. |
| 47 | 52 | ||
| 48 | ## Bug Report / Feature Request | 53 | ## Bug Report / Feature Request |
diff --git a/bitreader.go b/bitreader.go index a56aab8..d3cc71f 100644 --- a/bitreader.go +++ b/bitreader.go | |||
| @@ -118,9 +118,17 @@ func (reader *ReaderType) TryReadFloat64() float64 { | |||
| 118 | return math.Float64frombits(value) | 118 | return math.Float64frombits(value) |
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | // TryReadString is a wrapper function that returns the string | ||
| 122 | // that is read until it is null-terminated. | ||
| 123 | func (reader *ReaderType) TryReadString() string { | ||
| 124 | text, _ := reader.ReadString() | ||
| 125 | return text | ||
| 126 | } | ||
| 127 | |||
| 121 | // SkipBits is a function that increases Reader index | 128 | // SkipBits is a function that increases Reader index |
| 122 | // based on given input bits number. Returns an error | 129 | // based on given input bits number. |
| 123 | // if there are no remaining bits. | 130 | // |
| 131 | // Returns an error if there are no remaining bits. | ||
| 124 | func (reader *ReaderType) SkipBits(bits int) error { | 132 | func (reader *ReaderType) SkipBits(bits int) error { |
| 125 | // Read as many raw bytes as we can | 133 | // Read as many raw bytes as we can |
| 126 | bytes := bits / 8 | 134 | bytes := bits / 8 |
| @@ -144,8 +152,9 @@ func (reader *ReaderType) SkipBits(bits int) error { | |||
| 144 | } | 152 | } |
| 145 | 153 | ||
| 146 | // SkipBytes is a function that increases Reader index | 154 | // SkipBytes is a function that increases Reader index |
| 147 | // based on given input bytes number. Returns an error | 155 | // based on given input bytes number. |
| 148 | // if there are no remaining bits. | 156 | // |
| 157 | // Returns an error if there are no remaining bits. | ||
| 149 | func (reader *ReaderType) SkipBytes(bytes int) error { | 158 | func (reader *ReaderType) SkipBytes(bytes int) error { |
| 150 | err := reader.SkipBits(bytes * 8) | 159 | err := reader.SkipBits(bytes * 8) |
| 151 | if err != nil { | 160 | if err != nil { |
| @@ -154,6 +163,26 @@ func (reader *ReaderType) SkipBytes(bytes int) error { | |||
| 154 | return nil | 163 | return nil |
| 155 | } | 164 | } |
| 156 | 165 | ||
| 166 | // ReadString is a function that reads every byte | ||
| 167 | // until it is null-terminated (the byte is 0). Returns the | ||
| 168 | // string that is read until the null-termination. | ||
| 169 | // | ||
| 170 | // Returns an error if there are no remaining bits. | ||
| 171 | func (reader *ReaderType) ReadString() (string, error) { | ||
| 172 | var out string | ||
| 173 | for { | ||
| 174 | value, err := reader.ReadBytes(1) | ||
| 175 | if err != nil { | ||
| 176 | return out, err | ||
| 177 | } | ||
| 178 | if value == 0 { | ||
| 179 | break | ||
| 180 | } | ||
| 181 | out += string(rune(value)) | ||
| 182 | } | ||
| 183 | return out, nil | ||
| 184 | } | ||
| 185 | |||
| 157 | // ReadBits is a function that reads the specified amount of bits | 186 | // ReadBits is a function that reads the specified amount of bits |
| 158 | // specified in the parameter and returns the value, error | 187 | // specified in the parameter and returns the value, error |
| 159 | // based on the output. It can read up to 64 bits. Returns the read | 188 | // based on the output. It can read up to 64 bits. Returns the read |