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

#1500: Add example for bits::complete::tag #1501

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
43 changes: 43 additions & 0 deletions src/bits/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,49 @@
}

/// Generates a parser taking `count` bits and comparing them to `pattern`
///
/// # Example
/// ```rust
///
/// use nom::error::{Error, ErrorKind};
///
/// /// Compare the lowest `count` bits of `input` against the lowest `count` bits of `pattern`.
/// /// Return Ok and the matching section of `input` if there's a match.
/// /// Return Err if there's no match.
/// fn parser(pattern: u8, count: u8, input: (&[u8], usize)) -> nom::IResult<(&[u8], usize), u8> {
/// nom::bits::complete::tag(pattern, count)(input)
/// }
///
/// // The lowest 4 bits of 0b00001111 match the lowest 4 bits of 0b11111111.
/// assert_eq!(
/// parser(0b0000_1111, 4, ([0b1111_1111].as_ref(), 0)),
/// Ok((([0b1111_1111]..as_ref()(), 4), 0b0000_1111))
/// );

Check failure on line 100 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

cannot find function `as_ref` in this scope

Check failure on line 100 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

mismatched types

Check failure on line 100 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

cannot find function `as_ref` in this scope

Check failure on line 100 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

mismatched types

Check failure on line 100 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

cannot find function `as_ref` in this scope

Check failure on line 100 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

mismatched types

Check failure on line 100 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

cannot find function `as_ref` in this scope

Check failure on line 100 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

mismatched types
///
/// // The lowest bit of 0b00001111 matches the lowest bit of 0b11111111 (both are 1).
/// assert_eq!(
/// parser(0b00000001, 1, ([0b11111111].as_ref(), 0)),
/// Ok((([0b11111111]..as_ref()(), 1), 0b00000001))
/// );

Check failure on line 106 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

cannot find function `as_ref` in this scope

Check failure on line 106 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

mismatched types

Check failure on line 106 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

cannot find function `as_ref` in this scope

Check failure on line 106 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

mismatched types

Check failure on line 106 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

cannot find function `as_ref` in this scope

Check failure on line 106 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

mismatched types

Check failure on line 106 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

cannot find function `as_ref` in this scope

Check failure on line 106 in src/bits/complete.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

mismatched types
///
/// // The lowest 2 bits of 0b11111111 and 0b00000001 are different.
/// assert_eq!(
/// parser(0b000000_01, 2, ([0b111111_11].as_ref(), 0)),
/// Err(nom::Err::Error(Error {
/// input: ([0b11111111].as_ref(), 0),
/// code: ErrorKind::TagBits
/// }))
/// );
///
/// // The lowest 8 bits of 0b11111111 and 0b11111110 are different.
/// assert_eq!(
/// parser(0b11111110, 8, ([0b11111111].as_ref(), 0)),
/// Err(nom::Err::Error(Error {
/// input: ([0b11111111].as_ref(), 0),
/// code: ErrorKind::TagBits
/// }))
/// );
/// ```
pub fn tag<I, O, C, E: ParseError<(I, usize)>>(
pattern: O,
count: C,
Expand Down
Loading