Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support varint #1567

Open
ikrivosheev opened this issue Oct 28, 2022 · 2 comments · May be fixed by #1568
Open

Add support varint #1567

ikrivosheev opened this issue Oct 28, 2022 · 2 comments · May be fixed by #1568

Comments

@ikrivosheev
Copy link

Hello! Thank you for the great library! I I want to add support for varint.

Link: https://en.wikipedia.org/wiki/Variable-length_quantity

@ikrivosheev ikrivosheev linked a pull request Oct 28, 2022 that will close this issue
@hackaugusto
Copy link

It would be nice to have both variants for little and big endian encodings, and maybe also zig-zag encoding.

@hackaugusto
Copy link

If there is any interest, this is the big-ending version w/ overflow handling that I'm using:

#[inline]
pub fn be_varint_u64<I, E: ParseError<I>>(mut i: I) -> IResult<I, u64, E>
where
    I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
{
    let mut result = 0;

    'end: {
        let mut byte;
        for _ in 0..(u64::BITS / 8) {
            (i, byte) = be_u8(i)?;
            result = (result << 7) + u64::from(byte & 0x7F);
            if byte < 0x80 {
                break 'end;
            }
        }
        (i, byte) = be_u8(i)?;
        result = (result << 8) + u64::from(byte);
    }

    Ok((i, result))
}

Because of the BITS associated constants this can't be a generic function, but should be easy enough to write a macro for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants