Ecosyste.ms: Packages

An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

Top 1.9% on proxy.golang.org
Top 0.5% dependent packages on proxy.golang.org
Top 0.4% dependent repos on proxy.golang.org
Top 4.4% forks on proxy.golang.org
Top 0.8% docker downloads on proxy.golang.org

proxy.golang.org : github.com/icza/bitio

Package bitio provides an optimized bit-level Reader and Writer. You can use Reader.ReadBits() to read arbitrary number of bits from an io.Reader and return it as an uint64, and Writer.WriteBits() to write arbitrary number of bits of an uint64 value to an io.Writer. Both Reader and Writer also provide optimized methods for reading / writing 1 bit of information in the form of a bool value: Reader.ReadBool() and Writer.WriteBool(). These make this package ideal for compression algorithms that use Huffman coding for example, where decision whether to step left or right in the Huffman tree is the most frequent operation. Reader and Writer give a bit-level view of the underlying io.Reader and io.Writer, but they also provide a byte-level view (io.Reader and io.Writer) at the same time. This means you can also use the Reader.Read() and Writer.Write() methods to read and write slices of bytes. These will give you best performance if the underlying io.Reader and io.Writer are aligned to a byte boundary (else all the individual bytes are assembled from / spread to multiple bytes). You can ensure byte boundary alignment by calling the Align() method of Reader and Writer. As an extra, io.ByteReader and io.ByteWriter are also implemented. The more general highest-bits-first order is used. So for example if the input provides the bytes 0x8f and 0x55: Then ReadBits will return the following values: Writing the above values would result in the same sequence of bytes: All ReadXXX() and WriteXXX() methods return an error which you are expected to handle. For convenience, there are also matching TryReadXXX() and TryWriteXXX() methods which do not return an error. Instead they store the (first) error in the Reader.TryError / Writer.TryError field which you can inspect later. These TryXXX() methods are a no-op if a TryError has been encountered before, so it's safe to call multiple TryXXX() methods and defer the error checking. For example: This allows you to easily convert the result of individual ReadBits(), like this: And similarly: For performance reasons, Reader and Writer do not keep track of the number of read or written bits. If you happen to need the total number of processed bits, you may use the CountReader and CountWriter types which have identical API to that of Reader and Writer, but they also maintain the number of processed bits which you can query using the BitsCount field.

Registry - Source - Documentation - JSON
purl: pkg:golang/github.com/icza/bitio
Keywords: bit, reader, writer
License: Apache-2.0
Latest release: over 2 years ago
First release: over 6 years ago
Namespace: github.com/icza
Dependent packages: 99
Dependent repositories: 230
Stars: 207 on GitHub
Forks: 26 on GitHub
Docker dependents: 10
Docker downloads: 302,272
See more repository details: repos.ecosyste.ms
Funding links: https://github.com/sponsors/icza
Last synced: 7 days ago

    Loading...
    Readme
    Loading...