Skip to content

Add parse functions for large integer types. #11

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

Merged
merged 4 commits into from
Sep 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,54 @@ impl<Ref: AsRef<[u8]>> Parser<Ref> {
self.parse_buf(&mut res)?;
Ok(u32::from_be_bytes(res))
}

/// Takes an `i64` from the beginning of the parser.
///
/// The value is converted from network byte order into the system’s own
/// byte order if necessary. The parser is advanced by eight octets. If
/// there aren’t enough octets left, leaves the parser untouched and
/// returns an error instead.
pub fn parse_i64(&mut self) -> Result<i64, ShortInput> {
let mut res = [0; 8];
self.parse_buf(&mut res)?;
Ok(i64::from_be_bytes(res))
}

/// Takes a `u64` from the beginning of the parser.
///
/// The value is converted from network byte order into the system’s own
/// byte order if necessary. The parser is advanced by four octets. If
/// there aren’t enough octets left, leaves the parser untouched and
/// returns an error instead.
pub fn parse_u64(&mut self) -> Result<u64, ShortInput> {
let mut res = [0; 8];
self.parse_buf(&mut res)?;
Ok(u64::from_be_bytes(res))
}

/// Takes a `i128` from the beginning of the parser.
///
/// The value is converted from network byte order into the system’s own
/// byte order if necessary. The parser is advanced by sixteen octets. If
/// there aren’t enough octets left, leaves the parser untouched and
/// returns an error instead.
pub fn parse_i128(&mut self) -> Result<i128, ShortInput> {
let mut res = [0; 16];
self.parse_buf(&mut res)?;
Ok(i128::from_be_bytes(res))
}

/// Takes a `u128` from the beginning of the parser.
///
/// The value is converted from network byte order into the system’s own
/// byte order if necessary. The parser is advanced by sixteen octets. If
/// there aren’t enough octets left, leaves the parser untouched and
/// returns an error instead.
pub fn parse_u128(&mut self) -> Result<u128, ShortInput> {
let mut res = [0; 16];
self.parse_buf(&mut res)?;
Ok(u128::from_be_bytes(res))
}
}


Expand Down