From 2c6eea0886192a1bc07f564ef26c664b0953b73d Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Mon, 7 Nov 2022 12:11:06 +0300 Subject: add ReadString() function --- README.md | 13 +++++++++---- bitreader.go | 37 +++++++++++++++++++++++++++++++++---- 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 @@ # 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) A simple bit reader with big/little-endian support for golang.\ -Reads data from an existing byte array.\ -Uses string manipulation (for now).\ +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.\ -Checking for overflowing the data. +Includes wrapper functions for most used data types.\ +Error checking on all but wrapper functions.\ ## Installation ```bash @@ -30,7 +31,11 @@ err := reader.SkipBytes(4) value, err := reader.ReadBytes(4) value, err := reader.ReadBits(64) // up to 64 bits +// Read String +value, err := reader.ReadString() // null-terminated + // Wrapper functions +text := reader.ReadString() // string state := reader.TryReadBool() // bool value := reader.TryReadInt1() // uint8 value := reader.TryReadInt8() // uint8 @@ -42,7 +47,7 @@ value := reader.TryReadFloat64() // float64 ``` ## Error Handling -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. \ +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. \ Wrapper functions, however, only returns the value and panics if an error is encountered. ## 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 { return math.Float64frombits(value) } +// TryReadString is a wrapper function that returns the string +// that is read until it is null-terminated. +func (reader *ReaderType) TryReadString() string { + text, _ := reader.ReadString() + return text +} + // SkipBits is a function that increases Reader index -// based on given input bits number. Returns an error -// if there are no remaining bits. +// based on given input bits number. +// +// Returns an error if there are no remaining bits. func (reader *ReaderType) SkipBits(bits int) error { // Read as many raw bytes as we can bytes := bits / 8 @@ -144,8 +152,9 @@ func (reader *ReaderType) SkipBits(bits int) error { } // SkipBytes is a function that increases Reader index -// based on given input bytes number. Returns an error -// if there are no remaining bits. +// based on given input bytes number. +// +// Returns an error if there are no remaining bits. func (reader *ReaderType) SkipBytes(bytes int) error { err := reader.SkipBits(bytes * 8) if err != nil { @@ -154,6 +163,26 @@ func (reader *ReaderType) SkipBytes(bytes int) error { return nil } +// ReadString is a function that reads every byte +// until it is null-terminated (the byte is 0). Returns the +// string that is read until the null-termination. +// +// Returns an error if there are no remaining bits. +func (reader *ReaderType) ReadString() (string, error) { + var out string + for { + value, err := reader.ReadBytes(1) + if err != nil { + return out, err + } + if value == 0 { + break + } + out += string(rune(value)) + } + return out, nil +} + // ReadBits is a function that reads the specified amount of bits // specified in the parameter and returns the value, error // based on the output. It can read up to 64 bits. Returns the read -- cgit v1.2.3