diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2022-11-07 15:24:20 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2022-11-07 15:24:20 +0300 |
| commit | 7986c19178a4d217ecff436ccaf79a0ff11adf88 (patch) | |
| tree | 80f7dc77c2a3accb52a278069ac20c54997bacc6 /bitreader.go | |
| parent | readme typo (diff) | |
| download | bitreader-7986c19178a4d217ecff436ccaf79a0ff11adf88.tar.gz bitreader-7986c19178a4d217ecff436ccaf79a0ff11adf88.tar.bz2 bitreader-7986c19178a4d217ecff436ccaf79a0ff11adf88.zip | |
add ReadStringLen(length int) func
Diffstat (limited to '')
| -rw-r--r-- | bitreader.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/bitreader.go b/bitreader.go index d3cc71f..d42b8e6 100644 --- a/bitreader.go +++ b/bitreader.go | |||
| @@ -125,6 +125,13 @@ func (reader *ReaderType) TryReadString() string { | |||
| 125 | return text | 125 | return text |
| 126 | } | 126 | } |
| 127 | 127 | ||
| 128 | // TryReadStringLen is a wrapper function that returns the string | ||
| 129 | // that is read until the given length is reached or it is null-terminated. | ||
| 130 | func (reader *ReaderType) TryReadStringLen(length int) string { | ||
| 131 | text, _ := reader.ReadStringLen(length) | ||
| 132 | return text | ||
| 133 | } | ||
| 134 | |||
| 128 | // SkipBits is a function that increases Reader index | 135 | // SkipBits is a function that increases Reader index |
| 129 | // based on given input bits number. | 136 | // based on given input bits number. |
| 130 | // | 137 | // |
| @@ -183,6 +190,28 @@ func (reader *ReaderType) ReadString() (string, error) { | |||
| 183 | return out, nil | 190 | return out, nil |
| 184 | } | 191 | } |
| 185 | 192 | ||
| 193 | // ReadStringLen is a function that reads every byte | ||
| 194 | // until the given length, or it is null-terminated (the byte is 0). | ||
| 195 | // Returns the string that is read until the lenth or null-termination. | ||
| 196 | // It will skip the remaining bytes if it is null-terminated. | ||
| 197 | // | ||
| 198 | // Returns an error if there are no remaining bits. | ||
| 199 | func (reader *ReaderType) ReadStringLen(length int) (string, error) { | ||
| 200 | var out string | ||
| 201 | for i := 0; i < length; i++ { | ||
| 202 | value, err := reader.ReadBytes(1) | ||
| 203 | if err != nil { | ||
| 204 | return out, err | ||
| 205 | } | ||
| 206 | if value == 0 { | ||
| 207 | reader.SkipBytes(length - 1 - i) | ||
| 208 | break | ||
| 209 | } | ||
| 210 | out += string(rune(value)) | ||
| 211 | } | ||
| 212 | return out, nil | ||
| 213 | } | ||
| 214 | |||
| 186 | // ReadBits is a function that reads the specified amount of bits | 215 | // ReadBits is a function that reads the specified amount of bits |
| 187 | // specified in the parameter and returns the value, error | 216 | // specified in the parameter and returns the value, error |
| 188 | // based on the output. It can read up to 64 bits. Returns the read | 217 | // based on the output. It can read up to 64 bits. Returns the read |