diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2022-11-07 18:17:51 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2022-11-07 18:17:51 +0300 |
| commit | c8ceaa54253f82db12a1fe3bbc8ec19d41e44cac (patch) | |
| tree | b830cb92f214bf05c8de117a44f1fe0e862100a3 | |
| parent | update readme for ReadStringLen() (diff) | |
| download | bitreader-1.2.2.tar.gz bitreader-1.2.2.tar.bz2 bitreader-1.2.2.zip | |
add ReadBytesToSlice() functionv1.2.2
| -rw-r--r-- | bitreader.go | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/bitreader.go b/bitreader.go index d42b8e6..33383e9 100644 --- a/bitreader.go +++ b/bitreader.go | |||
| @@ -132,6 +132,20 @@ func (reader *ReaderType) TryReadStringLen(length int) string { | |||
| 132 | return text | 132 | return text |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | // TryReadBytesToSlice is a wrapper function that reads the specified amount of bytes | ||
| 136 | // from the parameter and puts each byte into a slice and returns this slice. | ||
| 137 | func (reader *ReaderType) TryReadBytesToSlice(bytes int) []byte { | ||
| 138 | var out []byte | ||
| 139 | for i := 0; i < bytes; i++ { | ||
| 140 | val, err := reader.ReadBytes(1) | ||
| 141 | if err != nil { | ||
| 142 | panic(err) | ||
| 143 | } | ||
| 144 | out = append(out, byte(val)) | ||
| 145 | } | ||
| 146 | return out | ||
| 147 | } | ||
| 148 | |||
| 135 | // SkipBits is a function that increases Reader index | 149 | // SkipBits is a function that increases Reader index |
| 136 | // based on given input bits number. | 150 | // based on given input bits number. |
| 137 | // | 151 | // |
| @@ -213,7 +227,7 @@ func (reader *ReaderType) ReadStringLen(length int) (string, error) { | |||
| 213 | } | 227 | } |
| 214 | 228 | ||
| 215 | // ReadBits is a function that reads the specified amount of bits | 229 | // ReadBits is a function that reads the specified amount of bits |
| 216 | // specified in the parameter and returns the value, error | 230 | // from the parameter and returns the value, error |
| 217 | // based on the output. It can read up to 64 bits. Returns the read | 231 | // based on the output. It can read up to 64 bits. Returns the read |
| 218 | // value in type uint64. | 232 | // value in type uint64. |
| 219 | // | 233 | // |
| @@ -239,7 +253,7 @@ func (reader *ReaderType) ReadBits(bits int) (uint64, error) { | |||
| 239 | } | 253 | } |
| 240 | 254 | ||
| 241 | // ReadBytes is a function that reads the specified amount of bytes | 255 | // ReadBytes is a function that reads the specified amount of bytes |
| 242 | // specified in the parameter and returns the value, error | 256 | // from the parameter and returns the value, error |
| 243 | // based on the output. It can read up to 8 bytes. Returns the read | 257 | // based on the output. It can read up to 8 bytes. Returns the read |
| 244 | // value in type uint64. | 258 | // value in type uint64. |
| 245 | // | 259 | // |
| @@ -255,6 +269,22 @@ func (reader *ReaderType) ReadBytes(bytes int) (uint64, error) { | |||
| 255 | return value, nil | 269 | return value, nil |
| 256 | } | 270 | } |
| 257 | 271 | ||
| 272 | // ReadBytesToSlice is a function that reads the specified amount of bytes | ||
| 273 | // from the parameter and puts each byte into a slice and returns this slice. | ||
| 274 | // | ||
| 275 | // Returns an error if there are no remaining bytes. | ||
| 276 | func (reader *ReaderType) ReadBytesToSlice(bytes int) ([]byte, error) { | ||
| 277 | var out []byte | ||
| 278 | for i := 0; i < bytes; i++ { | ||
| 279 | val, err := reader.ReadBytes(1) | ||
| 280 | if err != nil { | ||
| 281 | return out, err | ||
| 282 | } | ||
| 283 | out = append(out, byte(val)) | ||
| 284 | } | ||
| 285 | return out, nil | ||
| 286 | } | ||
| 287 | |||
| 258 | // ReadBool is a function that reads one bit and returns the state, error | 288 | // ReadBool is a function that reads one bit and returns the state, error |
| 259 | // based on the output. Returns the read value in a bool format. | 289 | // based on the output. Returns the read value in a bool format. |
| 260 | // | 290 | // |