Skip to content

Commit 6860641

Browse files
w1ll-i-codeWilliam Calliari
and
William Calliari
authored
1533 implement bool function for bits (#1534)
* Implement function `bool` for `nom::bits::complete` * Implement function `bool` for `nom::bits::streaming` Co-authored-by: William Calliari <[email protected]>
1 parent 6e45c5d commit 6860641

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/bits/complete.rs

+47
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ where
105105
}
106106
}
107107

108+
/// Parses one specific bit as a bool.
109+
///
110+
/// # Example
111+
/// ```rust
112+
/// # use nom::bits::complete::bool;
113+
/// # use nom::IResult;
114+
/// # use nom::error::{Error, ErrorKind};
115+
///
116+
/// fn parse(input: (&[u8], usize)) -> IResult<(&[u8], usize), bool> {
117+
/// bool(input)
118+
/// }
119+
///
120+
/// assert_eq!(parse(([0b10000000].as_ref(), 0)), Ok((([0b10000000].as_ref(), 1), true)));
121+
/// assert_eq!(parse(([0b10000000].as_ref(), 1)), Ok((([0b10000000].as_ref(), 2), false)));
122+
/// ```
123+
pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
124+
where
125+
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
126+
{
127+
let (res, bit): (_, u32) = take(1usize)(input)?;
128+
Ok((res, bit != 0))
129+
}
130+
108131
#[cfg(test)]
109132
mod test {
110133
use super::*;
@@ -147,4 +170,28 @@ mod test {
147170
Ok((([0b11111111].as_ref(), 4), 0b1000110100111111111111))
148171
);
149172
}
173+
174+
#[test]
175+
fn test_bool_0() {
176+
let input = [0b10000000].as_ref();
177+
178+
let result: crate::IResult<(&[u8], usize), bool> = bool((input, 0));
179+
180+
assert_eq!(result, Ok(((input, 1), true)));
181+
}
182+
183+
#[test]
184+
fn test_bool_eof() {
185+
let input = [0b10000000].as_ref();
186+
187+
let result: crate::IResult<(&[u8], usize), bool> = bool((input, 8));
188+
189+
assert_eq!(
190+
result,
191+
Err(crate::Err::Error(crate::error::Error {
192+
input: (input, 8),
193+
code: ErrorKind::Eof
194+
}))
195+
);
196+
}
150197
}

src/bits/streaming.rs

+44
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,29 @@ where
7979
}
8080
}
8181

82+
/// Parses one specific bit as a bool.
83+
///
84+
/// # Example
85+
/// ```rust
86+
/// # use nom::bits::complete::bool;
87+
/// # use nom::IResult;
88+
/// # use nom::error::{Error, ErrorKind};
89+
///
90+
/// fn parse(input: (&[u8], usize)) -> IResult<(&[u8], usize), bool> {
91+
/// bool(input)
92+
/// }
93+
///
94+
/// assert_eq!(parse(([0b10000000].as_ref(), 0)), Ok((([0b10000000].as_ref(), 1), true)));
95+
/// assert_eq!(parse(([0b10000000].as_ref(), 1)), Ok((([0b10000000].as_ref(), 2), false)));
96+
/// ```
97+
pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
98+
where
99+
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
100+
{
101+
let (res, bit): (_, u32) = take(1usize)(input)?;
102+
Ok((res, bit != 0))
103+
}
104+
82105
#[cfg(test)]
83106
mod test {
84107
use super::*;
@@ -126,4 +149,25 @@ mod test {
126149
}))
127150
);
128151
}
152+
153+
#[test]
154+
fn test_bool_0() {
155+
let input = [0b10000000].as_ref();
156+
157+
let result: crate::IResult<(&[u8], usize), bool> = bool((input, 0));
158+
159+
assert_eq!(result, Ok(((input, 1), true)));
160+
}
161+
162+
#[test]
163+
fn test_bool_eof() {
164+
let input = [0b10000000].as_ref();
165+
166+
let result: crate::IResult<(&[u8], usize), bool> = bool((input, 8));
167+
168+
assert_eq!(
169+
result,
170+
Err(crate::Err::Incomplete(Needed::new(1)))
171+
);
172+
}
129173
}

0 commit comments

Comments
 (0)