1 //! Byte level parsers and combinators
2 //!
3 #[allow(unused_variables)]
4 
5 /// `tag!(&[T]: nom::AsBytes) => &[T] -> IResult<&[T], &[T]>`
6 /// declares a byte array as a suite to recognize
7 ///
8 /// consumes the recognized characters
9 ///
10 /// ```
11 /// # #[macro_use] extern crate nom;
12 /// # use nom::IResult::Done;
13 /// # fn main() {
14 ///  named!(x, tag!("abcd"));
15 ///  let r = x(&b"abcdefgh"[..]);
16 ///  assert_eq!(r, Done(&b"efgh"[..], &b"abcd"[..]));
17 /// # }
18 /// ```
19 #[macro_export]
20 macro_rules! tag (
21   ($i:expr, $tag: expr) => (
22     {
23       use $crate::{Compare,CompareResult,InputLength,Slice};
24       let res: $crate::IResult<_,_> = match ($i).compare($tag) {
25         CompareResult::Ok => {
26           let blen = $tag.input_len();
27           $crate::IResult::Done($i.slice(blen..), $i.slice(..blen))
28         },
29         CompareResult::Incomplete => {
30           $crate::IResult::Incomplete($crate::Needed::Size($tag.input_len()))
31         },
32         CompareResult::Error => {
33           $crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i))
34         }
35       };
36       res
37     }
38   );
39 );
40 
41 /// `tag_no_case!(&[T]) => &[T] -> IResult<&[T], &[T]>`
42 /// declares a case insensitive ascii string as a suite to recognize
43 ///
44 /// consumes the recognized characters
45 ///
46 /// ```
47 /// # #[macro_use] extern crate nom;
48 /// # use nom::IResult::{self,Done};
49 /// # fn main() {
50 ///  named!(test, tag_no_case!("ABcd"));
51 ///
52 ///  let r = test(&b"aBCdefgh"[..]);
53 ///  assert_eq!(r, Done(&b"efgh"[..], &b"aBCd"[..]));
54 /// # }
55 /// ```
56 #[macro_export]
57 macro_rules! tag_no_case (
58   ($i:expr, $tag: expr) => (
59     {
60       use $crate::{Compare,CompareResult,InputLength,Slice};
61       let res: $crate::IResult<_,_> = match ($i).compare_no_case($tag) {
62         CompareResult::Ok => {
63           let blen = $tag.input_len();
64           $crate::IResult::Done($i.slice(blen..), $i.slice(..blen))
65         },
66         CompareResult::Incomplete => {
67           $crate::IResult::Incomplete($crate::Needed::Size($tag.input_len()))
68         },
69         CompareResult::Error => {
70           $crate::IResult::Error(error_position!($crate::ErrorKind::Tag, $i))
71         }
72       };
73       res
74     }
75   );
76 );
77 
78 
79 /// `is_not!(&[T:AsBytes]) => &[T] -> IResult<&[T], &[T]>`
80 /// returns the longest list of bytes that do not appear in the provided array
81 ///
82 /// ```
83 /// # #[macro_use] extern crate nom;
84 /// # use nom::IResult::Done;
85 /// # fn main() {
86 ///  named!( not_space, is_not!( " \t\r\n" ) );
87 ///
88 ///  let r = not_space(&b"abcdefgh\nijkl"[..]);
89 ///  assert_eq!(r, Done(&b"\nijkl"[..], &b"abcdefgh"[..]));
90 ///  # }
91 /// ```
92 #[macro_export]
93 macro_rules! is_not(
94   ($input:expr, $arr:expr) => (
95     {
96       use $crate::InputLength;
97       use $crate::InputIter;
98       use $crate::FindToken;
99       use $crate::Slice;
100 
101       let res: $crate::IResult<_,_> = match $input.position(|c| {
102         c.find_token($arr)
103       }) {
104         Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::IsNot,$input)),
105         Some(n) => {
106           let res = $crate::IResult::Done($input.slice(n..), $input.slice(..n));
107           res
108         },
109         None    => {
110           $crate::IResult::Done($input.slice($input.input_len()..), $input)
111         }
112       };
113       res
114     }
115   );
116 );
117 
118 /// `is_a!(&[T]) => &[T] -> IResult<&[T], &[T]>`
119 /// returns the longest list of bytes that appear in the provided array
120 ///
121 /// ```
122 /// # #[macro_use] extern crate nom;
123 /// # use nom::IResult::Done;
124 /// # fn main() {
125 ///  named!(abcd, is_a!( "abcd" ));
126 ///
127 ///  let r1 = abcd(&b"aaaaefgh"[..]);
128 ///  assert_eq!(r1, Done(&b"efgh"[..], &b"aaaa"[..]));
129 ///
130 ///  let r2 = abcd(&b"dcbaefgh"[..]);
131 ///  assert_eq!(r2, Done(&b"efgh"[..], &b"dcba"[..]));
132 /// # }
133 /// ```
134 #[macro_export]
135 macro_rules! is_a (
136   ($input:expr, $arr:expr) => (
137     {
138       use $crate::InputLength;
139       use $crate::InputIter;
140       use $crate::FindToken;
141       use $crate::Slice;
142 
143       let res: $crate::IResult<_,_> = match $input.position(|c| {
144         !c.find_token($arr)
145       }) {
146         Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::IsA,$input)),
147         Some(n) => {
148           let res: $crate::IResult<_,_> = $crate::IResult::Done($input.slice(n..), $input.slice(..n));
149           res
150         },
151         None    => {
152           $crate::IResult::Done($input.slice(($input).input_len()..), $input)
153         }
154       };
155       res
156     }
157   );
158 );
159 
160 /// `escaped!(&[T] -> IResult<&[T], &[T]>, T, &[T] -> IResult<&[T], &[T]>) => &[T] -> IResult<&[T], &[T]>`
161 /// matches a byte string with escaped characters.
162 ///
163 /// The first argument matches the normal characters (it must not accept the control character), the second argument is the control character (like `\` in most languages),
164 /// the third argument matches the escaped characters
165 ///
166 /// ```
167 /// # #[macro_use] extern crate nom;
168 /// # use nom::IResult::Done;
169 /// # use nom::alpha;
170 /// # fn main() {
171 ///  named!(esc, escaped!(call!(alpha), '\\', one_of!("\"n\\")));
172 ///  assert_eq!(esc(&b"abcd"[..]), Done(&b""[..], &b"abcd"[..]));
173 ///  assert_eq!(esc(&b"ab\\\"cd"[..]), Done(&b""[..], &b"ab\\\"cd"[..]));
174 /// # }
175 /// ```
176 #[macro_export]
177 macro_rules! escaped (
178   // Internal parser, do not use directly
179   (__impl $i: expr, $normal:ident!(  $($args:tt)* ), $control_char: expr, $escapable:ident!(  $($args2:tt)* )) => (
180     {
181       use $crate::InputLength;
182       use $crate::Slice;
183       let cl = || -> $crate::IResult<_,_,_> {
184         use $crate::Offset;
185         let mut index  = 0;
186 
187         while index < $i.input_len() {
188           match $normal!($i.slice(index..), $($args)*) {
189             $crate::IResult::Done(i, _) => {
190               if i.is_empty() {
191                 return $crate::IResult::Done($i.slice($i.input_len()..), $i)
192               } else {
193                 index = $i.offset(i);
194               }
195             },
196             $crate::IResult::Incomplete(i) => {
197               return $crate::IResult::Incomplete(i)
198             },
199             $crate::IResult::Error(e) => {
200               if $i[index] == $control_char as u8 {
201                 if index + 1 >= $i.input_len() {
202                   return $crate::IResult::Error(error_node_position!($crate::ErrorKind::Escaped, $i.slice(index..), e));
203                 } else {
204                   match $escapable!($i.slice(index+1..), $($args2)*) {
205                     $crate::IResult::Done(i,_) => {
206                       if i.is_empty() {
207                         return $crate::IResult::Done($i.slice($i.input_len()..), $i)
208                       } else {
209                         index = $i.offset(i);
210                       }
211                     },
212                     $crate::IResult::Incomplete(i) => return $crate::IResult::Incomplete(i),
213                     $crate::IResult::Error(e2)     => return $crate::IResult::Error(e2)
214                   }
215                 }
216               } else {
217                 return $crate::IResult::Done($i.slice(index..), $i.slice(..index));
218               }
219             }
220           }
221         }
222         $crate::IResult::Done(&$i[index..], &$i[..index])
223       };
224       match cl() {
225         $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
226         $crate::IResult::Done(i, o)    => $crate::IResult::Done(i, o),
227         $crate::IResult::Error(e)      => {
228           return $crate::IResult::Error(error_node_position!($crate::ErrorKind::Escaped, $i, e))
229         }
230       }
231     }
232   );
233   // Internal parser, do not use directly
234   (__impl_1 $i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => (
235     {
236      escaped!(__impl $i, $submac1!($($args)*), $control_char,  $submac2!($($args2)*))
237     }
238   );
239   // Internal parser, do not use directly
240   (__impl_1 $i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $g:expr) => (
241      escaped!(__impl $i, $submac1!($($args)*), $control_char, call!($g))
242   );
243   ($i:expr, $submac:ident!( $($args:tt)* ), $control_char: expr, $($rest:tt)+) => (
244     {
245       let input: &[u8] = $i;
246 
247       escaped!(__impl_1 input, $submac!($($args)*), $control_char, $($rest)*)
248     }
249   );
250 
251   ($i:expr, $f:expr, $control_char: expr, $($rest:tt)+) => (
252     escaped!(__impl_1 $i, call!($f), $control_char, $($rest)*)
253   );
254 );
255 
256 /// `escaped_transform!(&[T] -> IResult<&[T], &[T]>, T, &[T] -> IResult<&[T], &[T]>) => &[T] -> IResult<&[T], Vec<T>>`
257 /// matches a byte string with escaped characters.
258 ///
259 /// The first argument matches the normal characters (it must not match the control character), the second argument is the control character (like `\` in most languages),
260 /// the third argument matches the escaped characters and transforms them.
261 ///
262 /// As an example, the chain `abc\tdef` could be `abc    def` (it also consumes the control character)
263 ///
264 /// WARNING: if you do not use the `verbose-errors` feature, this combinator will currently fail to build
265 /// because of a type inference error
266 ///
267 /// ```ignore
268 /// # #[macro_use] extern crate nom;
269 /// # use nom::IResult::Done;
270 /// # use nom::alpha;
271 /// # use std::str::from_utf8;
272 /// # fn main() {
273 /// fn to_s(i:Vec<u8>) -> String {
274 ///   String::from_utf8_lossy(&i).into_owned()
275 /// }
276 
277 ///  named!(transform < String >,
278 ///    map!(
279 ///      escaped_transform!(call!(alpha), '\\',
280 ///        alt!(
281 ///            tag!("\\")       => { |_| &b"\\"[..] }
282 ///          | tag!("\"")       => { |_| &b"\""[..] }
283 ///          | tag!("n")        => { |_| &b"\n"[..] }
284 ///        )
285 ///      ), to_s
286 ///    )
287 ///  );
288 ///  assert_eq!(transform(&b"ab\\\"cd"[..]), Done(&b""[..], String::from("ab\"cd")));
289 /// # }
290 /// ```
291 #[macro_export]
292 macro_rules! escaped_transform (
293   // Internal parser, do not use directly
294   (__impl $i: expr, $normal:ident!(  $($args:tt)* ), $control_char: expr, $transform:ident!(  $($args2:tt)* )) => (
295     {
296       use $crate::{InputLength,Slice};
297       let cl = || {
298         use $crate::Offset;
299         let mut index  = 0;
300         let mut res = Vec::new();
301 
302         while index < $i.input_len() {
303           if let $crate::IResult::Done(i,o) = $normal!($i.slice(index..), $($args)*) {
304             res.extend(o.iter().cloned());
305             if i.is_empty() {
306               return $crate::IResult::Done($i.slice($i.input_len()..), res);
307             } else {
308               index = $i.offset(i);
309             }
310           } else if $i[index] == $control_char as u8 {
311             if index + 1 >= $i.input_len() {
312               return $crate::IResult::Error(error_position!($crate::ErrorKind::EscapedTransform,$i.slice(index..)));
313             } else {
314               match $transform!($i.slice(index+1..), $($args2)*) {
315                 $crate::IResult::Done(i,o) => {
316                   res.extend(o.iter().cloned());
317                   if i.is_empty() {
318                     return $crate::IResult::Done($i.slice($i.input_len()..), res)
319                   } else {
320                     index = $i.offset(i);
321                   }
322                 },
323                 $crate::IResult::Incomplete(i) => return $crate::IResult::Incomplete(i),
324                 $crate::IResult::Error(e)      => return $crate::IResult::Error(e)
325               }
326             }
327           } else {
328             if index == 0 {
329               return $crate::IResult::Error(error_position!($crate::ErrorKind::EscapedTransform,$i.slice(index..)))
330             } else {
331               return $crate::IResult::Done($i.slice(index..), res)
332             }
333           }
334         }
335         $crate::IResult::Done($i.slice(index..), res)
336       };
337       match cl() {
338         $crate::IResult::Incomplete(x) => $crate::IResult::Incomplete(x),
339         $crate::IResult::Done(i, o)    => $crate::IResult::Done(i, o),
340         $crate::IResult::Error(e)      => {
341           return $crate::IResult::Error(error_node_position!($crate::ErrorKind::EscapedTransform, $i, e))
342         }
343       }
344     }
345   );
346   // Internal parser, do not use directly
347   (__impl_1 $i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => (
348     {
349      escaped_transform!(__impl $i, $submac1!($($args)*), $control_char,  $submac2!($($args2)*))
350     }
351   );
352   // Internal parser, do not use directly
353   (__impl_1 $i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $g:expr) => (
354      escaped_transform_impl!($i, $submac1!($($args)*), $control_char, call!($g))
355   );
356   ($i:expr, $submac:ident!( $($args:tt)* ), $control_char: expr, $($rest:tt)+) => (
357     {
358       let input: &[u8] = $i;
359 
360       escaped_transform!(__impl_1 input, $submac!($($args)*), $control_char, $($rest)*)
361     }
362   );
363 
364   ($i:expr, $f:expr, $control_char: expr, $($rest:tt)+) => (
365     escaped_transform!(__impl_1 $i, call!($f), $control_char, $($rest)*)
366   );
367 );
368 
369 /// `take_while!(T -> bool) => &[T] -> IResult<&[T], &[T]>`
370 /// returns the longest list of bytes until the provided function fails.
371 ///
372 /// The argument is either a function `T -> bool` or a macro returning a `bool`.
373 ///
374 /// ```
375 /// # #[macro_use] extern crate nom;
376 /// # use nom::IResult::Done;
377 /// # use nom::is_alphanumeric;
378 /// # fn main() {
379 ///  named!( alpha, take_while!( is_alphanumeric ) );
380 ///
381 ///  let r = alpha(&b"abcd\nefgh"[..]);
382 ///  assert_eq!(r, Done(&b"\nefgh"[..], &b"abcd"[..]));
383 /// # }
384 /// ```
385 #[macro_export]
386 macro_rules! take_while (
387   ($input:expr, $submac:ident!( $($args:tt)* )) => (
388     {
389       use $crate::{InputLength,InputIter,Slice};
390       let input = $input;
391 
392       match input.position(|c| !$submac!(c, $($args)*)) {
393         Some(n) => {
394           let res:$crate::IResult<_,_> = $crate::IResult::Done(input.slice(n..), input.slice(..n));
395           res
396         },
397         None    => {
398           $crate::IResult::Done(input.slice(input.input_len()..), input)
399         }
400       }
401     }
402   );
403   ($input:expr, $f:expr) => (
404     take_while!($input, call!($f));
405   );
406 );
407 
408 /// `take_while1!(T -> bool) => &[T] -> IResult<&[T], &[T]>`
409 /// returns the longest (non empty) list of bytes until the provided function fails.
410 ///
411 /// The argument is either a function `&[T] -> bool` or a macro returning a `bool
412 #[macro_export]
413 macro_rules! take_while1 (
414   ($input:expr, $submac:ident!( $($args:tt)* )) => (
415     {
416       let input = $input;
417 
418       use $crate::InputLength;
419       use $crate::InputIter;
420       use $crate::Slice;
421       if input.input_len() == 0 {
422         $crate::IResult::Incomplete($crate::Needed::Size(1))
423       } else {
424         match input.position(|c| !$submac!(c, $($args)*)) {
425           Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeWhile1,input)),
426           Some(n) => {
427             $crate::IResult::Done(input.slice(n..), input.slice(..n))
428           },
429           None    => {
430             $crate::IResult::Done(input.slice(input.input_len()..), input)
431           }
432         }
433       }
434     }
435   );
436   ($input:expr, $f:expr) => (
437     take_while1!($input, call!($f));
438   );
439 );
440 
441 /// `take_till!(T -> bool) => &[T] -> IResult<&[T], &[T]>`
442 /// returns the longest list of bytes until the provided function succeeds
443 ///
444 /// The argument is either a function `&[T] -> bool` or a macro returning a `bool
445 #[macro_export]
446 macro_rules! take_till (
447   ($input:expr, $submac:ident!( $($args:tt)* )) => (
448     {
449       let input = $input;
450 
451       use $crate::InputLength;
452       use $crate::InputIter;
453       use $crate::Slice;
454       match input.position(|c| $submac!(c, $($args)*)) {
455         Some(n) => $crate::IResult::Done(input.slice(n..), input.slice(..n)),
456         None    => $crate::IResult::Done(input.slice(input.input_len()..), input)
457       }
458     }
459   );
460   ($input:expr, $f:expr) => (
461     take_till!($input, call!($f));
462   );
463 );
464 
465 /// `take_till1!(T -> bool) => &[T] -> IResult<&[T], &[T]>`
466 /// returns the longest non empty list of bytes until the provided function succeeds
467 ///
468 /// The argument is either a function `&[T] -> bool` or a macro returning a `bool
469 #[macro_export]
470 macro_rules! take_till1 (
471   ($input:expr, $submac:ident!( $($args:tt)* )) => (
472     {
473       let input = $input;
474 
475       use $crate::InputLength;
476       use $crate::InputIter;
477       use $crate::Slice;
478       if input.input_len() == 0 {
479         $crate::IResult::Incomplete($crate::Needed::Size(1))
480       } else {
481         match input.position(|c| $submac!(c, $($args)*)) {
482           Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeTill1,input)),
483           Some(n) => $crate::IResult::Done(input.slice(n..), input.slice(..n)),
484           None    => $crate::IResult::Done(input.slice(input.input_len()..), input)
485         }
486       }
487     }
488   );
489   ($input:expr, $f:expr) => (
490     take_till1!($input, call!($f));
491   );
492 );
493 
494 /// `take!(nb) => &[T] -> IResult<&[T], &[T]>`
495 /// generates a parser consuming the specified number of bytes
496 ///
497 /// ```
498 /// # #[macro_use] extern crate nom;
499 /// # use nom::IResult::Done;
500 /// # fn main() {
501 ///  // Desmond parser
502 ///  named!(take5, take!( 5 ) );
503 ///
504 ///  let a = b"abcdefgh";
505 ///
506 ///  assert_eq!(take5(&a[..]), Done(&b"fgh"[..], &b"abcde"[..]));
507 /// # }
508 /// ```
509 #[macro_export]
510 macro_rules! take (
511   ($i:expr, $count:expr) => (
512     {
513       use $crate::InputIter;
514       use $crate::Slice;
515       let input = $i;
516 
517       let cnt = $count as usize;
518 
519       let res: $crate::IResult<_,_> = match input.slice_index(cnt) {
520         None        => $crate::IResult::Incomplete($crate::Needed::Size(cnt)),
521         //FIXME: use the InputTake trait
522         Some(index) => $crate::IResult::Done(input.slice(index..), input.slice(..index))
523       };
524       res
525     }
526   );
527 );
528 
529 /// `take!(nb) => &[T] -> IResult<&[T], &str>`
530 /// same as take! but returning a &str
531 #[macro_export]
532 macro_rules! take_str (
533  ( $i:expr, $size:expr ) => (
534     {
535       let input: &[u8] = $i;
536 
537       map_res!(input, take!($size), ::std::str::from_utf8)
538     }
539   );
540 );
541 
542 /// `take_until_and_consume!(tag) => &[T] -> IResult<&[T], &[T]>`
543 /// generates a parser consuming bytes until the specified byte sequence is found, and consumes it
544 #[macro_export]
545 macro_rules! take_until_and_consume (
546   ($i:expr, $substr:expr) => (
547     {
548       use $crate::InputLength;
549       use $crate::FindSubstring;
550       use $crate::Slice;
551 
552       let res: $crate::IResult<_,_> = if $substr.input_len() > $i.input_len() {
553         $crate::IResult::Incomplete($crate::Needed::Size($substr.input_len()))
554       } else {
555         match ($i).find_substring($substr) {
556           None => {
557             $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilAndConsume,$i))
558           },
559           Some(index) => {
560             $crate::IResult::Done($i.slice(index+$substr.input_len()..), $i.slice(0..index))
561           },
562         }
563       };
564       res
565     }
566   );
567 );
568 
569 /// `take_until_and_consume1!(tag) => &[T] -> IResult<&[T], &[T]>`
570 /// generates a parser consuming bytes (at least 1) until the specified byte sequence is found, and consumes it
571 #[macro_export]
572 macro_rules! take_until_and_consume1 (
573   ($i:expr, $substr:expr) => (
574     {
575       use $crate::InputLength;
576 
577       let res: $crate::IResult<_,_> = if 1 + $substr.input_len() > $i.input_len() {
578         $crate::IResult::Incomplete($crate::Needed::Size($substr.input_len()))
579       } else {
580         match ($i).find_substring($substr) {
581           None => {
582             $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilAndConsume,$i))
583           },
584           Some(index) => {
585             $crate::IResult::Done($i.slice(index+$substr.input_len()..), $i.slice(0..index))
586           },
587         }
588       };
589       res
590     }
591   );
592 );
593 
594 /// `take_until!(tag) => &[T] -> IResult<&[T], &[T]>`
595 /// consumes data until it finds the specified tag
596 #[macro_export]
597 macro_rules! take_until (
598   ($i:expr, $substr:expr) => (
599     {
600       use $crate::InputLength;
601       use $crate::FindSubstring;
602       use $crate::Slice;
603 
604       let res: $crate::IResult<_,_> = if $substr.input_len() > $i.input_len() {
605         $crate::IResult::Incomplete($crate::Needed::Size($substr.input_len()))
606       } else {
607         match ($i).find_substring($substr) {
608           None => {
609             $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntil,$i))
610           },
611           Some(index) => {
612             $crate::IResult::Done($i.slice(index..), $i.slice(0..index))
613           },
614         }
615       };
616       res
617     }
618   );
619 );
620 
621 /// `take_until1!(tag) => &[T] -> IResult<&[T], &[T]>`
622 /// consumes data until it finds the specified tag
623 #[macro_export]
624 macro_rules! take_until1 (
625   ($i:expr, $substr:expr) => (
626     {
627       use $crate::InputLength;
628       use $crate::FindSubstring;
629       use $crate::Slice;
630 
631       let res: $crate::IResult<_,_> = if 1+$substr.input_len() > $i.input_len() {
632         $crate::IResult::Incomplete($crate::Needed::Size($substr.input_len()))
633       } else {
634         match ($i).find_substring($substr) {
635           None => {
636             $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntil,$i))
637           },
638           Some(index) => {
639             $crate::IResult::Done($i.slice(index..), $i.slice(0..index))
640           },
641         }
642       };
643       res
644     }
645   );
646 );
647 
648 /// `take_until_either_and_consume!(tag) => &[T] -> IResult<&[T], &[T]>`
649 /// consumes data until it finds any of the specified characters, and consume it
650 #[macro_export]
651 macro_rules! take_until_either_and_consume (
652   ($input:expr, $arr:expr) => (
653     {
654       use $crate::InputLength;
655       use $crate::InputIter;
656       use $crate::FindToken;
657       use $crate::Slice;
658 
659       if $input.input_len() == 0 {
660         $crate::IResult::Incomplete($crate::Needed::Size(1))
661       } else {
662         let res: $crate::IResult<_,_> = match $input.position(|c| {
663           c.find_token($arr)
664         }) {
665           Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEitherAndConsume,$input)),
666           Some(n) => {
667             let res = $crate::IResult::Done($input.slice(n+1..), $input.slice(..n));
668             res
669           },
670           None    => {
671             $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEitherAndConsume,$input))
672           }
673         };
674         res
675       }
676     }
677   );
678 );
679 
680 /// `take_until_either!(tag) => &[T] -> IResult<&[T], &[T]>`
681 #[macro_export]
682 macro_rules! take_until_either (
683   ($input:expr, $arr:expr) => (
684     {
685       use $crate::InputLength;
686       use $crate::InputIter;
687       use $crate::FindToken;
688       use $crate::Slice;
689 
690       if $input.input_len() == 0 {
691         $crate::IResult::Incomplete($crate::Needed::Size(1))
692       } else {
693         let res: $crate::IResult<_,_> = match $input.position(|c| {
694           c.find_token($arr)
695         }) {
696           Some(0) => $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEither,$input)),
697           Some(n) => {
698             let res = $crate::IResult::Done($input.slice(n..), $input.slice(..n));
699             res
700           },
701           None    => {
702             $crate::IResult::Error(error_position!($crate::ErrorKind::TakeUntilEither,$input))
703           }
704         };
705         res
706       }
707     }
708   );
709 );
710 
711 /// `length_bytes!(&[T] -> IResult<&[T], nb>) => &[T] -> IResult<&[T], &[T]>`
712 /// Gets a number from the first parser, then extracts that many bytes from the
713 /// remaining stream
714 #[macro_export]
715 macro_rules! length_bytes(
716   ($i:expr, $submac:ident!( $($args:tt)* )) => (
717     {
718       use $crate::{Slice,InputLength};
719       let input: &[u8] = $i;
720 
721       match  $submac!(input, $($args)*) {
722         $crate::IResult::Error(a)      => $crate::IResult::Error(a),
723         $crate::IResult::Incomplete(i) => $crate::IResult::Incomplete(i),
724         $crate::IResult::Done(i1,nb)   => {
725           let nb = nb as usize;
726           if i1.input_len() < nb {
727             use $crate::Offset;
728             let (size,overflowed) = $i.offset(i1).overflowing_add(nb);
729             match overflowed {
730                 true  => $crate::IResult::Incomplete($crate::Needed::Unknown),
731                 false => $crate::IResult::Incomplete($crate::Needed::Size(size)),
732             }
733           } else {
734             $crate::IResult::Done(i1.slice(nb..), i1.slice(..nb))
735           }
736         }
737       }
738     }
739   );
740   ($i:expr, $f:expr) => (
741     length_bytes!($i, call!($f))
742   )
743 );
744 
745 #[cfg(test)]
746 mod tests {
747   use internal::Needed;
748   use internal::IResult::*;
749   use util::ErrorKind;
750   use nom::{alpha, digit, hex_digit, oct_digit, alphanumeric, space, multispace};
751 
752   macro_rules! one_of (
753     ($i:expr, $inp: expr) => (
754       {
755         if $i.is_empty() {
756           $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1))
757         } else {
758           #[inline(always)]
759           fn as_bytes<T: $crate::AsBytes>(b: &T) -> &[u8] {
760             b.as_bytes()
761           }
762 
763           let expected = $inp;
764           let bytes = as_bytes(&expected);
765           one_of_bytes!($i, bytes)
766         }
767       }
768     );
769   );
770 
771   macro_rules! one_of_bytes (
772     ($i:expr, $bytes: expr) => (
773       {
774         if $i.is_empty() {
775           $crate::IResult::Incomplete::<_, _>($crate::Needed::Size(1))
776         } else {
777           let mut found = false;
778 
779           for &i in $bytes {
780             if i == $i[0] {
781               found = true;
782               break;
783             }
784           }
785 
786           if found {
787             $crate::IResult::Done(&$i[1..], $i[0] as char)
788           } else {
789             $crate::IResult::Error(error_position!($crate::ErrorKind::OneOf, $i))
790           }
791         }
792       }
793     );
794   );
795 
796   #[test]
is_a()797   fn is_a() {
798     named!(a_or_b, is_a!(&b"ab"[..]));
799 
800     let a = &b"abcd"[..];
801     assert_eq!(a_or_b(a), Done(&b"cd"[..], &b"ab"[..]));
802 
803     let b = &b"bcde"[..];
804     assert_eq!(a_or_b(b), Done(&b"cde"[..], &b"b"[..]));
805 
806     let c = &b"cdef"[..];
807     assert_eq!(a_or_b(c), Error(error_position!(ErrorKind::IsA,c)));
808 
809     let d = &b"bacdef"[..];
810     assert_eq!(a_or_b(d), Done(&b"cdef"[..], &b"ba"[..]));
811   }
812 
813   #[test]
is_not()814   fn is_not() {
815     named!(a_or_b, is_not!(&b"ab"[..]));
816 
817     let a = &b"cdab"[..];
818     assert_eq!(a_or_b(a), Done(&b"ab"[..], &b"cd"[..]));
819 
820     let b = &b"cbde"[..];
821     assert_eq!(a_or_b(b), Done(&b"bde"[..], &b"c"[..]));
822 
823     let c = &b"abab"[..];
824     assert_eq!(a_or_b(c), Error(error_position!(ErrorKind::IsNot,c)));
825 
826     let d = &b"cdefba"[..];
827     assert_eq!(a_or_b(d), Done(&b"ba"[..], &b"cdef"[..]));
828 
829     let e = &b"e"[..];
830     assert_eq!(a_or_b(e), Done(&b""[..], &b"e"[..]));
831 
832     let f = &b"fghi"[..];
833     assert_eq!(a_or_b(f), Done(&b""[..], &b"fghi"[..]));
834   }
835 
836   #[allow(unused_variables)]
837   #[test]
escaping()838   fn escaping() {
839     named!(esc, escaped!(call!(alpha), '\\', one_of!("\"n\\")));
840     assert_eq!(esc(&b"abcd"[..]), Done(&b""[..], &b"abcd"[..]));
841     assert_eq!(esc(&b"ab\\\"cd"[..]), Done(&b""[..], &b"ab\\\"cd"[..]));
842     assert_eq!(esc(&b"\\\"abcd"[..]), Done(&b""[..], &b"\\\"abcd"[..]));
843     assert_eq!(esc(&b"\\n"[..]), Done(&b""[..], &b"\\n"[..]));
844     assert_eq!(esc(&b"ab\\\"12"[..]), Done(&b"12"[..], &b"ab\\\""[..]));
845     assert_eq!(esc(&b"AB\\"[..]), Error(error_node_position!(ErrorKind::Escaped, &b"AB\\"[..],
846       error_position!(ErrorKind::Escaped, &b"\\"[..]))));
847     assert_eq!(esc(&b"AB\\A"[..]), Error(error_node_position!(ErrorKind::Escaped, &b"AB\\A"[..],
848       error_position!(ErrorKind::IsA, &b"A"[..]))));
849 
850     named!(esc2, escaped!(call!(digit), '\\', one_of!("\"n\\")));
851     assert_eq!(esc2(&b"12\\nnn34"[..]), Done(&b"nn34"[..], &b"12\\n"[..]));
852   }
853 
854   #[cfg(feature = "verbose-errors")]
855   fn to_s(i:Vec<u8>) -> String {
856     String::from_utf8_lossy(&i).into_owned()
857   }
858 
859   #[cfg(feature = "verbose-errors")]
860   #[test]
861   fn escape_transform() {
862     use std::str;
863 
864     named!(esc<String>, map!(escaped_transform!(alpha, '\\',
865       alt!(
866           tag!("\\")       => { |_| &b"\\"[..] }
867         | tag!("\"")       => { |_| &b"\""[..] }
868         | tag!("n")        => { |_| &b"\n"[..] }
869       )), to_s)
870     );
871 
872     assert_eq!(esc(&b"abcd"[..]), Done(&b""[..], String::from("abcd")));
873     assert_eq!(esc(&b"ab\\\"cd"[..]), Done(&b""[..], String::from("ab\"cd")));
874     assert_eq!(esc(&b"\\\"abcd"[..]), Done(&b""[..], String::from("\"abcd")));
875     assert_eq!(esc(&b"\\n"[..]), Done(&b""[..], String::from("\n")));
876     assert_eq!(esc(&b"ab\\\"12"[..]), Done(&b"12"[..], String::from("ab\"")));
877     assert_eq!(esc(&b"AB\\"[..]), Error(error_node_position!(ErrorKind::EscapedTransform, &b"AB\\"[..], error_position!(ErrorKind::EscapedTransform, &b"\\"[..]))));
878     assert_eq!(esc(&b"AB\\A"[..]), Error(error_node_position!(ErrorKind::EscapedTransform, &b"AB\\A"[..],
879       error_position!(ErrorKind::Alt, &b"A"[..]))));
880 
881     let e = "è";
882     let a = "à";
883     println!("è: {:?} | à: {:?}", str::as_bytes(e), str::as_bytes(a));
884     named!(esc2< String >, map!(escaped_transform!(call!(alpha), '&',
885       alt!(
886           tag!("egrave;") => { |_| str::as_bytes("è") }
887         | tag!("agrave;") => { |_| str::as_bytes("à") }
888       )), to_s)
889     );
890     assert_eq!(esc2(&b"ab&egrave;DEF"[..]), Done(&b""[..], String::from("abèDEF")));
891     assert_eq!(esc2(&b"ab&egrave;D&agrave;EF"[..]), Done(&b""[..], String::from("abèDàEF")));
892   }
893 
894   #[test]
895   fn issue_84() {
896     let r0 = is_a!(&b"aaaaefgh"[..], "abcd");
897     assert_eq!(r0, Done(&b"efgh"[..], &b"aaaa"[..]));
898     let r1 = is_a!(&b"aaaa"[..], "abcd");
899     assert_eq!(r1, Done(&b""[..], &b"aaaa"[..]));
900     let r2 = is_a!(&b"1"[..], "123456789");
901     assert_eq!(r2, Done(&b""[..], &b"1"[..]));
902   }
903 
904   #[test]
905   fn take_str_test() {
906     let a = b"omnomnom";
907 
908     assert_eq!(take_str!(&a[..], 5), Done(&b"nom"[..], "omnom"));
909     assert_eq!(take_str!(&a[..], 9), Incomplete(Needed::Size(9)));
910   }
911 
912   #[test]
913   fn take_until_test() {
914     named!(x, take_until_and_consume!("efgh"));
915     let r = x(&b"abcdabcdefghijkl"[..]);
916     assert_eq!(r, Done(&b"ijkl"[..], &b"abcdabcd"[..]));
917 
918     println!("Done 1\n");
919 
920     let r2 = x(&b"abcdabcdefgh"[..]);
921     assert_eq!(r2, Done(&b""[..], &b"abcdabcd"[..]));
922 
923     println!("Done 2\n");
924     let r3 = x(&b"abcefg"[..]);
925     assert_eq!(r3,  Error(error_position!(ErrorKind::TakeUntilAndConsume, &b"abcefg"[..])));
926 
927     assert_eq!(
928       x(&b"ab"[..]),
929       Incomplete(Needed::Size(4))
930     );
931   }
932 
933   #[test]
934   fn take_until_either() {
935     named!(x, take_until_either!("!."));
936     assert_eq!(
937       x(&b"123!abc"[..]),
938       Done(&b"!abc"[..], &b"123"[..])
939     );
940   }
941 
942   #[test]
943   fn take_until_either_incomplete() {
944     named!(x, take_until_either!("!."));
945     assert_eq!(
946       x(&b"123"[..]),
947       Error(error_position!(ErrorKind::TakeUntilEither, &b"123"[..]))
948     );
949   }
950 
951   #[test]
952   fn take_until_either_and_consume() {
953     named!(x, take_until_either_and_consume!("!."));
954     assert_eq!(
955       x(&b"123.abc"[..]),
956       Done(&b"abc"[..], &b"123"[..])
957     );
958   }
959 
960 
961   #[test]
962   fn take_until_incomplete() {
963     named!(y, take_until!("end"));
964     assert_eq!(
965       y(&b"nd"[..]),
966       Incomplete(Needed::Size(3))
967     );
968     assert_eq!(
969       y(&b"123"[..]),
970       Error(error_position!(ErrorKind::TakeUntil, &b"123"[..]))
971     );
972   }
973 
974   #[test]
975   fn recognize() {
976     named!(x, recognize!(delimited!(tag!("<!--"), take!(5), tag!("-->"))));
977     let r = x(&b"<!-- abc --> aaa"[..]);
978     assert_eq!(r, Done(&b" aaa"[..], &b"<!-- abc -->"[..]));
979 
980     let empty = &b""[..];
981 
982     named!(ya, recognize!(alpha));
983     let ra = ya(&b"abc"[..]);
984     assert_eq!(ra, Done(empty, &b"abc"[..]));
985 
986     named!(yd, recognize!(digit));
987     let rd = yd(&b"123"[..]);
988     assert_eq!(rd, Done(empty, &b"123"[..]));
989 
990     named!(yhd, recognize!(hex_digit));
991     let rhd = yhd(&b"123abcDEF"[..]);
992     assert_eq!(rhd, Done(empty, &b"123abcDEF"[..]));
993 
994     named!(yod, recognize!(oct_digit));
995     let rod = yod(&b"1234567"[..]);
996     assert_eq!(rod, Done(empty, &b"1234567"[..]));
997 
998     named!(yan, recognize!(alphanumeric));
999     let ran = yan(&b"123abc"[..]);
1000     assert_eq!(ran, Done(empty, &b"123abc"[..]));
1001 
1002     named!(ys, recognize!(space));
1003     let rs = ys(&b" \t"[..]);
1004     assert_eq!(rs, Done(empty, &b" \t"[..]));
1005 
1006     named!(yms, recognize!(multispace));
1007     let rms = yms(&b" \t\r\n"[..]);
1008     assert_eq!(rms, Done(empty, &b" \t\r\n"[..]));
1009   }
1010 
1011   #[test]
1012   fn take_while() {
1013     use nom::is_alphabetic;
1014     named!(f, take_while!(is_alphabetic));
1015     let a = b"";
1016     let b = b"abcd";
1017     let c = b"abcd123";
1018     let d = b"123";
1019 
1020     assert_eq!(f(&a[..]), Done(&a[..], &a[..]));
1021     assert_eq!(f(&b[..]), Done(&a[..], &b[..]));
1022     assert_eq!(f(&c[..]), Done(&d[..], &b[..]));
1023     assert_eq!(f(&d[..]), Done(&d[..], &a[..]));
1024   }
1025 
1026   #[test]
1027   fn take_while1() {
1028     use nom::is_alphabetic;
1029     named!(f, take_while1!(is_alphabetic));
1030     let a = b"";
1031     let b = b"abcd";
1032     let c = b"abcd123";
1033     let d = b"123";
1034 
1035     assert_eq!(f(&a[..]), Incomplete(Needed::Size(1)));
1036     assert_eq!(f(&b[..]), Done(&a[..], &b[..]));
1037     assert_eq!(f(&c[..]), Done(&b"123"[..], &b[..]));
1038     assert_eq!(f(&d[..]), Error(error_position!(ErrorKind::TakeWhile1, &d[..])));
1039   }
1040 
1041   #[test]
1042   fn take_till() {
1043     use nom::is_alphabetic;
1044     named!(f, take_till!(is_alphabetic));
1045     let a = b"";
1046     let b = b"abcd";
1047     let c = b"123abcd";
1048     let d = b"123";
1049 
1050     assert_eq!(f(&a[..]), Done(&b""[..], &b""[..]));
1051     assert_eq!(f(&b[..]), Done(&b"abcd"[..], &b""[..]));
1052     assert_eq!(f(&c[..]), Done(&b"abcd"[..], &b"123"[..]));
1053     assert_eq!(f(&d[..]), Done(&b""[..], &b"123"[..]));
1054   }
1055 
1056   #[test]
1057   fn take_till1() {
1058     use nom::is_alphabetic;
1059     named!(f, take_till1!(is_alphabetic));
1060     let a = b"";
1061     let b = b"abcd";
1062     let c = b"123abcd";
1063     let d = b"123";
1064 
1065     assert_eq!(f(&a[..]), Incomplete(Needed::Size(1)));
1066     assert_eq!(f(&b[..]), Error(error_position!(ErrorKind::TakeTill1, &b""[..])));
1067     assert_eq!(f(&c[..]), Done(&b"abcd"[..], &b"123"[..]));
1068     assert_eq!(f(&d[..]), Done(&b""[..], &b"123"[..]));
1069   }
1070 
1071   #[cfg(feature = "nightly")]
1072   use test::Bencher;
1073 
1074   #[cfg(feature = "nightly")]
1075   #[bench]
1076   fn take_while_bench(b: &mut Bencher) {
1077     use nom::is_alphabetic;
1078     named!(f, take_while!(is_alphabetic));
1079     b.iter(|| {
1080       f(&b"abcdefghijklABCDEejfrfrjgro12aa"[..])
1081     });
1082   }
1083 
1084   #[test]
1085   fn recognize_take_while() {
1086     use nom::is_alphanumeric;
1087     named!(x, take_while!(is_alphanumeric));
1088     named!(y, recognize!(x));
1089     assert_eq!(x(&b"ab"[..]), Done(&[][..], &b"ab"[..]));
1090     println!("X: {:?}", x(&b"ab"[..]));
1091     assert_eq!(y(&b"ab"[..]), Done(&[][..], &b"ab"[..]));
1092   }
1093 
1094   #[test]
1095   fn length_bytes() {
1096     use nom::le_u8;
1097     named!(x, length_bytes!(le_u8));
1098     assert_eq!(x(b"\x02..>>"), Done(&b">>"[..], &b".."[..]));
1099     assert_eq!(x(b"\x02.."), Done(&[][..], &b".."[..]));
1100     assert_eq!(x(b"\x02."), Incomplete(Needed::Size(3)));
1101     assert_eq!(x(b"\x02"), Incomplete(Needed::Size(3)));
1102 
1103     named!(y, do_parse!(tag!("magic") >> b: length_bytes!(le_u8) >> (b)));
1104     assert_eq!(y(b"magic\x02..>>"), Done(&b">>"[..], &b".."[..]));
1105     assert_eq!(y(b"magic\x02.."), Done(&[][..], &b".."[..]));
1106     assert_eq!(y(b"magic\x02."), Incomplete(Needed::Size(8)));
1107     assert_eq!(y(b"magic\x02"), Incomplete(Needed::Size(8)));
1108   }
1109 
1110   #[test]
1111   fn case_insensitive() {
1112     named!(test, tag_no_case!("ABcd"));
1113     assert_eq!(test(&b"aBCdefgh"[..]), Done(&b"efgh"[..], &b"aBCd"[..]));
1114     assert_eq!(test(&b"abcdefgh"[..]), Done(&b"efgh"[..], &b"abcd"[..]));
1115     assert_eq!(test(&b"ABCDefgh"[..]), Done(&b"efgh"[..], &b"ABCD"[..]));
1116     assert_eq!(test(&b"ab"[..]), Incomplete(Needed::Size(4)));
1117     assert_eq!(test(&b"Hello"[..]), Error(error_code!(ErrorKind::Tag)));
1118     assert_eq!(test(&b"Hel"[..]), Error(error_code!(ErrorKind::Tag)));
1119 
1120     named!(test2<&str, &str>, tag_no_case!("ABcd"));
1121     assert_eq!(test2("aBCdefgh"), Done("efgh", "aBCd"));
1122     assert_eq!(test2("abcdefgh"), Done("efgh", "abcd"));
1123     assert_eq!(test2("ABCDefgh"), Done("efgh", "ABCD"));
1124     assert_eq!(test2("ab"), Incomplete(Needed::Size(4)));
1125     assert_eq!(test2("Hello"), Error(error_code!(ErrorKind::Tag)));
1126     assert_eq!(test2("Hel"), Error(error_code!(ErrorKind::Tag)));
1127   }
1128 
1129   #[test]
1130   fn tag_fixed_size_array() {
1131     named!(test, tag!([0x42]));
1132     named!(test2, tag!(&[0x42]));
1133     let input = vec!(0x42, 0x00);
1134     assert_eq!(test(&input), Done(&b"\x00"[..], &b"\x42"[..]));
1135     assert_eq!(test2(&input), Done(&b"\x00"[..], &b"\x42"[..]));
1136   }
1137 }
1138