Skip to content

Commit 23418e7

Browse files
committed
Fixes rust-bakery#768: Generalize bits.rs to handle inputs other than &[u8]
1 parent 64af2d1 commit 23418e7

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/bits.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ macro_rules! bits_impl (
4747
{
4848
use $crate::lib::std::result::Result::*;
4949
use $crate::{Context,Err,Needed};
50+
use $crate::Slice;
5051

5152
let input = ($i, 0usize);
5253
match $submac!(input, $($args)*) {
@@ -76,7 +77,7 @@ macro_rules! bits_impl (
7677
Ok(((i, bit_index), o)) => {
7778
let byte_index = bit_index / 8 + if bit_index % 8 == 0 { 0 } else { 1 } ;
7879
//println!("bit index=={} => byte index=={}", bit_index, byte_index);
79-
Ok((&i[byte_index..], o))
80+
Ok((i.slice(byte_index..), o))
8081
}
8182
}
8283
}
@@ -92,6 +93,7 @@ macro_rules! bits_impl (
9293
{
9394
use $crate::lib::std::result::Result::*;
9495
use $crate::{Err,Needed,Context};
96+
use $crate::Slice;
9597

9698
let input = ($i, 0usize);
9799
match $submac!(input, $($args)*) {
@@ -111,7 +113,7 @@ macro_rules! bits_impl (
111113
Ok(((i, bit_index), o)) => {
112114
let byte_index = bit_index / 8 + if bit_index % 8 == 0 { 0 } else { 1 } ;
113115
//println!("bit index=={} => byte index=={}", bit_index, byte_index);
114-
Ok((&i[byte_index..], o))
116+
Ok((i.slice(byte_index..), o))
115117
}
116118
}
117119
}
@@ -278,9 +280,10 @@ macro_rules! take_bits (
278280

279281
use $crate::lib::std::ops::Div;
280282
use $crate::lib::std::convert::Into;
283+
use $crate::Slice;
281284
//println!("taking {} bits from {:?}", $count, $i);
282285
let (input, bit_offset) = $i;
283-
let res : IResult<(&[u8],usize), $t> = if $count == 0 {
286+
let res : IResult<_, $t> = if $count == 0 {
284287
Ok(( (input, bit_offset), (0 as u8).into()))
285288
} else {
286289
let cnt = ($count as usize + bit_offset).div(8);
@@ -313,7 +316,7 @@ macro_rules! take_bits (
313316
offset = 0;
314317
}
315318
}
316-
Ok(( (&input[cnt..], end_offset) , acc))
319+
Ok(( (input.slice(cnt..), end_offset) , acc))
317320
}
318321
};
319322
res
@@ -351,7 +354,7 @@ macro_rules! tag_bits (
351354
Err(Err::Incomplete(i)) => Err(Err::Incomplete(i)),
352355
Ok((i, o)) => {
353356
if let $p = o {
354-
let res: IResult<(&[u8],usize),$t> = Ok((i, o));
357+
let res: IResult<_,$t> = Ok((i, o));
355358
res
356359
} else {
357360
let e: $crate::ErrorKind<u32> = $crate::ErrorKind::TagBits;
@@ -372,6 +375,7 @@ mod tests {
372375
use lib::std::ops::{AddAssign, Shl, Shr};
373376
use internal::{Err, Needed};
374377
use util::ErrorKind;
378+
use types::CompleteByteSlice;
375379

376380
#[test]
377381
fn take_bits() {
@@ -392,6 +396,7 @@ mod tests {
392396
assert_eq!(take_bits!((sl, 6), u16, 11), Ok(((&sl[2..], 1), 1504)));
393397
assert_eq!(take_bits!((sl, 0), u32, 20), Ok(((&sl[2..], 4), 700_163)));
394398
assert_eq!(take_bits!((sl, 4), u32, 20), Ok(((&sl[3..], 0), 716_851)));
399+
assert_eq!(take_bits!((CompleteByteSlice(sl), 4), u32, 20), Ok(((sl[3..].into(), 0), 716_851)));
395400
assert_eq!(
396401
take_bits!((sl, 4), u32, 22),
397402
Err(Err::Incomplete(Needed::Size(22)))
@@ -405,6 +410,7 @@ mod tests {
405410

406411
assert_eq!(tag_bits!((sl, 0), u8, 3, 0b101), Ok(((&sl[0..], 3), 5)));
407412
assert_eq!(tag_bits!((sl, 0), u8, 4, 0b1010), Ok(((&sl[0..], 4), 10)));
413+
assert_eq!(tag_bits!((CompleteByteSlice(sl), 0), u8, 4, 0b1010), Ok(((sl[0..].into(), 4), 10)));
408414
}
409415

410416
named!(ch<(&[u8],usize),(u8,u8)>,

tests/issues.rs

+26
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,29 @@ fn issue_759() {
272272
named_args!(issue_771(count: usize)<Vec<u32>>,
273273
length_count!(value!(count), call!(nom::be_u32))
274274
);
275+
276+
#[test]
277+
fn issue_768() {
278+
named!(bit_vec8<CompleteByteSlice, Vec<u16>>, bits!(many0!(take_bits!(u16, 8))));
279+
named!(bit_vec4<CompleteByteSlice, Vec<u16>>, bits!(many0!(take_bits!(u16, 4))));
280+
named!(bit_vec3<CompleteByteSlice, Vec<u16>>, bits!(many0!(take_bits!(u16, 3))));
281+
named!(bit_vec11<CompleteByteSlice, Vec<u16>>, bits!(many0!(take_bits!(u16, 11))));
282+
283+
let m: Vec<u8> = vec![70, 97, 106, 121, 86, 66, 105, 98, 86, 106, 101];
284+
assert_eq!(
285+
bit_vec8(CompleteByteSlice(m.as_slice())),
286+
Ok((CompleteByteSlice(&[]), vec![70, 97, 106, 121, 86, 66, 105, 98, 86, 106, 101]))
287+
);
288+
assert_eq!(
289+
bit_vec4(CompleteByteSlice(m.as_slice())),
290+
Ok((CompleteByteSlice(&[]), vec![4, 6, 6, 1, 6, 10, 7, 9, 5, 6, 4, 2, 6, 9, 6, 2, 5, 6, 6, 10, 6, 5]))
291+
);
292+
assert_eq!(
293+
bit_vec3(CompleteByteSlice(m.as_slice())),
294+
Ok((CompleteByteSlice(&[]), vec![2, 1, 4, 6, 0, 5, 5, 2, 3, 6, 2, 5, 3, 1, 0, 2, 3, 2, 2, 6, 1, 1, 2, 6, 3, 2, 4, 6, 2]))
295+
);
296+
assert_eq!(
297+
bit_vec11(CompleteByteSlice(m.as_slice())),
298+
Ok((CompleteByteSlice(&[]), vec![563, 90, 1266, 1380, 308, 1417, 717, 613]))
299+
);
300+
}

0 commit comments

Comments
 (0)