Giter Club home page Giter Club logo

bitreader's Introduction

BitReader

BitReader is a helper type to extract strings of bits from a slice of bytes.

Published Package Documentation Build Status

Here is how you read first a single bit, then three bits and finally four bits from a byte buffer:

use bitreader::BitReader;

let slice_of_u8 = &[0b1000_1111];
let mut reader = BitReader::new(slice_of_u8);

// You obviously should use try! or some other error handling mechanism here
let a_single_bit = reader.read_u8(1).unwrap(); // 1
let more_bits = reader.read_u8(3).unwrap(); // 0
let last_bits_of_byte = reader.read_u8(4).unwrap(); // 0b1111

You can naturally read bits from longer buffer of data than just a single byte.

As you read bits, the internal cursor of BitReader moves on along the stream of bits. Big endian format is assumed when reading the multi-byte values. BitReader supports reading maximum of 64 bits at a time (with read_u64).

License

Licensed under the Apache License, Version 2.0 or the MIT license, at your option.

bitreader's People

Contributors

antoyo avatar bramp avatar dancerj avatar irauta avatar kornelski avatar rillian avatar tp-m avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bitreader's Issues

Implement ReadInto for bool?

In mozilla/mp4parse-rust#55 we do a lot of

let v: u8 = ReadInto::read(reader, 1);
if v > 0 {
    true_thing();
} else
    false_thing();
}

I think this could be improved if there was an impl ReadInto for bool so one could:

if ReadInto::read(reader, 1) {
  true_thing();
};

Rust analyzer text completion fails when unwrapping u8 bit

    result.rq = reader.read_u8(1)
        .unwrap()
        . // here is where the completion fails

This is the error Rust analyzer is throwing:

[Error - 12:22:37 PM] Request textDocument/completion failed.
  Message: request handler panicked: index out of bounds: the len is 1 but the index is 73
  Code: -32603 

Feature Request: relative_reader with max length?

When parsing a stream, it is common to have length prefixed data. I would love to be able to call relative_reader with this length, and pass the result to another function responsible for parsing the data. Having the length encoded in the bitreader would greatly simplify bound checking.

Thoughts?

Little endian support

One of the main pieces still missing from this crate is support for little endian values. I made a first sketch of what this could look like, but I'm open to any critique (and obviously it would require testing which is completely lacking as of now and probably some performance improvements):

https://github.com/leoschwarz/opensim-networking/blob/20858c6e0c8c0f5de3b0f05778b3e1513d377538/src/layer_data/reader.rs

The main problem I had with the API wasn't the addition of a (generic) parameter specifying endianness to the methods, but also the fact that values can be padded from both the left and right with zeros, before applying the byte reordering. (i.e. say you read 9 bits 1 0010 and want that as a u16 it could either be 0001 0010 or 1001 0000, and when you swap the bytes this makes a difference) That's why I ended up taking two generic arguments for the read_part_u32 method. Obviously the read_full_* and read_part_* methods could be combined but then every call would involve supplying two generic arguments which is really verbose. One alternative is taking only one generic argument, which implements both traits, and then to define types implementing both traits.

One alternative to the calling read_u8 all the time would be to read into the target type directly, skip the byteorder crate, and have our own logic to call swap_bytes on the values if needed. (This would have to check the requested ordering and probably the host ordering too.)

Allow reading N bytes into a &mut [u8] at once

Hi, this crate seems to implement most of what I need right now, except there doesn't seem to be an easy way to read many bytes at once.

I wanted to propose implementing a read method like here, however I'm not sure if you'd like the reader to implement std::io::Read or not. The benefit of implementing Read is that I could use it directly with other parts of my code where I sometimes also read from non BitReader Read implementors (but if there was any method I could also just implement Read using a new type).

I'm going to implement in my own fork first anyway, but I wanted to hear if there is any objection to such functionality to be added here?

Crash with wrong parameter (read 0 bits)

The following code crashes (default debug target) with thread 'main' panicked at 'attempt to subtract with overflow', /Users/hub/.cargo/registry/src/github.com-1ecc6299db9ec823/bitreader-0.3.5/src/lib.rs:320:36

extern crate bitreader;

use bitreader::BitReader;

fn main() {
    let buf = vec![10, 20, 30, 40];

    let mut bits = BitReader::new(&buf);

    let zero = bits.read_i32(0).expect("can't read bits");

    println!("zero {}", zero);
}

I think it should return an error instead.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.