aboutsummaryrefslogtreecommitdiff
path: root/bitreader.go
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2022-11-07 12:11:06 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2022-11-07 12:11:06 +0300
commit2c6eea0886192a1bc07f564ef26c664b0953b73d (patch)
tree19df9ac262cd83fe864595ccf93a464bbb63873b /bitreader.go
parentDelete .github/ISSUE_TEMPLATE directory (diff)
downloadbitreader-2c6eea0886192a1bc07f564ef26c664b0953b73d.tar.gz
bitreader-2c6eea0886192a1bc07f564ef26c664b0953b73d.tar.bz2
bitreader-2c6eea0886192a1bc07f564ef26c664b0953b73d.zip
add ReadString() function
Diffstat (limited to '')
-rw-r--r--bitreader.go37
1 files changed, 33 insertions, 4 deletions
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 {
118 return math.Float64frombits(value) 118 return math.Float64frombits(value)
119} 119}
120 120
121// TryReadString is a wrapper function that returns the string
122// that is read until it is null-terminated.
123func (reader *ReaderType) TryReadString() string {
124 text, _ := reader.ReadString()
125 return text
126}
127
121// SkipBits is a function that increases Reader index 128// SkipBits is a function that increases Reader index
122// based on given input bits number. Returns an error 129// based on given input bits number.
123// if there are no remaining bits. 130//
131// Returns an error if there are no remaining bits.
124func (reader *ReaderType) SkipBits(bits int) error { 132func (reader *ReaderType) SkipBits(bits int) error {
125 // Read as many raw bytes as we can 133 // Read as many raw bytes as we can
126 bytes := bits / 8 134 bytes := bits / 8
@@ -144,8 +152,9 @@ func (reader *ReaderType) SkipBits(bits int) error {
144} 152}
145 153
146// SkipBytes is a function that increases Reader index 154// SkipBytes is a function that increases Reader index
147// based on given input bytes number. Returns an error 155// based on given input bytes number.
148// if there are no remaining bits. 156//
157// Returns an error if there are no remaining bits.
149func (reader *ReaderType) SkipBytes(bytes int) error { 158func (reader *ReaderType) SkipBytes(bytes int) error {
150 err := reader.SkipBits(bytes * 8) 159 err := reader.SkipBits(bytes * 8)
151 if err != nil { 160 if err != nil {
@@ -154,6 +163,26 @@ func (reader *ReaderType) SkipBytes(bytes int) error {
154 return nil 163 return nil
155} 164}
156 165
166// ReadString is a function that reads every byte
167// until it is null-terminated (the byte is 0). Returns the
168// string that is read until the null-termination.
169//
170// Returns an error if there are no remaining bits.
171func (reader *ReaderType) ReadString() (string, error) {
172 var out string
173 for {
174 value, err := reader.ReadBytes(1)
175 if err != nil {
176 return out, err
177 }
178 if value == 0 {
179 break
180 }
181 out += string(rune(value))
182 }
183 return out, nil
184}
185
157// ReadBits is a function that reads the specified amount of bits 186// ReadBits is a function that reads the specified amount of bits
158// specified in the parameter and returns the value, error 187// specified in the parameter and returns the value, error
159// based on the output. It can read up to 64 bits. Returns the read 188// based on the output. It can read up to 64 bits. Returns the read