aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md16
-rw-r--r--bitreader.go48
2 files changed, 59 insertions, 5 deletions
diff --git a/README.md b/README.md
index 1093153..2bc00e7 100644
--- a/README.md
+++ b/README.md
@@ -28,16 +28,22 @@ err := reader.SkipBits(8)
28err := reader.SkipBytes(4) 28err := reader.SkipBytes(4)
29 29
30// Read Bits/Bytes 30// Read Bits/Bytes
31value, err := reader.ReadBytes(4) 31value, err := reader.ReadBytes(4) // up to 8 bytes
32value, err := reader.ReadBits(64) // up to 64 bits 32value, err := reader.ReadBits(64) // up to 64 bits
33 33
34// Read String 34// Read String
35text, err := reader.ReadString() // null-terminated 35text, err := reader.ReadString() // null-terminated
36text, err := reader.ReadStringLen(256) // length-specified 36text, err := reader.ReadStringLen(256) // length-specified
37
38// Read Bits/Bytes into Slice
39arr, err := reader.ReadBitsToSlice(128)
40arr, err := reader.ReadBytesToSlice(64)
37 41
38// Wrapper functions 42// Wrapper functions
39text := reader.TryReadString() // string 43text := reader.TryReadString() // string
40text := reader.TryReadStringLen(64) // string 44text := reader.TryReadStringLen(64) // string
45arr := reader.ReadBitsToSlice(128) // []byte
46arr := reader.ReadBytesToSlice(64) // []byte
41state := reader.TryReadBool() // bool 47state := reader.TryReadBool() // bool
42value := reader.TryReadInt1() // uint8 48value := reader.TryReadInt1() // uint8
43value := reader.TryReadInt8() // uint8 49value := reader.TryReadInt8() // uint8
@@ -51,7 +57,7 @@ value := reader.TryReadBytes(8) // uint64
51``` 57```
52 58
53## Error Handling 59## Error Handling
54ReadBits(x), ReadBytes(x), ReadBool(), ReadString(), ReadStringLen(x) SkipBits(x) and SkipBytes(x) functions returns an error message when they don't work as expected. It is advised to always handle errors. \ 60ReadBits(x), ReadBytes(x), ReadBool(), ReadString(), ReadStringLen(x), ReadBitsToSlice(x), ReadBytesToSlice(x), SkipBits(x) and SkipBytes(x) functions returns an error message when they don't work as expected. It is advised to always handle errors. \
55Wrapper functions, however, only returns the value and panics if an error is encountered. 61Wrapper functions, however, only returns the value and panics if an error is encountered.
56 62
57## Bug Report / Feature Request 63## Bug Report / Feature Request
diff --git a/bitreader.go b/bitreader.go
index b9ac772..5c7dc04 100644
--- a/bitreader.go
+++ b/bitreader.go
@@ -152,6 +152,29 @@ func (reader *ReaderType) TryReadStringLen(length int) string {
152 return text 152 return text
153} 153}
154 154
155// TryReadBytesToSlice is a wrapper function that reads the specified amount of bits
156// from the parameter and puts each bit into a slice and returns this slice.
157func (reader *ReaderType) TryReadBitsToSlice(bits int) []byte {
158 bytes := (bits / 8) + (bits % 8)
159 out := make([]byte, bytes)
160 for i := 0; i < bytes; i++ {
161 if bits/8 > 0 {
162 val, err := reader.ReadBytes(1)
163 if err != nil {
164 panic(err)
165 }
166 out[i] = byte(val)
167 } else { // Not enough to fill a whole byte
168 val, err := reader.ReadBits(bits % 8)
169 if err != nil {
170 panic(err)
171 }
172 out[i] = byte(val)
173 }
174 }
175 return out
176}
177
155// TryReadBytesToSlice is a wrapper function that reads the specified amount of bytes 178// TryReadBytesToSlice is a wrapper function that reads the specified amount of bytes
156// from the parameter and puts each byte into a slice and returns this slice. 179// from the parameter and puts each byte into a slice and returns this slice.
157func (reader *ReaderType) TryReadBytesToSlice(bytes int) []byte { 180func (reader *ReaderType) TryReadBytesToSlice(bytes int) []byte {
@@ -289,6 +312,31 @@ func (reader *ReaderType) ReadBytes(bytes int) (uint64, error) {
289 return value, nil 312 return value, nil
290} 313}
291 314
315// ReadBitsToSlice is a function that reads the specified amount of bits
316// from the parameter and puts each bit into a slice and returns this slice.
317//
318// Returns an error if there are no remaining bits.
319func (reader *ReaderType) ReadBitsToSlice(bits int) ([]byte, error) {
320 bytes := (bits / 8) + (bits % 8)
321 out := make([]byte, bytes)
322 for i := 0; i < bytes; i++ {
323 if bits/8 > 0 {
324 val, err := reader.ReadBytes(1)
325 if err != nil {
326 return out, err
327 }
328 out[i] = byte(val)
329 } else { // Not enough to fill a whole byte
330 val, err := reader.ReadBits(bits % 8)
331 if err != nil {
332 panic(err)
333 }
334 out[i] = byte(val)
335 }
336 }
337 return out, nil
338}
339
292// ReadBytesToSlice is a function that reads the specified amount of bytes 340// ReadBytesToSlice is a function that reads the specified amount of bytes
293// from the parameter and puts each byte into a slice and returns this slice. 341// from the parameter and puts each byte into a slice and returns this slice.
294// 342//