aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2022-11-07 12:11:06 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2022-11-07 12:11:06 +0300
commit2c6eea0886192a1bc07f564ef26c664b0953b73d (patch)
tree19df9ac262cd83fe864595ccf93a464bbb63873b
parentDelete .github/ISSUE_TEMPLATE directory (diff)
downloadbitreader-2c6eea0886192a1bc07f564ef26c664b0953b73d.tar.gz
bitreader-2c6eea0886192a1bc07f564ef26c664b0953b73d.tar.bz2
bitreader-2c6eea0886192a1bc07f564ef26c664b0953b73d.zip
add ReadString() function
Diffstat (limited to '')
-rw-r--r--README.md13
-rw-r--r--bitreader.go37
2 files changed, 42 insertions, 8 deletions
diff --git a/README.md b/README.md
index 9e9807a..6e70b37 100644
--- a/README.md
+++ b/README.md
@@ -1,9 +1,10 @@
1# BitReader [![Go Report Card](https://goreportcard.com/badge/github.com/pektezol/bitreader)](https://goreportcard.com/report/github.com/pektezol/bitreader) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/pektezol/bitreader/blob/main/LICENSE) [![Go Reference](https://pkg.go.dev/badge/github.com/pektezol/bitreader.svg)](https://pkg.go.dev/github.com/pektezol/bitreader) 1# BitReader [![Go Report Card](https://goreportcard.com/badge/github.com/pektezol/bitreader)](https://goreportcard.com/report/github.com/pektezol/bitreader) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/pektezol/bitreader/blob/main/LICENSE) [![Go Reference](https://pkg.go.dev/badge/github.com/pektezol/bitreader.svg)](https://pkg.go.dev/github.com/pektezol/bitreader)
2A simple bit reader with big/little-endian support for golang.\ 2A simple bit reader with big/little-endian support for golang.\
3Reads data from an existing byte array.\ 3Reads stream data from an io.Reader; can read from os.File and a byte array with bytes.NewReader(array).\
4Uses string manipulation (for now).\ 4Uses bitwise operations.\
5Support reading up to 64 bits at one time.\ 5Support reading up to 64 bits at one time.\
6Checking for overflowing the data. 6Includes wrapper functions for most used data types.\
7Error checking on all but wrapper functions.\
7 8
8## Installation 9## Installation
9```bash 10```bash
@@ -30,7 +31,11 @@ err := reader.SkipBytes(4)
30value, err := reader.ReadBytes(4) 31value, err := reader.ReadBytes(4)
31value, err := reader.ReadBits(64) // up to 64 bits 32value, err := reader.ReadBits(64) // up to 64 bits
32 33
34// Read String
35value, err := reader.ReadString() // null-terminated
36
33// Wrapper functions 37// Wrapper functions
38text := reader.ReadString() // string
34state := reader.TryReadBool() // bool 39state := reader.TryReadBool() // bool
35value := reader.TryReadInt1() // uint8 40value := reader.TryReadInt1() // uint8
36value := reader.TryReadInt8() // uint8 41value := reader.TryReadInt8() // uint8
@@ -42,7 +47,7 @@ value := reader.TryReadFloat64() // float64
42``` 47```
43 48
44## Error Handling 49## Error Handling
45ReadBits(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. \ 50ReadBits(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. \
46Wrapper functions, however, only returns the value and panics if an error is encountered. 51Wrapper 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.
123func (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.
124func (reader *ReaderType) SkipBits(bits int) error { 132func (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.
149func (reader *ReaderType) SkipBytes(bytes int) error { 158func (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.
171func (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