1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#[macro_use]
mod macros;
pub mod streaming;
pub mod complete;
use crate::error::{ParseError, ErrorKind};
use crate::internal::{Err, IResult, Needed};
use crate::lib::std::ops::RangeFrom;
use crate::traits::{Slice, ErrorConvert};
pub fn bits<I, O, E1: ParseError<(I, usize)>+ErrorConvert<E2>, E2: ParseError<I>, P>(parser: P) -> impl Fn(I) -> IResult<I, O, E2>
where
I: Slice<RangeFrom<usize>>,
P: Fn((I, usize)) -> IResult<(I, usize), O, E1>,
{
move |input: I| match parser((input, 0)) {
Ok(((rest, offset), res)) => {
let byte_index = offset / 8 + if offset % 8 == 0 { 0 } else { 1 };
Ok((rest.slice(byte_index..), res))
}
Err(Err::Incomplete(n)) => Err(Err::Incomplete(n.map(|u| u / 8 + 1))),
Err(Err::Error(e)) => Err(Err::Error(e.convert())),
Err(Err::Failure(e)) => Err(Err::Failure(e.convert())),
}
}
#[doc(hidden)]
pub fn bitsc<I, O, E1: ParseError<(I, usize)>+ErrorConvert<E2>, E2: ParseError<I>, P>(input: I, parser: P) -> IResult<I, O, E2>
where
I: Slice<RangeFrom<usize>>,
P: Fn((I, usize)) -> IResult<(I, usize), O, E1>,
{
bits(parser)(input)
}
pub fn bytes<I, O, E1: ParseError<I>+ErrorConvert<E2>, E2: ParseError<(I, usize)>, P>(parser: P) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E2>
where
I: Slice<RangeFrom<usize>> + Clone,
P: Fn(I) -> IResult<I, O, E1>,
{
move |(input, offset): (I, usize)| {
let inner = if offset % 8 != 0 {
input.slice((1 + offset / 8)..)
} else {
input.slice((offset / 8)..)
};
let i = (input.clone(), offset);
match parser(inner) {
Ok((rest, res)) => Ok(((rest, 0), res)),
Err(Err::Incomplete(Needed::Unknown)) => Err(Err::Incomplete(Needed::Unknown)),
Err(Err::Incomplete(Needed::Size(sz))) => Err(match sz.checked_mul(8) {
Some(v) => Err::Incomplete(Needed::Size(v)),
None => Err::Failure(E2::from_error_kind(i, ErrorKind::TooLarge)),
}),
Err(Err::Error(e)) => Err(Err::Error(e.convert())),
Err(Err::Failure(e)) => Err(Err::Failure(e.convert())),
}
}
}
#[doc(hidden)]
pub fn bytesc<I, O, E1: ParseError<I>+ErrorConvert<E2>, E2: ParseError<(I, usize)>, P>(input: (I, usize), parser: P) -> IResult<(I, usize), O, E2>
where
I: Slice<RangeFrom<usize>> + Clone,
P: Fn(I) -> IResult<I, O, E1>,
{
bytes(parser)(input)
}