/rust/registry/src/index.crates.io-6f17d22bba15001f/nom-7.1.3/src/traits.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Traits input types have to implement to work with nom combinators |
2 | | use crate::error::{ErrorKind, ParseError}; |
3 | | use crate::internal::{Err, IResult, Needed}; |
4 | | use crate::lib::std::iter::{Copied, Enumerate}; |
5 | | use crate::lib::std::ops::{Range, RangeFrom, RangeFull, RangeTo}; |
6 | | use crate::lib::std::slice::Iter; |
7 | | use crate::lib::std::str::from_utf8; |
8 | | use crate::lib::std::str::CharIndices; |
9 | | use crate::lib::std::str::Chars; |
10 | | use crate::lib::std::str::FromStr; |
11 | | |
12 | | #[cfg(feature = "alloc")] |
13 | | use crate::lib::std::string::String; |
14 | | #[cfg(feature = "alloc")] |
15 | | use crate::lib::std::vec::Vec; |
16 | | |
17 | | /// Abstract method to calculate the input length |
18 | | pub trait InputLength { |
19 | | /// Calculates the input length, as indicated by its name, |
20 | | /// and the name of the trait itself |
21 | | fn input_len(&self) -> usize; |
22 | | } |
23 | | |
24 | | impl<'a, T> InputLength for &'a [T] { |
25 | | #[inline] |
26 | 0 | fn input_len(&self) -> usize { |
27 | 0 | self.len() |
28 | 0 | } Unexecuted instantiation: <&[u8] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[_] as nom::traits::InputLength>::input_len |
29 | | } |
30 | | |
31 | | impl<'a> InputLength for &'a str { |
32 | | #[inline] |
33 | 0 | fn input_len(&self) -> usize { |
34 | 0 | self.len() |
35 | 0 | } |
36 | | } |
37 | | |
38 | | impl<'a> InputLength for (&'a [u8], usize) { |
39 | | #[inline] |
40 | 0 | fn input_len(&self) -> usize { |
41 | 0 | //println!("bit input length for ({:?}, {}):", self.0, self.1); |
42 | 0 | //println!("-> {}", self.0.len() * 8 - self.1); |
43 | 0 | self.0.len() * 8 - self.1 |
44 | 0 | } |
45 | | } |
46 | | |
47 | | /// Useful functions to calculate the offset between slices and show a hexdump of a slice |
48 | | pub trait Offset { |
49 | | /// Offset between the first byte of self and the first byte of the argument |
50 | | fn offset(&self, second: &Self) -> usize; |
51 | | } |
52 | | |
53 | | impl Offset for [u8] { |
54 | 0 | fn offset(&self, second: &Self) -> usize { |
55 | 0 | let fst = self.as_ptr(); |
56 | 0 | let snd = second.as_ptr(); |
57 | 0 |
|
58 | 0 | snd as usize - fst as usize |
59 | 0 | } |
60 | | } |
61 | | |
62 | | impl<'a> Offset for &'a [u8] { |
63 | 0 | fn offset(&self, second: &Self) -> usize { |
64 | 0 | let fst = self.as_ptr(); |
65 | 0 | let snd = second.as_ptr(); |
66 | 0 |
|
67 | 0 | snd as usize - fst as usize |
68 | 0 | } |
69 | | } |
70 | | |
71 | | impl Offset for str { |
72 | 0 | fn offset(&self, second: &Self) -> usize { |
73 | 0 | let fst = self.as_ptr(); |
74 | 0 | let snd = second.as_ptr(); |
75 | 0 |
|
76 | 0 | snd as usize - fst as usize |
77 | 0 | } |
78 | | } |
79 | | |
80 | | impl<'a> Offset for &'a str { |
81 | 0 | fn offset(&self, second: &Self) -> usize { |
82 | 0 | let fst = self.as_ptr(); |
83 | 0 | let snd = second.as_ptr(); |
84 | 0 |
|
85 | 0 | snd as usize - fst as usize |
86 | 0 | } |
87 | | } |
88 | | |
89 | | /// Helper trait for types that can be viewed as a byte slice |
90 | | pub trait AsBytes { |
91 | | /// Casts the input type to a byte slice |
92 | | fn as_bytes(&self) -> &[u8]; |
93 | | } |
94 | | |
95 | | impl<'a> AsBytes for &'a str { |
96 | | #[inline(always)] |
97 | 0 | fn as_bytes(&self) -> &[u8] { |
98 | 0 | (*self).as_bytes() |
99 | 0 | } |
100 | | } |
101 | | |
102 | | impl AsBytes for str { |
103 | | #[inline(always)] |
104 | 0 | fn as_bytes(&self) -> &[u8] { |
105 | 0 | self.as_ref() |
106 | 0 | } |
107 | | } |
108 | | |
109 | | impl<'a> AsBytes for &'a [u8] { |
110 | | #[inline(always)] |
111 | 0 | fn as_bytes(&self) -> &[u8] { |
112 | 0 | *self |
113 | 0 | } |
114 | | } |
115 | | |
116 | | impl AsBytes for [u8] { |
117 | | #[inline(always)] |
118 | 0 | fn as_bytes(&self) -> &[u8] { |
119 | 0 | self |
120 | 0 | } |
121 | | } |
122 | | |
123 | | macro_rules! as_bytes_array_impls { |
124 | | ($($N:expr)+) => { |
125 | | $( |
126 | | impl<'a> AsBytes for &'a [u8; $N] { |
127 | | #[inline(always)] |
128 | 0 | fn as_bytes(&self) -> &[u8] { |
129 | 0 | *self |
130 | 0 | } Unexecuted instantiation: <&[u8; 0] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 1] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 2] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 3] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 4] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 5] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 6] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 7] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 8] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 9] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 10] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 11] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 12] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 13] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 14] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 15] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 16] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 17] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 18] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 19] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 20] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 21] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 22] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 23] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 24] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 25] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 26] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 27] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 28] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 29] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 30] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 31] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <&[u8; 32] as nom::traits::AsBytes>::as_bytes |
131 | | } |
132 | | |
133 | | impl AsBytes for [u8; $N] { |
134 | | #[inline(always)] |
135 | 0 | fn as_bytes(&self) -> &[u8] { |
136 | 0 | self |
137 | 0 | } Unexecuted instantiation: <[u8; 0] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 1] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 2] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 3] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 4] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 5] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 6] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 7] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 8] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 9] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 10] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 11] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 12] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 13] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 14] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 15] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 16] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 17] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 18] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 19] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 20] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 21] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 22] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 23] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 24] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 25] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 26] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 27] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 28] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 29] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 30] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 31] as nom::traits::AsBytes>::as_bytes Unexecuted instantiation: <[u8; 32] as nom::traits::AsBytes>::as_bytes |
138 | | } |
139 | | )+ |
140 | | }; |
141 | | } |
142 | | |
143 | | as_bytes_array_impls! { |
144 | | 0 1 2 3 4 5 6 7 8 9 |
145 | | 10 11 12 13 14 15 16 17 18 19 |
146 | | 20 21 22 23 24 25 26 27 28 29 |
147 | | 30 31 32 |
148 | | } |
149 | | |
150 | | /// Transforms common types to a char for basic token parsing |
151 | | pub trait AsChar { |
152 | | /// makes a char from self |
153 | | fn as_char(self) -> char; |
154 | | |
155 | | /// Tests that self is an alphabetic character |
156 | | /// |
157 | | /// Warning: for `&str` it recognizes alphabetic |
158 | | /// characters outside of the 52 ASCII letters |
159 | | fn is_alpha(self) -> bool; |
160 | | |
161 | | /// Tests that self is an alphabetic character |
162 | | /// or a decimal digit |
163 | | fn is_alphanum(self) -> bool; |
164 | | /// Tests that self is a decimal digit |
165 | | fn is_dec_digit(self) -> bool; |
166 | | /// Tests that self is an hex digit |
167 | | fn is_hex_digit(self) -> bool; |
168 | | /// Tests that self is an octal digit |
169 | | fn is_oct_digit(self) -> bool; |
170 | | /// Gets the len in bytes for self |
171 | | fn len(self) -> usize; |
172 | | } |
173 | | |
174 | | impl AsChar for u8 { |
175 | | #[inline] |
176 | 0 | fn as_char(self) -> char { |
177 | 0 | self as char |
178 | 0 | } |
179 | | #[inline] |
180 | 0 | fn is_alpha(self) -> bool { |
181 | 0 | (self >= 0x41 && self <= 0x5A) || (self >= 0x61 && self <= 0x7A) |
182 | 0 | } |
183 | | #[inline] |
184 | 0 | fn is_alphanum(self) -> bool { |
185 | 0 | self.is_alpha() || self.is_dec_digit() |
186 | 0 | } |
187 | | #[inline] |
188 | 0 | fn is_dec_digit(self) -> bool { |
189 | 0 | self >= 0x30 && self <= 0x39 |
190 | 0 | } |
191 | | #[inline] |
192 | 0 | fn is_hex_digit(self) -> bool { |
193 | 0 | (self >= 0x30 && self <= 0x39) |
194 | 0 | || (self >= 0x41 && self <= 0x46) |
195 | 0 | || (self >= 0x61 && self <= 0x66) |
196 | 0 | } |
197 | | #[inline] |
198 | 0 | fn is_oct_digit(self) -> bool { |
199 | 0 | self >= 0x30 && self <= 0x37 |
200 | 0 | } |
201 | | #[inline] |
202 | 0 | fn len(self) -> usize { |
203 | 0 | 1 |
204 | 0 | } |
205 | | } |
206 | | impl<'a> AsChar for &'a u8 { |
207 | | #[inline] |
208 | 0 | fn as_char(self) -> char { |
209 | 0 | *self as char |
210 | 0 | } |
211 | | #[inline] |
212 | 0 | fn is_alpha(self) -> bool { |
213 | 0 | (*self >= 0x41 && *self <= 0x5A) || (*self >= 0x61 && *self <= 0x7A) |
214 | 0 | } |
215 | | #[inline] |
216 | 0 | fn is_alphanum(self) -> bool { |
217 | 0 | self.is_alpha() || self.is_dec_digit() |
218 | 0 | } |
219 | | #[inline] |
220 | 0 | fn is_dec_digit(self) -> bool { |
221 | 0 | *self >= 0x30 && *self <= 0x39 |
222 | 0 | } |
223 | | #[inline] |
224 | 0 | fn is_hex_digit(self) -> bool { |
225 | 0 | (*self >= 0x30 && *self <= 0x39) |
226 | 0 | || (*self >= 0x41 && *self <= 0x46) |
227 | 0 | || (*self >= 0x61 && *self <= 0x66) |
228 | 0 | } |
229 | | #[inline] |
230 | 0 | fn is_oct_digit(self) -> bool { |
231 | 0 | *self >= 0x30 && *self <= 0x37 |
232 | 0 | } |
233 | | #[inline] |
234 | 0 | fn len(self) -> usize { |
235 | 0 | 1 |
236 | 0 | } |
237 | | } |
238 | | |
239 | | impl AsChar for char { |
240 | | #[inline] |
241 | 0 | fn as_char(self) -> char { |
242 | 0 | self |
243 | 0 | } |
244 | | #[inline] |
245 | 0 | fn is_alpha(self) -> bool { |
246 | 0 | self.is_ascii_alphabetic() |
247 | 0 | } |
248 | | #[inline] |
249 | 0 | fn is_alphanum(self) -> bool { |
250 | 0 | self.is_alpha() || self.is_dec_digit() |
251 | 0 | } |
252 | | #[inline] |
253 | 0 | fn is_dec_digit(self) -> bool { |
254 | 0 | self.is_ascii_digit() |
255 | 0 | } |
256 | | #[inline] |
257 | 0 | fn is_hex_digit(self) -> bool { |
258 | 0 | self.is_ascii_hexdigit() |
259 | 0 | } |
260 | | #[inline] |
261 | 0 | fn is_oct_digit(self) -> bool { |
262 | 0 | self.is_digit(8) |
263 | 0 | } |
264 | | #[inline] |
265 | 0 | fn len(self) -> usize { |
266 | 0 | self.len_utf8() |
267 | 0 | } |
268 | | } |
269 | | |
270 | | impl<'a> AsChar for &'a char { |
271 | | #[inline] |
272 | 0 | fn as_char(self) -> char { |
273 | 0 | *self |
274 | 0 | } |
275 | | #[inline] |
276 | 0 | fn is_alpha(self) -> bool { |
277 | 0 | self.is_ascii_alphabetic() |
278 | 0 | } |
279 | | #[inline] |
280 | 0 | fn is_alphanum(self) -> bool { |
281 | 0 | self.is_alpha() || self.is_dec_digit() |
282 | 0 | } |
283 | | #[inline] |
284 | 0 | fn is_dec_digit(self) -> bool { |
285 | 0 | self.is_ascii_digit() |
286 | 0 | } |
287 | | #[inline] |
288 | 0 | fn is_hex_digit(self) -> bool { |
289 | 0 | self.is_ascii_hexdigit() |
290 | 0 | } |
291 | | #[inline] |
292 | 0 | fn is_oct_digit(self) -> bool { |
293 | 0 | self.is_digit(8) |
294 | 0 | } |
295 | | #[inline] |
296 | 0 | fn len(self) -> usize { |
297 | 0 | self.len_utf8() |
298 | 0 | } |
299 | | } |
300 | | |
301 | | /// Abstracts common iteration operations on the input type |
302 | | pub trait InputIter { |
303 | | /// The current input type is a sequence of that `Item` type. |
304 | | /// |
305 | | /// Example: `u8` for `&[u8]` or `char` for `&str` |
306 | | type Item; |
307 | | /// An iterator over the input type, producing the item and its position |
308 | | /// for use with [Slice]. If we're iterating over `&str`, the position |
309 | | /// corresponds to the byte index of the character |
310 | | type Iter: Iterator<Item = (usize, Self::Item)>; |
311 | | |
312 | | /// An iterator over the input type, producing the item |
313 | | type IterElem: Iterator<Item = Self::Item>; |
314 | | |
315 | | /// Returns an iterator over the elements and their byte offsets |
316 | | fn iter_indices(&self) -> Self::Iter; |
317 | | /// Returns an iterator over the elements |
318 | | fn iter_elements(&self) -> Self::IterElem; |
319 | | /// Finds the byte position of the element |
320 | | fn position<P>(&self, predicate: P) -> Option<usize> |
321 | | where |
322 | | P: Fn(Self::Item) -> bool; |
323 | | /// Get the byte offset from the element's position in the stream |
324 | | fn slice_index(&self, count: usize) -> Result<usize, Needed>; |
325 | | } |
326 | | |
327 | | /// Abstracts slicing operations |
328 | | pub trait InputTake: Sized { |
329 | | /// Returns a slice of `count` bytes. panics if count > length |
330 | | fn take(&self, count: usize) -> Self; |
331 | | /// Split the stream at the `count` byte offset. panics if count > length |
332 | | fn take_split(&self, count: usize) -> (Self, Self); |
333 | | } |
334 | | |
335 | | impl<'a> InputIter for &'a [u8] { |
336 | | type Item = u8; |
337 | | type Iter = Enumerate<Self::IterElem>; |
338 | | type IterElem = Copied<Iter<'a, u8>>; |
339 | | |
340 | | #[inline] |
341 | 0 | fn iter_indices(&self) -> Self::Iter { |
342 | 0 | self.iter_elements().enumerate() |
343 | 0 | } |
344 | | #[inline] |
345 | 0 | fn iter_elements(&self) -> Self::IterElem { |
346 | 0 | self.iter().copied() |
347 | 0 | } Unexecuted instantiation: <&[u8] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8] as nom::traits::InputIter>::iter_elements |
348 | | #[inline] |
349 | 0 | fn position<P>(&self, predicate: P) -> Option<usize> |
350 | 0 | where |
351 | 0 | P: Fn(Self::Item) -> bool, |
352 | 0 | { |
353 | 0 | self.iter().position(|b| predicate(*b)) |
354 | 0 | } |
355 | | #[inline] |
356 | 0 | fn slice_index(&self, count: usize) -> Result<usize, Needed> { |
357 | 0 | if self.len() >= count { |
358 | 0 | Ok(count) |
359 | | } else { |
360 | 0 | Err(Needed::new(count - self.len())) |
361 | | } |
362 | 0 | } Unexecuted instantiation: <&[u8] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8] as nom::traits::InputIter>::slice_index |
363 | | } |
364 | | |
365 | | impl<'a> InputTake for &'a [u8] { |
366 | | #[inline] |
367 | 0 | fn take(&self, count: usize) -> Self { |
368 | 0 | &self[0..count] |
369 | 0 | } |
370 | | #[inline] |
371 | 0 | fn take_split(&self, count: usize) -> (Self, Self) { |
372 | 0 | let (prefix, suffix) = self.split_at(count); |
373 | 0 | (suffix, prefix) |
374 | 0 | } Unexecuted instantiation: <&[u8] as nom::traits::InputTake>::take_split Unexecuted instantiation: <&[u8] as nom::traits::InputTake>::take_split Unexecuted instantiation: <&[u8] as nom::traits::InputTake>::take_split |
375 | | } |
376 | | |
377 | | impl<'a> InputIter for &'a str { |
378 | | type Item = char; |
379 | | type Iter = CharIndices<'a>; |
380 | | type IterElem = Chars<'a>; |
381 | | #[inline] |
382 | 0 | fn iter_indices(&self) -> Self::Iter { |
383 | 0 | self.char_indices() |
384 | 0 | } |
385 | | #[inline] |
386 | 0 | fn iter_elements(&self) -> Self::IterElem { |
387 | 0 | self.chars() |
388 | 0 | } |
389 | 0 | fn position<P>(&self, predicate: P) -> Option<usize> |
390 | 0 | where |
391 | 0 | P: Fn(Self::Item) -> bool, |
392 | 0 | { |
393 | 0 | for (o, c) in self.char_indices() { |
394 | 0 | if predicate(c) { |
395 | 0 | return Some(o); |
396 | 0 | } |
397 | | } |
398 | 0 | None |
399 | 0 | } |
400 | | #[inline] |
401 | 0 | fn slice_index(&self, count: usize) -> Result<usize, Needed> { |
402 | 0 | let mut cnt = 0; |
403 | 0 | for (index, _) in self.char_indices() { |
404 | 0 | if cnt == count { |
405 | 0 | return Ok(index); |
406 | 0 | } |
407 | 0 | cnt += 1; |
408 | | } |
409 | 0 | if cnt == count { |
410 | 0 | return Ok(self.len()); |
411 | 0 | } |
412 | 0 | Err(Needed::Unknown) |
413 | 0 | } |
414 | | } |
415 | | |
416 | | impl<'a> InputTake for &'a str { |
417 | | #[inline] |
418 | 0 | fn take(&self, count: usize) -> Self { |
419 | 0 | &self[..count] |
420 | 0 | } |
421 | | |
422 | | // return byte index |
423 | | #[inline] |
424 | 0 | fn take_split(&self, count: usize) -> (Self, Self) { |
425 | 0 | let (prefix, suffix) = self.split_at(count); |
426 | 0 | (suffix, prefix) |
427 | 0 | } |
428 | | } |
429 | | |
430 | | /// Dummy trait used for default implementations (currently only used for `InputTakeAtPosition` and `Compare`). |
431 | | /// |
432 | | /// When implementing a custom input type, it is possible to use directly the |
433 | | /// default implementation: If the input type implements `InputLength`, `InputIter`, |
434 | | /// `InputTake` and `Clone`, you can implement `UnspecializedInput` and get |
435 | | /// a default version of `InputTakeAtPosition` and `Compare`. |
436 | | /// |
437 | | /// For performance reasons, you might want to write a custom implementation of |
438 | | /// `InputTakeAtPosition` (like the one for `&[u8]`). |
439 | | pub trait UnspecializedInput {} |
440 | | |
441 | | /// Methods to take as much input as possible until the provided function returns true for the current element. |
442 | | /// |
443 | | /// A large part of nom's basic parsers are built using this trait. |
444 | | pub trait InputTakeAtPosition: Sized { |
445 | | /// The current input type is a sequence of that `Item` type. |
446 | | /// |
447 | | /// Example: `u8` for `&[u8]` or `char` for `&str` |
448 | | type Item; |
449 | | |
450 | | /// Looks for the first element of the input type for which the condition returns true, |
451 | | /// and returns the input up to this position. |
452 | | /// |
453 | | /// *streaming version*: If no element is found matching the condition, this will return `Incomplete` |
454 | | fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> |
455 | | where |
456 | | P: Fn(Self::Item) -> bool; |
457 | | |
458 | | /// Looks for the first element of the input type for which the condition returns true |
459 | | /// and returns the input up to this position. |
460 | | /// |
461 | | /// Fails if the produced slice is empty. |
462 | | /// |
463 | | /// *streaming version*: If no element is found matching the condition, this will return `Incomplete` |
464 | | fn split_at_position1<P, E: ParseError<Self>>( |
465 | | &self, |
466 | | predicate: P, |
467 | | e: ErrorKind, |
468 | | ) -> IResult<Self, Self, E> |
469 | | where |
470 | | P: Fn(Self::Item) -> bool; |
471 | | |
472 | | /// Looks for the first element of the input type for which the condition returns true, |
473 | | /// and returns the input up to this position. |
474 | | /// |
475 | | /// *complete version*: If no element is found matching the condition, this will return the whole input |
476 | | fn split_at_position_complete<P, E: ParseError<Self>>( |
477 | | &self, |
478 | | predicate: P, |
479 | | ) -> IResult<Self, Self, E> |
480 | | where |
481 | | P: Fn(Self::Item) -> bool; |
482 | | |
483 | | /// Looks for the first element of the input type for which the condition returns true |
484 | | /// and returns the input up to this position. |
485 | | /// |
486 | | /// Fails if the produced slice is empty. |
487 | | /// |
488 | | /// *complete version*: If no element is found matching the condition, this will return the whole input |
489 | | fn split_at_position1_complete<P, E: ParseError<Self>>( |
490 | | &self, |
491 | | predicate: P, |
492 | | e: ErrorKind, |
493 | | ) -> IResult<Self, Self, E> |
494 | | where |
495 | | P: Fn(Self::Item) -> bool; |
496 | | } |
497 | | |
498 | | impl<T: InputLength + InputIter + InputTake + Clone + UnspecializedInput> InputTakeAtPosition |
499 | | for T |
500 | | { |
501 | | type Item = <T as InputIter>::Item; |
502 | | |
503 | 0 | fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> |
504 | 0 | where |
505 | 0 | P: Fn(Self::Item) -> bool, |
506 | 0 | { |
507 | 0 | match self.position(predicate) { |
508 | 0 | Some(n) => Ok(self.take_split(n)), |
509 | 0 | None => Err(Err::Incomplete(Needed::new(1))), |
510 | | } |
511 | 0 | } |
512 | | |
513 | 0 | fn split_at_position1<P, E: ParseError<Self>>( |
514 | 0 | &self, |
515 | 0 | predicate: P, |
516 | 0 | e: ErrorKind, |
517 | 0 | ) -> IResult<Self, Self, E> |
518 | 0 | where |
519 | 0 | P: Fn(Self::Item) -> bool, |
520 | 0 | { |
521 | 0 | match self.position(predicate) { |
522 | 0 | Some(0) => Err(Err::Error(E::from_error_kind(self.clone(), e))), |
523 | 0 | Some(n) => Ok(self.take_split(n)), |
524 | 0 | None => Err(Err::Incomplete(Needed::new(1))), |
525 | | } |
526 | 0 | } |
527 | | |
528 | 0 | fn split_at_position_complete<P, E: ParseError<Self>>( |
529 | 0 | &self, |
530 | 0 | predicate: P, |
531 | 0 | ) -> IResult<Self, Self, E> |
532 | 0 | where |
533 | 0 | P: Fn(Self::Item) -> bool, |
534 | 0 | { |
535 | 0 | match self.split_at_position(predicate) { |
536 | 0 | Err(Err::Incomplete(_)) => Ok(self.take_split(self.input_len())), |
537 | 0 | res => res, |
538 | | } |
539 | 0 | } |
540 | | |
541 | 0 | fn split_at_position1_complete<P, E: ParseError<Self>>( |
542 | 0 | &self, |
543 | 0 | predicate: P, |
544 | 0 | e: ErrorKind, |
545 | 0 | ) -> IResult<Self, Self, E> |
546 | 0 | where |
547 | 0 | P: Fn(Self::Item) -> bool, |
548 | 0 | { |
549 | 0 | match self.split_at_position1(predicate, e) { |
550 | | Err(Err::Incomplete(_)) => { |
551 | 0 | if self.input_len() == 0 { |
552 | 0 | Err(Err::Error(E::from_error_kind(self.clone(), e))) |
553 | | } else { |
554 | 0 | Ok(self.take_split(self.input_len())) |
555 | | } |
556 | | } |
557 | 0 | res => res, |
558 | | } |
559 | 0 | } |
560 | | } |
561 | | |
562 | | impl<'a> InputTakeAtPosition for &'a [u8] { |
563 | | type Item = u8; |
564 | | |
565 | 0 | fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> |
566 | 0 | where |
567 | 0 | P: Fn(Self::Item) -> bool, |
568 | 0 | { |
569 | 0 | match self.iter().position(|c| predicate(*c)) { |
570 | 0 | Some(i) => Ok(self.take_split(i)), |
571 | 0 | None => Err(Err::Incomplete(Needed::new(1))), |
572 | | } |
573 | 0 | } |
574 | | |
575 | 0 | fn split_at_position1<P, E: ParseError<Self>>( |
576 | 0 | &self, |
577 | 0 | predicate: P, |
578 | 0 | e: ErrorKind, |
579 | 0 | ) -> IResult<Self, Self, E> |
580 | 0 | where |
581 | 0 | P: Fn(Self::Item) -> bool, |
582 | 0 | { |
583 | 0 | match self.iter().position(|c| predicate(*c)) { |
584 | 0 | Some(0) => Err(Err::Error(E::from_error_kind(self, e))), |
585 | 0 | Some(i) => Ok(self.take_split(i)), |
586 | 0 | None => Err(Err::Incomplete(Needed::new(1))), |
587 | | } |
588 | 0 | } |
589 | | |
590 | 0 | fn split_at_position_complete<P, E: ParseError<Self>>( |
591 | 0 | &self, |
592 | 0 | predicate: P, |
593 | 0 | ) -> IResult<Self, Self, E> |
594 | 0 | where |
595 | 0 | P: Fn(Self::Item) -> bool, |
596 | 0 | { |
597 | 0 | match self.iter().position(|c| predicate(*c)) { |
598 | 0 | Some(i) => Ok(self.take_split(i)), |
599 | 0 | None => Ok(self.take_split(self.input_len())), |
600 | | } |
601 | 0 | } |
602 | | |
603 | 0 | fn split_at_position1_complete<P, E: ParseError<Self>>( |
604 | 0 | &self, |
605 | 0 | predicate: P, |
606 | 0 | e: ErrorKind, |
607 | 0 | ) -> IResult<Self, Self, E> |
608 | 0 | where |
609 | 0 | P: Fn(Self::Item) -> bool, |
610 | 0 | { |
611 | 0 | match self.iter().position(|c| predicate(*c)) { |
612 | 0 | Some(0) => Err(Err::Error(E::from_error_kind(self, e))), |
613 | 0 | Some(i) => Ok(self.take_split(i)), |
614 | | None => { |
615 | 0 | if self.is_empty() { |
616 | 0 | Err(Err::Error(E::from_error_kind(self, e))) |
617 | | } else { |
618 | 0 | Ok(self.take_split(self.input_len())) |
619 | | } |
620 | | } |
621 | | } |
622 | 0 | } |
623 | | } |
624 | | |
625 | | impl<'a> InputTakeAtPosition for &'a str { |
626 | | type Item = char; |
627 | | |
628 | 0 | fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E> |
629 | 0 | where |
630 | 0 | P: Fn(Self::Item) -> bool, |
631 | 0 | { |
632 | 0 | match self.find(predicate) { |
633 | | // find() returns a byte index that is already in the slice at a char boundary |
634 | 0 | Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) }, |
635 | 0 | None => Err(Err::Incomplete(Needed::new(1))), |
636 | | } |
637 | 0 | } |
638 | | |
639 | 0 | fn split_at_position1<P, E: ParseError<Self>>( |
640 | 0 | &self, |
641 | 0 | predicate: P, |
642 | 0 | e: ErrorKind, |
643 | 0 | ) -> IResult<Self, Self, E> |
644 | 0 | where |
645 | 0 | P: Fn(Self::Item) -> bool, |
646 | 0 | { |
647 | 0 | match self.find(predicate) { |
648 | 0 | Some(0) => Err(Err::Error(E::from_error_kind(self, e))), |
649 | | // find() returns a byte index that is already in the slice at a char boundary |
650 | 0 | Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) }, |
651 | 0 | None => Err(Err::Incomplete(Needed::new(1))), |
652 | | } |
653 | 0 | } |
654 | | |
655 | 0 | fn split_at_position_complete<P, E: ParseError<Self>>( |
656 | 0 | &self, |
657 | 0 | predicate: P, |
658 | 0 | ) -> IResult<Self, Self, E> |
659 | 0 | where |
660 | 0 | P: Fn(Self::Item) -> bool, |
661 | 0 | { |
662 | 0 | match self.find(predicate) { |
663 | | // find() returns a byte index that is already in the slice at a char boundary |
664 | 0 | Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) }, |
665 | | // the end of slice is a char boundary |
666 | | None => unsafe { |
667 | 0 | Ok(( |
668 | 0 | self.get_unchecked(self.len()..), |
669 | 0 | self.get_unchecked(..self.len()), |
670 | 0 | )) |
671 | | }, |
672 | | } |
673 | 0 | } |
674 | | |
675 | 0 | fn split_at_position1_complete<P, E: ParseError<Self>>( |
676 | 0 | &self, |
677 | 0 | predicate: P, |
678 | 0 | e: ErrorKind, |
679 | 0 | ) -> IResult<Self, Self, E> |
680 | 0 | where |
681 | 0 | P: Fn(Self::Item) -> bool, |
682 | 0 | { |
683 | 0 | match self.find(predicate) { |
684 | 0 | Some(0) => Err(Err::Error(E::from_error_kind(self, e))), |
685 | | // find() returns a byte index that is already in the slice at a char boundary |
686 | 0 | Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) }, |
687 | | None => { |
688 | 0 | if self.is_empty() { |
689 | 0 | Err(Err::Error(E::from_error_kind(self, e))) |
690 | | } else { |
691 | | // the end of slice is a char boundary |
692 | | unsafe { |
693 | 0 | Ok(( |
694 | 0 | self.get_unchecked(self.len()..), |
695 | 0 | self.get_unchecked(..self.len()), |
696 | 0 | )) |
697 | | } |
698 | | } |
699 | | } |
700 | | } |
701 | 0 | } |
702 | | } |
703 | | |
704 | | /// Indicates whether a comparison was successful, an error, or |
705 | | /// if more data was needed |
706 | | #[derive(Debug, PartialEq)] |
707 | | pub enum CompareResult { |
708 | | /// Comparison was successful |
709 | | Ok, |
710 | | /// We need more data to be sure |
711 | | Incomplete, |
712 | | /// Comparison failed |
713 | | Error, |
714 | | } |
715 | | |
716 | | /// Abstracts comparison operations |
717 | | pub trait Compare<T> { |
718 | | /// Compares self to another value for equality |
719 | | fn compare(&self, t: T) -> CompareResult; |
720 | | /// Compares self to another value for equality |
721 | | /// independently of the case. |
722 | | /// |
723 | | /// Warning: for `&str`, the comparison is done |
724 | | /// by lowercasing both strings and comparing |
725 | | /// the result. This is a temporary solution until |
726 | | /// a better one appears |
727 | | fn compare_no_case(&self, t: T) -> CompareResult; |
728 | | } |
729 | | |
730 | 0 | fn lowercase_byte(c: u8) -> u8 { |
731 | 0 | match c { |
732 | 0 | b'A'..=b'Z' => c - b'A' + b'a', |
733 | 0 | _ => c, |
734 | | } |
735 | 0 | } |
736 | | |
737 | | impl<'a, 'b> Compare<&'b [u8]> for &'a [u8] { |
738 | | #[inline(always)] |
739 | 0 | fn compare(&self, t: &'b [u8]) -> CompareResult { |
740 | 0 | let pos = self.iter().zip(t.iter()).position(|(a, b)| a != b); |
741 | 0 |
|
742 | 0 | match pos { |
743 | 0 | Some(_) => CompareResult::Error, |
744 | | None => { |
745 | 0 | if self.len() >= t.len() { |
746 | 0 | CompareResult::Ok |
747 | | } else { |
748 | 0 | CompareResult::Incomplete |
749 | | } |
750 | | } |
751 | | } |
752 | | |
753 | | /* |
754 | | let len = self.len(); |
755 | | let blen = t.len(); |
756 | | let m = if len < blen { len } else { blen }; |
757 | | let reduced = &self[..m]; |
758 | | let b = &t[..m]; |
759 | | |
760 | | if reduced != b { |
761 | | CompareResult::Error |
762 | | } else if m < blen { |
763 | | CompareResult::Incomplete |
764 | | } else { |
765 | | CompareResult::Ok |
766 | | } |
767 | | */ |
768 | 0 | } |
769 | | |
770 | | #[inline(always)] |
771 | 0 | fn compare_no_case(&self, t: &'b [u8]) -> CompareResult { |
772 | 0 | if self |
773 | 0 | .iter() |
774 | 0 | .zip(t) |
775 | 0 | .any(|(a, b)| lowercase_byte(*a) != lowercase_byte(*b)) |
776 | | { |
777 | 0 | CompareResult::Error |
778 | 0 | } else if self.len() < t.len() { |
779 | 0 | CompareResult::Incomplete |
780 | | } else { |
781 | 0 | CompareResult::Ok |
782 | | } |
783 | 0 | } |
784 | | } |
785 | | |
786 | | impl< |
787 | | T: InputLength + InputIter<Item = u8> + InputTake + UnspecializedInput, |
788 | | O: InputLength + InputIter<Item = u8> + InputTake, |
789 | | > Compare<O> for T |
790 | | { |
791 | | #[inline(always)] |
792 | 0 | fn compare(&self, t: O) -> CompareResult { |
793 | 0 | let pos = self |
794 | 0 | .iter_elements() |
795 | 0 | .zip(t.iter_elements()) |
796 | 0 | .position(|(a, b)| a != b); |
797 | 0 |
|
798 | 0 | match pos { |
799 | 0 | Some(_) => CompareResult::Error, |
800 | | None => { |
801 | 0 | if self.input_len() >= t.input_len() { |
802 | 0 | CompareResult::Ok |
803 | | } else { |
804 | 0 | CompareResult::Incomplete |
805 | | } |
806 | | } |
807 | | } |
808 | 0 | } |
809 | | |
810 | | #[inline(always)] |
811 | 0 | fn compare_no_case(&self, t: O) -> CompareResult { |
812 | 0 | if self |
813 | 0 | .iter_elements() |
814 | 0 | .zip(t.iter_elements()) |
815 | 0 | .any(|(a, b)| lowercase_byte(a) != lowercase_byte(b)) |
816 | | { |
817 | 0 | CompareResult::Error |
818 | 0 | } else if self.input_len() < t.input_len() { |
819 | 0 | CompareResult::Incomplete |
820 | | } else { |
821 | 0 | CompareResult::Ok |
822 | | } |
823 | 0 | } |
824 | | } |
825 | | |
826 | | impl<'a, 'b> Compare<&'b str> for &'a [u8] { |
827 | | #[inline(always)] |
828 | 0 | fn compare(&self, t: &'b str) -> CompareResult { |
829 | 0 | self.compare(AsBytes::as_bytes(t)) |
830 | 0 | } |
831 | | #[inline(always)] |
832 | 0 | fn compare_no_case(&self, t: &'b str) -> CompareResult { |
833 | 0 | self.compare_no_case(AsBytes::as_bytes(t)) |
834 | 0 | } |
835 | | } |
836 | | |
837 | | impl<'a, 'b> Compare<&'b str> for &'a str { |
838 | | #[inline(always)] |
839 | 0 | fn compare(&self, t: &'b str) -> CompareResult { |
840 | 0 | self.as_bytes().compare(t.as_bytes()) |
841 | 0 | } |
842 | | |
843 | | //FIXME: this version is too simple and does not use the current locale |
844 | | #[inline(always)] |
845 | 0 | fn compare_no_case(&self, t: &'b str) -> CompareResult { |
846 | 0 | let pos = self |
847 | 0 | .chars() |
848 | 0 | .zip(t.chars()) |
849 | 0 | .position(|(a, b)| a.to_lowercase().ne(b.to_lowercase())); |
850 | 0 |
|
851 | 0 | match pos { |
852 | 0 | Some(_) => CompareResult::Error, |
853 | | None => { |
854 | 0 | if self.len() >= t.len() { |
855 | 0 | CompareResult::Ok |
856 | | } else { |
857 | 0 | CompareResult::Incomplete |
858 | | } |
859 | | } |
860 | | } |
861 | 0 | } |
862 | | } |
863 | | |
864 | | impl<'a, 'b> Compare<&'b [u8]> for &'a str { |
865 | | #[inline(always)] |
866 | 0 | fn compare(&self, t: &'b [u8]) -> CompareResult { |
867 | 0 | AsBytes::as_bytes(self).compare(t) |
868 | 0 | } |
869 | | #[inline(always)] |
870 | 0 | fn compare_no_case(&self, t: &'b [u8]) -> CompareResult { |
871 | 0 | AsBytes::as_bytes(self).compare_no_case(t) |
872 | 0 | } |
873 | | } |
874 | | |
875 | | /// Look for a token in self |
876 | | pub trait FindToken<T> { |
877 | | /// Returns true if self contains the token |
878 | | fn find_token(&self, token: T) -> bool; |
879 | | } |
880 | | |
881 | | impl<'a> FindToken<u8> for &'a [u8] { |
882 | 0 | fn find_token(&self, token: u8) -> bool { |
883 | 0 | memchr::memchr(token, self).is_some() |
884 | 0 | } |
885 | | } |
886 | | |
887 | | impl<'a> FindToken<u8> for &'a str { |
888 | 0 | fn find_token(&self, token: u8) -> bool { |
889 | 0 | self.as_bytes().find_token(token) |
890 | 0 | } |
891 | | } |
892 | | |
893 | | impl<'a, 'b> FindToken<&'a u8> for &'b [u8] { |
894 | 0 | fn find_token(&self, token: &u8) -> bool { |
895 | 0 | self.find_token(*token) |
896 | 0 | } |
897 | | } |
898 | | |
899 | | impl<'a, 'b> FindToken<&'a u8> for &'b str { |
900 | 0 | fn find_token(&self, token: &u8) -> bool { |
901 | 0 | self.as_bytes().find_token(token) |
902 | 0 | } |
903 | | } |
904 | | |
905 | | impl<'a> FindToken<char> for &'a [u8] { |
906 | 0 | fn find_token(&self, token: char) -> bool { |
907 | 0 | self.iter().any(|i| *i == token as u8) |
908 | 0 | } |
909 | | } |
910 | | |
911 | | impl<'a> FindToken<char> for &'a str { |
912 | 0 | fn find_token(&self, token: char) -> bool { |
913 | 0 | self.chars().any(|i| i == token) |
914 | 0 | } |
915 | | } |
916 | | |
917 | | impl<'a> FindToken<char> for &'a [char] { |
918 | 0 | fn find_token(&self, token: char) -> bool { |
919 | 0 | self.iter().any(|i| *i == token) |
920 | 0 | } |
921 | | } |
922 | | |
923 | | impl<'a, 'b> FindToken<&'a char> for &'b [char] { |
924 | 0 | fn find_token(&self, token: &char) -> bool { |
925 | 0 | self.find_token(*token) |
926 | 0 | } |
927 | | } |
928 | | |
929 | | /// Look for a substring in self |
930 | | pub trait FindSubstring<T> { |
931 | | /// Returns the byte position of the substring if it is found |
932 | | fn find_substring(&self, substr: T) -> Option<usize>; |
933 | | } |
934 | | |
935 | | impl<'a, 'b> FindSubstring<&'b [u8]> for &'a [u8] { |
936 | 0 | fn find_substring(&self, substr: &'b [u8]) -> Option<usize> { |
937 | 0 | if substr.len() > self.len() { |
938 | 0 | return None; |
939 | 0 | } |
940 | | |
941 | 0 | let (&substr_first, substr_rest) = match substr.split_first() { |
942 | 0 | Some(split) => split, |
943 | | // an empty substring is found at position 0 |
944 | | // This matches the behavior of str.find(""). |
945 | 0 | None => return Some(0), |
946 | | }; |
947 | | |
948 | 0 | if substr_rest.is_empty() { |
949 | 0 | return memchr::memchr(substr_first, self); |
950 | 0 | } |
951 | 0 |
|
952 | 0 | let mut offset = 0; |
953 | 0 | let haystack = &self[..self.len() - substr_rest.len()]; |
954 | | |
955 | 0 | while let Some(position) = memchr::memchr(substr_first, &haystack[offset..]) { |
956 | 0 | offset += position; |
957 | 0 | let next_offset = offset + 1; |
958 | 0 | if &self[next_offset..][..substr_rest.len()] == substr_rest { |
959 | 0 | return Some(offset); |
960 | 0 | } |
961 | 0 |
|
962 | 0 | offset = next_offset; |
963 | | } |
964 | | |
965 | 0 | None |
966 | 0 | } |
967 | | } |
968 | | |
969 | | impl<'a, 'b> FindSubstring<&'b str> for &'a [u8] { |
970 | 0 | fn find_substring(&self, substr: &'b str) -> Option<usize> { |
971 | 0 | self.find_substring(AsBytes::as_bytes(substr)) |
972 | 0 | } |
973 | | } |
974 | | |
975 | | impl<'a, 'b> FindSubstring<&'b str> for &'a str { |
976 | | //returns byte index |
977 | 0 | fn find_substring(&self, substr: &'b str) -> Option<usize> { |
978 | 0 | self.find(substr) |
979 | 0 | } |
980 | | } |
981 | | |
982 | | /// Used to integrate `str`'s `parse()` method |
983 | | pub trait ParseTo<R> { |
984 | | /// Succeeds if `parse()` succeeded. The byte slice implementation |
985 | | /// will first convert it to a `&str`, then apply the `parse()` function |
986 | | fn parse_to(&self) -> Option<R>; |
987 | | } |
988 | | |
989 | | impl<'a, R: FromStr> ParseTo<R> for &'a [u8] { |
990 | 0 | fn parse_to(&self) -> Option<R> { |
991 | 0 | from_utf8(self).ok().and_then(|s| s.parse().ok()) |
992 | 0 | } |
993 | | } |
994 | | |
995 | | impl<'a, R: FromStr> ParseTo<R> for &'a str { |
996 | 0 | fn parse_to(&self) -> Option<R> { |
997 | 0 | self.parse().ok() |
998 | 0 | } |
999 | | } |
1000 | | |
1001 | | /// Slicing operations using ranges. |
1002 | | /// |
1003 | | /// This trait is loosely based on |
1004 | | /// `Index`, but can actually return |
1005 | | /// something else than a `&[T]` or `&str` |
1006 | | pub trait Slice<R> { |
1007 | | /// Slices self according to the range argument |
1008 | | fn slice(&self, range: R) -> Self; |
1009 | | } |
1010 | | |
1011 | | macro_rules! impl_fn_slice { |
1012 | | ( $ty:ty ) => { |
1013 | 0 | fn slice(&self, range: $ty) -> Self { |
1014 | 0 | &self[range] |
1015 | 0 | } Unexecuted instantiation: <&[u8] as nom::traits::Slice<core::ops::range::RangeFrom<usize>>>::slice Unexecuted instantiation: <&str as nom::traits::Slice<core::ops::range::Range<usize>>>::slice Unexecuted instantiation: <&str as nom::traits::Slice<core::ops::range::RangeTo<usize>>>::slice Unexecuted instantiation: <&str as nom::traits::Slice<core::ops::range::RangeFrom<usize>>>::slice Unexecuted instantiation: <&str as nom::traits::Slice<core::ops::range::RangeFull>>::slice Unexecuted instantiation: <&[_] as nom::traits::Slice<core::ops::range::Range<usize>>>::slice Unexecuted instantiation: <&[_] as nom::traits::Slice<core::ops::range::RangeTo<usize>>>::slice Unexecuted instantiation: <&[_] as nom::traits::Slice<core::ops::range::RangeFrom<usize>>>::slice Unexecuted instantiation: <&[_] as nom::traits::Slice<core::ops::range::RangeFull>>::slice |
1016 | | }; |
1017 | | } |
1018 | | |
1019 | | macro_rules! slice_range_impl { |
1020 | | ( [ $for_type:ident ], $ty:ty ) => { |
1021 | | impl<'a, $for_type> Slice<$ty> for &'a [$for_type] { |
1022 | | impl_fn_slice!($ty); |
1023 | | } |
1024 | | }; |
1025 | | ( $for_type:ty, $ty:ty ) => { |
1026 | | impl<'a> Slice<$ty> for &'a $for_type { |
1027 | | impl_fn_slice!($ty); |
1028 | | } |
1029 | | }; |
1030 | | } |
1031 | | |
1032 | | macro_rules! slice_ranges_impl { |
1033 | | ( [ $for_type:ident ] ) => { |
1034 | | slice_range_impl! {[$for_type], Range<usize>} |
1035 | | slice_range_impl! {[$for_type], RangeTo<usize>} |
1036 | | slice_range_impl! {[$for_type], RangeFrom<usize>} |
1037 | | slice_range_impl! {[$for_type], RangeFull} |
1038 | | }; |
1039 | | ( $for_type:ty ) => { |
1040 | | slice_range_impl! {$for_type, Range<usize>} |
1041 | | slice_range_impl! {$for_type, RangeTo<usize>} |
1042 | | slice_range_impl! {$for_type, RangeFrom<usize>} |
1043 | | slice_range_impl! {$for_type, RangeFull} |
1044 | | }; |
1045 | | } |
1046 | | |
1047 | | slice_ranges_impl! {str} |
1048 | | slice_ranges_impl! {[T]} |
1049 | | |
1050 | | macro_rules! array_impls { |
1051 | | ($($N:expr)+) => { |
1052 | | $( |
1053 | | impl InputLength for [u8; $N] { |
1054 | | #[inline] |
1055 | 0 | fn input_len(&self) -> usize { |
1056 | 0 | self.len() |
1057 | 0 | } Unexecuted instantiation: <[u8; 0] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 1] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 2] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 3] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 4] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 5] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 6] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 7] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 8] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 9] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 10] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 11] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 12] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 13] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 14] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 15] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 16] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 17] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 18] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 19] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 20] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 21] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 22] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 23] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 24] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 25] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 26] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 27] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 28] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 29] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 30] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 31] as nom::traits::InputLength>::input_len Unexecuted instantiation: <[u8; 32] as nom::traits::InputLength>::input_len |
1058 | | } |
1059 | | |
1060 | | impl<'a> InputLength for &'a [u8; $N] { |
1061 | | #[inline] |
1062 | 0 | fn input_len(&self) -> usize { |
1063 | 0 | self.len() |
1064 | 0 | } Unexecuted instantiation: <&[u8; 0] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 1] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 2] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 3] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 4] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 5] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 6] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 7] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 8] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 9] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 10] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 11] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 12] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 13] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 14] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 15] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 16] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 17] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 18] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 19] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 20] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 21] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 22] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 23] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 24] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 25] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 26] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 27] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 28] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 29] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 30] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 31] as nom::traits::InputLength>::input_len Unexecuted instantiation: <&[u8; 32] as nom::traits::InputLength>::input_len |
1065 | | } |
1066 | | |
1067 | | impl<'a> InputIter for &'a [u8; $N] { |
1068 | | type Item = u8; |
1069 | | type Iter = Enumerate<Self::IterElem>; |
1070 | | type IterElem = Copied<Iter<'a, u8>>; |
1071 | | |
1072 | 0 | fn iter_indices(&self) -> Self::Iter { |
1073 | 0 | (&self[..]).iter_indices() |
1074 | 0 | } Unexecuted instantiation: <&[u8; 0] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 1] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 2] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 3] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 4] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 5] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 6] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 7] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 8] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 9] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 10] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 11] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 12] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 13] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 14] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 15] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 16] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 17] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 18] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 19] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 20] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 21] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 22] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 23] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 24] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 25] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 26] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 27] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 28] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 29] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 30] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 31] as nom::traits::InputIter>::iter_indices Unexecuted instantiation: <&[u8; 32] as nom::traits::InputIter>::iter_indices |
1075 | | |
1076 | 0 | fn iter_elements(&self) -> Self::IterElem { |
1077 | 0 | (&self[..]).iter_elements() |
1078 | 0 | } Unexecuted instantiation: <&[u8; 0] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 1] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 2] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 3] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 4] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 5] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 6] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 7] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 8] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 9] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 10] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 11] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 12] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 13] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 14] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 15] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 16] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 17] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 18] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 19] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 20] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 21] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 22] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 23] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 24] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 25] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 26] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 27] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 28] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 29] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 30] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 31] as nom::traits::InputIter>::iter_elements Unexecuted instantiation: <&[u8; 32] as nom::traits::InputIter>::iter_elements |
1079 | | |
1080 | 0 | fn position<P>(&self, predicate: P) -> Option<usize> |
1081 | 0 | where P: Fn(Self::Item) -> bool { |
1082 | 0 | (&self[..]).position(predicate) |
1083 | 0 | } Unexecuted instantiation: <&[u8; 0] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 1] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 2] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 3] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 4] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 5] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 6] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 7] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 8] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 9] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 10] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 11] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 12] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 13] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 14] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 15] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 16] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 17] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 18] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 19] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 20] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 21] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 22] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 23] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 24] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 25] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 26] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 27] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 28] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 29] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 30] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 31] as nom::traits::InputIter>::position::<_> Unexecuted instantiation: <&[u8; 32] as nom::traits::InputIter>::position::<_> |
1084 | | |
1085 | 0 | fn slice_index(&self, count: usize) -> Result<usize, Needed> { |
1086 | 0 | (&self[..]).slice_index(count) |
1087 | 0 | } Unexecuted instantiation: <&[u8; 0] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 1] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 2] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 3] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 4] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 5] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 6] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 7] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 8] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 9] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 10] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 11] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 12] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 13] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 14] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 15] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 16] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 17] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 18] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 19] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 20] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 21] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 22] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 23] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 24] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 25] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 26] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 27] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 28] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 29] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 30] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 31] as nom::traits::InputIter>::slice_index Unexecuted instantiation: <&[u8; 32] as nom::traits::InputIter>::slice_index |
1088 | | } |
1089 | | |
1090 | | impl<'a> Compare<[u8; $N]> for &'a [u8] { |
1091 | | #[inline(always)] |
1092 | 0 | fn compare(&self, t: [u8; $N]) -> CompareResult { |
1093 | 0 | self.compare(&t[..]) |
1094 | 0 | } Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 0]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 1]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 2]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 3]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 4]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 5]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 6]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 7]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 8]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 9]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 10]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 11]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 12]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 13]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 14]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 15]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 16]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 17]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 18]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 19]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 20]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 21]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 22]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 23]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 24]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 25]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 26]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 27]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 28]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 29]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 30]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 31]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 32]>>::compare |
1095 | | |
1096 | | #[inline(always)] |
1097 | 0 | fn compare_no_case(&self, t: [u8;$N]) -> CompareResult { |
1098 | 0 | self.compare_no_case(&t[..]) |
1099 | 0 | } Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 0]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 1]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 2]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 3]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 4]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 5]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 6]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 7]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 8]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 9]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 10]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 11]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 12]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 13]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 14]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 15]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 16]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 17]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 18]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 19]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 20]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 21]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 22]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 23]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 24]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 25]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 26]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 27]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 28]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 29]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 30]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 31]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<[u8; 32]>>::compare_no_case |
1100 | | } |
1101 | | |
1102 | | impl<'a,'b> Compare<&'b [u8; $N]> for &'a [u8] { |
1103 | | #[inline(always)] |
1104 | 0 | fn compare(&self, t: &'b [u8; $N]) -> CompareResult { |
1105 | 0 | self.compare(&t[..]) |
1106 | 0 | } Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 0]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 1]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 2]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 3]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 4]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 5]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 6]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 7]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 8]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 9]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 10]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 11]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 12]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 13]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 14]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 15]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 16]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 17]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 18]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 19]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 20]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 21]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 22]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 23]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 24]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 25]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 26]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 27]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 28]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 29]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 30]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 31]>>::compare Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 32]>>::compare |
1107 | | |
1108 | | #[inline(always)] |
1109 | 0 | fn compare_no_case(&self, t: &'b [u8;$N]) -> CompareResult { |
1110 | 0 | self.compare_no_case(&t[..]) |
1111 | 0 | } Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 0]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 1]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 2]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 3]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 4]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 5]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 6]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 7]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 8]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 9]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 10]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 11]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 12]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 13]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 14]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 15]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 16]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 17]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 18]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 19]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 20]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 21]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 22]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 23]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 24]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 25]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 26]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 27]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 28]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 29]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 30]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 31]>>::compare_no_case Unexecuted instantiation: <&[u8] as nom::traits::Compare<&[u8; 32]>>::compare_no_case |
1112 | | } |
1113 | | |
1114 | | impl FindToken<u8> for [u8; $N] { |
1115 | 0 | fn find_token(&self, token: u8) -> bool { |
1116 | 0 | memchr::memchr(token, &self[..]).is_some() |
1117 | 0 | } Unexecuted instantiation: <[u8; 0] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 1] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 2] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 3] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 4] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 5] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 6] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 7] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 8] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 9] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 10] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 11] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 12] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 13] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 14] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 15] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 16] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 17] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 18] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 19] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 20] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 21] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 22] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 23] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 24] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 25] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 26] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 27] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 28] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 29] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 30] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 31] as nom::traits::FindToken<u8>>::find_token Unexecuted instantiation: <[u8; 32] as nom::traits::FindToken<u8>>::find_token |
1118 | | } |
1119 | | |
1120 | | impl<'a> FindToken<&'a u8> for [u8; $N] { |
1121 | 0 | fn find_token(&self, token: &u8) -> bool { |
1122 | 0 | self.find_token(*token) |
1123 | 0 | } Unexecuted instantiation: <[u8; 0] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 1] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 2] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 3] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 4] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 5] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 6] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 7] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 8] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 9] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 10] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 11] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 12] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 13] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 14] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 15] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 16] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 17] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 18] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 19] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 20] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 21] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 22] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 23] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 24] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 25] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 26] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 27] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 28] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 29] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 30] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 31] as nom::traits::FindToken<&u8>>::find_token Unexecuted instantiation: <[u8; 32] as nom::traits::FindToken<&u8>>::find_token |
1124 | | } |
1125 | | )+ |
1126 | | }; |
1127 | | } |
1128 | | |
1129 | | array_impls! { |
1130 | | 0 1 2 3 4 5 6 7 8 9 |
1131 | | 10 11 12 13 14 15 16 17 18 19 |
1132 | | 20 21 22 23 24 25 26 27 28 29 |
1133 | | 30 31 32 |
1134 | | } |
1135 | | |
1136 | | /// Abstracts something which can extend an `Extend`. |
1137 | | /// Used to build modified input slices in `escaped_transform` |
1138 | | pub trait ExtendInto { |
1139 | | /// The current input type is a sequence of that `Item` type. |
1140 | | /// |
1141 | | /// Example: `u8` for `&[u8]` or `char` for `&str` |
1142 | | type Item; |
1143 | | |
1144 | | /// The type that will be produced |
1145 | | type Extender; |
1146 | | |
1147 | | /// Create a new `Extend` of the correct type |
1148 | | fn new_builder(&self) -> Self::Extender; |
1149 | | /// Accumulate the input into an accumulator |
1150 | | fn extend_into(&self, acc: &mut Self::Extender); |
1151 | | } |
1152 | | |
1153 | | #[cfg(feature = "alloc")] |
1154 | | impl ExtendInto for [u8] { |
1155 | | type Item = u8; |
1156 | | type Extender = Vec<u8>; |
1157 | | |
1158 | | #[inline] |
1159 | 0 | fn new_builder(&self) -> Vec<u8> { |
1160 | 0 | Vec::new() |
1161 | 0 | } |
1162 | | #[inline] |
1163 | 0 | fn extend_into(&self, acc: &mut Vec<u8>) { |
1164 | 0 | acc.extend(self.iter().cloned()); |
1165 | 0 | } |
1166 | | } |
1167 | | |
1168 | | #[cfg(feature = "alloc")] |
1169 | | impl ExtendInto for &[u8] { |
1170 | | type Item = u8; |
1171 | | type Extender = Vec<u8>; |
1172 | | |
1173 | | #[inline] |
1174 | 0 | fn new_builder(&self) -> Vec<u8> { |
1175 | 0 | Vec::new() |
1176 | 0 | } |
1177 | | #[inline] |
1178 | 0 | fn extend_into(&self, acc: &mut Vec<u8>) { |
1179 | 0 | acc.extend_from_slice(self); |
1180 | 0 | } |
1181 | | } |
1182 | | |
1183 | | #[cfg(feature = "alloc")] |
1184 | | impl ExtendInto for str { |
1185 | | type Item = char; |
1186 | | type Extender = String; |
1187 | | |
1188 | | #[inline] |
1189 | 0 | fn new_builder(&self) -> String { |
1190 | 0 | String::new() |
1191 | 0 | } |
1192 | | #[inline] |
1193 | 0 | fn extend_into(&self, acc: &mut String) { |
1194 | 0 | acc.push_str(self); |
1195 | 0 | } |
1196 | | } |
1197 | | |
1198 | | #[cfg(feature = "alloc")] |
1199 | | impl ExtendInto for &str { |
1200 | | type Item = char; |
1201 | | type Extender = String; |
1202 | | |
1203 | | #[inline] |
1204 | 0 | fn new_builder(&self) -> String { |
1205 | 0 | String::new() |
1206 | 0 | } |
1207 | | #[inline] |
1208 | 0 | fn extend_into(&self, acc: &mut String) { |
1209 | 0 | acc.push_str(self); |
1210 | 0 | } |
1211 | | } |
1212 | | |
1213 | | #[cfg(feature = "alloc")] |
1214 | | impl ExtendInto for char { |
1215 | | type Item = char; |
1216 | | type Extender = String; |
1217 | | |
1218 | | #[inline] |
1219 | 0 | fn new_builder(&self) -> String { |
1220 | 0 | String::new() |
1221 | 0 | } |
1222 | | #[inline] |
1223 | 0 | fn extend_into(&self, acc: &mut String) { |
1224 | 0 | acc.push(*self); |
1225 | 0 | } |
1226 | | } |
1227 | | |
1228 | | /// Helper trait to convert numbers to usize. |
1229 | | /// |
1230 | | /// By default, usize implements `From<u8>` and `From<u16>` but not |
1231 | | /// `From<u32>` and `From<u64>` because that would be invalid on some |
1232 | | /// platforms. This trait implements the conversion for platforms |
1233 | | /// with 32 and 64 bits pointer platforms |
1234 | | pub trait ToUsize { |
1235 | | /// converts self to usize |
1236 | | fn to_usize(&self) -> usize; |
1237 | | } |
1238 | | |
1239 | | impl ToUsize for u8 { |
1240 | | #[inline] |
1241 | 0 | fn to_usize(&self) -> usize { |
1242 | 0 | *self as usize |
1243 | 0 | } Unexecuted instantiation: <u8 as nom::traits::ToUsize>::to_usize Unexecuted instantiation: <u8 as nom::traits::ToUsize>::to_usize |
1244 | | } |
1245 | | |
1246 | | impl ToUsize for u16 { |
1247 | | #[inline] |
1248 | 0 | fn to_usize(&self) -> usize { |
1249 | 0 | *self as usize |
1250 | 0 | } Unexecuted instantiation: <u16 as nom::traits::ToUsize>::to_usize Unexecuted instantiation: <u16 as nom::traits::ToUsize>::to_usize |
1251 | | } |
1252 | | |
1253 | | impl ToUsize for usize { |
1254 | | #[inline] |
1255 | 0 | fn to_usize(&self) -> usize { |
1256 | 0 | *self |
1257 | 0 | } Unexecuted instantiation: <usize as nom::traits::ToUsize>::to_usize Unexecuted instantiation: <usize as nom::traits::ToUsize>::to_usize Unexecuted instantiation: <usize as nom::traits::ToUsize>::to_usize |
1258 | | } |
1259 | | |
1260 | | #[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] |
1261 | | impl ToUsize for u32 { |
1262 | | #[inline] |
1263 | 0 | fn to_usize(&self) -> usize { |
1264 | 0 | *self as usize |
1265 | 0 | } |
1266 | | } |
1267 | | |
1268 | | #[cfg(target_pointer_width = "64")] |
1269 | | impl ToUsize for u64 { |
1270 | | #[inline] |
1271 | 0 | fn to_usize(&self) -> usize { |
1272 | 0 | *self as usize |
1273 | 0 | } |
1274 | | } |
1275 | | |
1276 | | /// Equivalent From implementation to avoid orphan rules in bits parsers |
1277 | | pub trait ErrorConvert<E> { |
1278 | | /// Transform to another error type |
1279 | | fn convert(self) -> E; |
1280 | | } |
1281 | | |
1282 | | impl<I> ErrorConvert<(I, ErrorKind)> for ((I, usize), ErrorKind) { |
1283 | 0 | fn convert(self) -> (I, ErrorKind) { |
1284 | 0 | ((self.0).0, self.1) |
1285 | 0 | } |
1286 | | } |
1287 | | |
1288 | | impl<I> ErrorConvert<((I, usize), ErrorKind)> for (I, ErrorKind) { |
1289 | 0 | fn convert(self) -> ((I, usize), ErrorKind) { |
1290 | 0 | ((self.0, 0), self.1) |
1291 | 0 | } |
1292 | | } |
1293 | | |
1294 | | use crate::error; |
1295 | | impl<I> ErrorConvert<error::Error<I>> for error::Error<(I, usize)> { |
1296 | 0 | fn convert(self) -> error::Error<I> { |
1297 | 0 | error::Error { |
1298 | 0 | input: self.input.0, |
1299 | 0 | code: self.code, |
1300 | 0 | } |
1301 | 0 | } |
1302 | | } |
1303 | | |
1304 | | impl<I> ErrorConvert<error::Error<(I, usize)>> for error::Error<I> { |
1305 | 0 | fn convert(self) -> error::Error<(I, usize)> { |
1306 | 0 | error::Error { |
1307 | 0 | input: (self.input, 0), |
1308 | 0 | code: self.code, |
1309 | 0 | } |
1310 | 0 | } |
1311 | | } |
1312 | | |
1313 | | #[cfg(feature = "alloc")] |
1314 | | #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] |
1315 | | impl<I> ErrorConvert<error::VerboseError<I>> for error::VerboseError<(I, usize)> { |
1316 | 0 | fn convert(self) -> error::VerboseError<I> { |
1317 | 0 | error::VerboseError { |
1318 | 0 | errors: self.errors.into_iter().map(|(i, e)| (i.0, e)).collect(), |
1319 | 0 | } |
1320 | 0 | } |
1321 | | } |
1322 | | |
1323 | | #[cfg(feature = "alloc")] |
1324 | | #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] |
1325 | | impl<I> ErrorConvert<error::VerboseError<(I, usize)>> for error::VerboseError<I> { |
1326 | 0 | fn convert(self) -> error::VerboseError<(I, usize)> { |
1327 | 0 | error::VerboseError { |
1328 | 0 | errors: self.errors.into_iter().map(|(i, e)| ((i, 0), e)).collect(), |
1329 | 0 | } |
1330 | 0 | } |
1331 | | } |
1332 | | |
1333 | | impl ErrorConvert<()> for () { |
1334 | 0 | fn convert(self) {} |
1335 | | } |
1336 | | |
1337 | | #[cfg(feature = "std")] |
1338 | | #[cfg_attr(feature = "docsrs", doc(cfg(feature = "std")))] |
1339 | | /// Helper trait to show a byte slice as a hex dump |
1340 | | pub trait HexDisplay { |
1341 | | /// Converts the value of `self` to a hex dump, returning the owned |
1342 | | /// `String`. |
1343 | | fn to_hex(&self, chunk_size: usize) -> String; |
1344 | | |
1345 | | /// Converts the value of `self` to a hex dump beginning at `from` address, returning the owned |
1346 | | /// `String`. |
1347 | | fn to_hex_from(&self, chunk_size: usize, from: usize) -> String; |
1348 | | } |
1349 | | |
1350 | | #[cfg(feature = "std")] |
1351 | | static CHARS: &[u8] = b"0123456789abcdef"; |
1352 | | |
1353 | | #[cfg(feature = "std")] |
1354 | | impl HexDisplay for [u8] { |
1355 | | #[allow(unused_variables)] |
1356 | 0 | fn to_hex(&self, chunk_size: usize) -> String { |
1357 | 0 | self.to_hex_from(chunk_size, 0) |
1358 | 0 | } |
1359 | | |
1360 | | #[allow(unused_variables)] |
1361 | 0 | fn to_hex_from(&self, chunk_size: usize, from: usize) -> String { |
1362 | 0 | let mut v = Vec::with_capacity(self.len() * 3); |
1363 | 0 | let mut i = from; |
1364 | 0 | for chunk in self.chunks(chunk_size) { |
1365 | 0 | let s = format!("{:08x}", i); |
1366 | 0 | for &ch in s.as_bytes().iter() { |
1367 | 0 | v.push(ch); |
1368 | 0 | } |
1369 | 0 | v.push(b'\t'); |
1370 | 0 |
|
1371 | 0 | i += chunk_size; |
1372 | | |
1373 | 0 | for &byte in chunk { |
1374 | 0 | v.push(CHARS[(byte >> 4) as usize]); |
1375 | 0 | v.push(CHARS[(byte & 0xf) as usize]); |
1376 | 0 | v.push(b' '); |
1377 | 0 | } |
1378 | 0 | if chunk_size > chunk.len() { |
1379 | 0 | for j in 0..(chunk_size - chunk.len()) { |
1380 | 0 | v.push(b' '); |
1381 | 0 | v.push(b' '); |
1382 | 0 | v.push(b' '); |
1383 | 0 | } |
1384 | 0 | } |
1385 | 0 | v.push(b'\t'); |
1386 | | |
1387 | 0 | for &byte in chunk { |
1388 | 0 | if (byte >= 32 && byte <= 126) || byte >= 128 { |
1389 | 0 | v.push(byte); |
1390 | 0 | } else { |
1391 | 0 | v.push(b'.'); |
1392 | 0 | } |
1393 | | } |
1394 | 0 | v.push(b'\n'); |
1395 | | } |
1396 | | |
1397 | 0 | String::from_utf8_lossy(&v[..]).into_owned() |
1398 | 0 | } |
1399 | | } |
1400 | | |
1401 | | #[cfg(feature = "std")] |
1402 | | impl HexDisplay for str { |
1403 | | #[allow(unused_variables)] |
1404 | 0 | fn to_hex(&self, chunk_size: usize) -> String { |
1405 | 0 | self.to_hex_from(chunk_size, 0) |
1406 | 0 | } |
1407 | | |
1408 | | #[allow(unused_variables)] |
1409 | 0 | fn to_hex_from(&self, chunk_size: usize, from: usize) -> String { |
1410 | 0 | self.as_bytes().to_hex_from(chunk_size, from) |
1411 | 0 | } |
1412 | | } |
1413 | | |
1414 | | #[cfg(test)] |
1415 | | mod tests { |
1416 | | use super::*; |
1417 | | |
1418 | | #[test] |
1419 | | fn test_offset_u8() { |
1420 | | let s = b"abcd123"; |
1421 | | let a = &s[..]; |
1422 | | let b = &a[2..]; |
1423 | | let c = &a[..4]; |
1424 | | let d = &a[3..5]; |
1425 | | assert_eq!(a.offset(b), 2); |
1426 | | assert_eq!(a.offset(c), 0); |
1427 | | assert_eq!(a.offset(d), 3); |
1428 | | } |
1429 | | |
1430 | | #[test] |
1431 | | fn test_offset_str() { |
1432 | | let s = "abcřèÂßÇd123"; |
1433 | | let a = &s[..]; |
1434 | | let b = &a[7..]; |
1435 | | let c = &a[..5]; |
1436 | | let d = &a[5..9]; |
1437 | | assert_eq!(a.offset(b), 7); |
1438 | | assert_eq!(a.offset(c), 0); |
1439 | | assert_eq!(a.offset(d), 5); |
1440 | | } |
1441 | | } |