From b0c0e97e53c150fbd763d4cc8f58b28837404ec6 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Thu, 10 Nov 2022 20:54:29 +0300 Subject: added wrapper funcs for read bits/bytes --- README.md | 2 ++ bitreader.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 6cdb38c..1093153 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,8 @@ 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 ``` ## Error Handling diff --git a/bitreader.go b/bitreader.go index 33383e9..b9ac772 100644 --- a/bitreader.go +++ b/bitreader.go @@ -118,6 +118,26 @@ func (reader *ReaderType) TryReadFloat64() float64 { return math.Float64frombits(value) } +// TryReadBits is a wrapper function that returns the value of bits specified in the parameter. +// Returns uint64. Panics on error. +func (reader *ReaderType) TryReadBits(bits int) uint64 { + value, err := reader.ReadBits(bits) + if err != nil { + panic(err) + } + return value +} + +// TryReadBytes is a wrapper function that returns the value of bits specified in the parameter. +// Returns uint64. Panics on error. +func (reader *ReaderType) TryReadBytes(bytes int) uint64 { + value, err := reader.ReadBytes(bytes) + if err != nil { + panic(err) + } + return value +} + // TryReadString is a wrapper function that returns the string // that is read until it is null-terminated. func (reader *ReaderType) TryReadString() string { -- cgit v1.2.3