From a344d1fa82e1f76c71a71bfaee7c81cbbe1e1d02 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Fri, 15 Sep 2023 21:09:47 +0300 Subject: revamped bitreader; with new functionality and bug fixes --- README.md | 76 +++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 34 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index f88c216..6264cd4 100644 --- a/README.md +++ b/README.md @@ -1,64 +1,72 @@ -# BitReader [![Go Report Card](https://goreportcard.com/badge/github.com/pektezol/bitreader)](https://goreportcard.com/report/github.com/pektezol/bitreader) [![License: LGPL 2.1](https://img.shields.io/badge/License-LGPL_v2.1-blue.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) -A simple bit reader with big/little-endian support for golang.\ -Reads stream data from an io.Reader; can read from os.File and a byte array with bytes.NewReader(array).\ -Uses bitwise operations.\ -Support reading up to 64 bits at one time.\ -Includes wrapper functions for most used data types.\ -Error checking on all but wrapper functions. +# BitReader [![Go Reference](https://pkg.go.dev/badge/github.com/pektezol/bitreader.svg)](https://pkg.go.dev/github.com/pektezol/bitreader) [![Go Report Card](https://goreportcard.com/badge/github.com/pektezol/bitreader)](https://goreportcard.com/report/github.com/pektezol/bitreader) [![License: LGPL 2.1](https://img.shields.io/badge/License-LGPL_v2.1-blue.svg)](https://github.com/pektezol/bitreader/blob/main/LICENSE) +A simple bit reader with big/little-endian support for golang. ## Installation ```bash $ go get github.com/pektezol/bitreader ``` -## Usage +## Usage Examples ```go import "github.com/pektezol/bitreader" -// data: io.Reader Data to read from an io stream -// le: bool Little-endian(true) or big-endian(false) state -reader := bitreader.Reader(data, le) +// ioStream: io.Reader Data to read from an io stream +// byteStream: []byte Data to read from a byte slice +// littleEndian: bool Little-endian(true) or big-endian(false) state +reader := bitreader.NewReader(ioStream, le) +reader := bitreader.NewReaderFromBytes(byteStream, le) + +// Fork Reader, Copies Current Reader +newReader, err := reader.Fork() + +// Read Total Number of Bits Left +bits, err := reader.ReadRemainingBits() // Read First Bit state, err := reader.ReadBool() -// Skip Bits/Bytes -err := reader.SkipBits(8) -err := reader.SkipBytes(4) - // Read Bits/Bytes -value, err := reader.ReadBytes(4) // up to 8 bytes value, err := reader.ReadBits(64) // up to 64 bits +value, err := reader.ReadBytes(8) // up to 8 bytes // Read String -text, err := reader.ReadString() // null-terminated -text, err := reader.ReadStringLen(256) // length-specified +text, err := reader.ReadString() // null-terminated +text, err := reader.ReadStringLength(256) // length-specified // Read Bits/Bytes into Slice arr, err := reader.ReadBitsToSlice(128) arr, err := reader.ReadBytesToSlice(64) +// Skip Bits/Bytes +err := reader.SkipBits(8) +err := reader.SkipBytes(4) + // Wrapper functions -text := reader.TryReadString() // string -text := reader.TryReadStringLen(64) // string -arr := reader.ReadBitsToSlice(128) // []byte -arr := reader.ReadBytesToSlice(64) // []byte -state := reader.TryReadBool() // bool -value := reader.TryReadInt1() // uint8 -value := reader.TryReadInt8() // uint8 -value := reader.TryReadInt16() // uint16 -value := reader.TryReadInt32() // uint32 -value := reader.TryReadInt64() // uint64 -value := reader.TryReadFloat32() // float32 -value := reader.TryReadFloat64() // float64 -value := reader.TryReadBits(64) // uint64 -value := reader.TryReadBytes(8) // uint64 +state := reader.TryReadBool() // bool +value := reader.TryReadInt1() // uint8 +value := reader.TryReadUInt8() // uint8 +value := reader.TryReadSInt8() // int8 +value := reader.TryReadUInt16() // uint16 +value := reader.TryReadSInt16() // int16 +value := reader.TryReadUInt32() // uint32 +value := reader.TryReadSInt32() // int32 +value := reader.TryReadUInt64() // uint64 +value := reader.TryReadSInt64() // int64 +value := reader.TryReadFloat32() // float32 +value := reader.TryReadFloat64() // float64 +value := reader.TryReadBits(64) // uint64 +value := reader.TryReadBytes(8) // uint64 +text := reader.TryReadString() // string +text := reader.TryReadStringLength(64) // string +arr := reader.TryReadBitsToSlice(1024) // []byte +arr := reader.TryReadBytesToSlice(128) // []byte +bits := reader.TryReadRemainingBits() // uint64 ``` ## Error Handling -ReadBits(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. \ -Wrapper functions, however, only returns the value and panics if an error is encountered. +All ReadXXX(), SkipXXX() and Fork() functions returns an error message when they don't work as expected. It is advised to always handle errors. \ +Wrapper functions, however, only returns the value and panics if an error is encountered, for the sake of ease of use. ## Bug Report / Feature Request Using [Github Issues](https://github.com/pektezol/BitReader/issues/new/choose), you can report a bug that you encountered and/or request a feature that you would like to be added. -- cgit v1.2.3