1use crate::branch::alt;
6use crate::combinator::opt;
7use crate::error::ErrorKind;
8use crate::error::ParseError;
9use crate::internal::{Err, IResult};
10use crate::traits::{AsChar, FindToken, Input};
11use crate::traits::{Compare, CompareResult};
12use crate::Complete;
13use crate::Emit;
14use crate::OutputM;
15use crate::Parser;
16
17pub fn char<I, Error: ParseError<I>>(c: char) -> impl FnMut(I) -> IResult<I, char, Error>
34where
35 I: Input,
36 <I as Input>::Item: AsChar,
37{
38 let mut parser = super::char(c);
39 move |i: I| parser.process::<OutputM<Emit, Emit, Complete>>(i)
40}
41
42pub fn satisfy<F, I, Error: ParseError<I>>(predicate: F) -> impl FnMut(I) -> IResult<I, char, Error>
58where
59 I: Input,
60 <I as Input>::Item: AsChar,
61 F: Fn(char) -> bool,
62{
63 let mut parser = super::satisfy(predicate);
64 move |i: I| parser.process::<OutputM<Emit, Emit, Complete>>(i)
65}
66
67pub fn one_of<I, T, Error: ParseError<I>>(list: T) -> impl FnMut(I) -> IResult<I, char, Error>
80where
81 I: Input,
82 <I as Input>::Item: AsChar,
83 T: FindToken<char>,
84{
85 let mut parser = super::one_of(list);
86 move |i: I| parser.process::<OutputM<Emit, Emit, Complete>>(i)
87}
88
89pub fn none_of<I, T, Error: ParseError<I>>(list: T) -> impl FnMut(I) -> IResult<I, char, Error>
102where
103 I: Input,
104 <I as Input>::Item: AsChar,
105 T: FindToken<char>,
106{
107 let mut parser = super::none_of(list);
108 move |i: I| parser.process::<OutputM<Emit, Emit, Complete>>(i)
109}
110
111pub fn crlf<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
128where
129 T: Input,
130 T: Compare<&'static str>,
131{
132 match input.compare("\r\n") {
133 CompareResult::Ok => Ok(input.take_split(2)),
134 _ => {
135 let e: ErrorKind = ErrorKind::CrLf;
136 Err(Err::Error(E::from_error_kind(input, e)))
137 }
138 }
139}
140
141pub fn not_line_ending<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
162where
163 T: Input,
164 T: Compare<&'static str>,
165 <T as Input>::Item: AsChar,
166{
167 match input.position(|item| {
168 let c = item.as_char();
169 c == '\r' || c == '\n'
170 }) {
171 None => Ok(input.take_split(input.input_len())),
172 Some(index) => {
173 let mut it = input.take_from(index).iter_elements();
174 let nth = it.next().unwrap().as_char();
175 if nth == '\r' {
176 let sliced = input.take_from(index);
177 let comp = sliced.compare("\r\n");
178 match comp {
179 CompareResult::Ok => Ok(input.take_split(index)),
181 _ => {
182 let e: ErrorKind = ErrorKind::Tag;
183 Err(Err::Error(E::from_error_kind(input, e)))
184 }
185 }
186 } else {
187 Ok(input.take_split(index))
188 }
189 }
190 }
191}
192
193pub fn line_ending<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
210where
211 T: Input,
212 T: Compare<&'static str>,
213{
214 match input.compare("\n") {
215 CompareResult::Ok => Ok(input.take_split(1)),
216 CompareResult::Incomplete => Err(Err::Error(E::from_error_kind(input, ErrorKind::CrLf))),
217 CompareResult::Error => match input.compare("\r\n") {
218 CompareResult::Ok => Ok(input.take_split(2)),
219 _ => Err(Err::Error(E::from_error_kind(input, ErrorKind::CrLf))),
220 },
221 }
222}
223
224pub fn newline<I, Error: ParseError<I>>(input: I) -> IResult<I, char, Error>
241where
242 I: Input,
243 <I as Input>::Item: AsChar,
244{
245 char('\n')(input)
246}
247
248pub fn tab<I, Error: ParseError<I>>(input: I) -> IResult<I, char, Error>
265where
266 I: Input,
267 <I as Input>::Item: AsChar,
268{
269 char('\t')(input)
270}
271
272pub fn anychar<T, E: ParseError<T>>(input: T) -> IResult<T, char, E>
288where
289 T: Input,
290 <T as Input>::Item: AsChar,
291{
292 let mut it = input.iter_elements();
293 match it.next() {
294 None => Err(Err::Error(E::from_error_kind(input, ErrorKind::Eof))),
295 Some(c) => Ok((input.take_from(c.len()), c.as_char())),
296 }
297}
298
299pub fn alpha0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
317where
318 T: Input,
319 <T as Input>::Item: AsChar,
320{
321 input.split_at_position_complete(|item| !item.is_alpha())
322}
323
324pub fn alpha1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
342where
343 T: Input,
344 <T as Input>::Item: AsChar,
345{
346 input.split_at_position1_complete(|item| !item.is_alpha(), ErrorKind::Alpha)
347}
348
349pub fn digit0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
368where
369 T: Input,
370 <T as Input>::Item: AsChar,
371{
372 input.split_at_position_complete(|item| !item.is_dec_digit())
373}
374
375pub fn digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
411where
412 T: Input,
413 <T as Input>::Item: AsChar,
414{
415 input.split_at_position1_complete(|item| !item.is_dec_digit(), ErrorKind::Digit)
416}
417
418pub fn hex_digit0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
435where
436 T: Input,
437 <T as Input>::Item: AsChar,
438{
439 input.split_at_position_complete(|item| !item.is_hex_digit())
440}
441pub fn hex_digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
459where
460 T: Input,
461 <T as Input>::Item: AsChar,
462{
463 input.split_at_position1_complete(|item| !item.is_hex_digit(), ErrorKind::HexDigit)
464}
465
466pub fn oct_digit0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
484where
485 T: Input,
486 <T as Input>::Item: AsChar,
487{
488 input.split_at_position_complete(|item| !item.is_oct_digit())
489}
490
491pub fn oct_digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
509where
510 T: Input,
511 <T as Input>::Item: AsChar,
512{
513 input.split_at_position1_complete(|item| !item.is_oct_digit(), ErrorKind::OctDigit)
514}
515
516pub fn bin_digit0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
534where
535 T: Input,
536 <T as Input>::Item: AsChar,
537{
538 input.split_at_position_complete(|item| !item.is_bin_digit())
539}
540
541pub fn bin_digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
559where
560 T: Input,
561 <T as Input>::Item: AsChar,
562{
563 input.split_at_position1_complete(|item| !item.is_bin_digit(), ErrorKind::BinDigit)
564}
565
566pub fn alphanumeric0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
584where
585 T: Input,
586 <T as Input>::Item: AsChar,
587{
588 input.split_at_position_complete(|item| !item.is_alphanum())
589}
590
591pub fn alphanumeric1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
609where
610 T: Input,
611 <T as Input>::Item: AsChar,
612{
613 input.split_at_position1_complete(|item| !item.is_alphanum(), ErrorKind::AlphaNumeric)
614}
615
616pub fn space0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
634where
635 T: Input,
636 <T as Input>::Item: AsChar + Clone,
637{
638 input.split_at_position_complete(|item| {
639 let c = item.as_char();
640 !(c == ' ' || c == '\t')
641 })
642}
643
644pub fn space1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
662where
663 T: Input,
664 <T as Input>::Item: AsChar,
665{
666 input.split_at_position1_complete(
667 |item| {
668 let c = item.as_char();
669 !(c == ' ' || c == '\t')
670 },
671 ErrorKind::Space,
672 )
673}
674
675pub fn multispace0<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
693where
694 T: Input,
695 <T as Input>::Item: AsChar,
696{
697 input.split_at_position_complete(|item| {
698 let c = item.as_char();
699 !(c == ' ' || c == '\t' || c == '\r' || c == '\n')
700 })
701}
702
703pub fn multispace1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
721where
722 T: Input,
723 <T as Input>::Item: AsChar,
724{
725 input.split_at_position1_complete(
726 |item| {
727 let c = item.as_char();
728 !(c == ' ' || c == '\t' || c == '\r' || c == '\n')
729 },
730 ErrorKind::MultiSpace,
731 )
732}
733
734pub(crate) fn sign<T, E: ParseError<T>>(input: T) -> IResult<T, bool, E>
735where
736 T: Clone + Input,
737 T: for<'a> Compare<&'a [u8]>,
738{
739 use crate::bytes::complete::tag;
740 use crate::combinator::value;
741
742 let (i, opt_sign) = opt(alt((
743 value(false, tag(&b"-"[..])),
744 value(true, tag(&b"+"[..])),
745 )))
746 .parse(input)?;
747 let sign = opt_sign.unwrap_or(true);
748
749 Ok((i, sign))
750}
751
752#[doc(hidden)]
753macro_rules! ints {
754 ($($t:tt)+) => {
755 $(
756 pub fn $t<T, E: ParseError<T>>(input: T) -> IResult<T, $t, E>
760 where
761 T: Input + Clone,
762 <T as Input>::Item: AsChar,
763 T: for <'a> Compare<&'a[u8]>,
764 {
765 let (i, sign) = sign(input.clone())?;
766
767 if i.input_len() == 0 {
768 return Err(Err::Error(E::from_error_kind(input, ErrorKind::Digit)));
769 }
770
771 let mut value: $t = 0;
772 if sign {
773 let mut pos = 0;
774 for c in i.iter_elements() {
775 match c.as_char().to_digit(10) {
776 None => {
777 if pos == 0 {
778 return Err(Err::Error(E::from_error_kind(input, ErrorKind::Digit)));
779 } else {
780 return Ok((i.take_from(pos), value));
781 }
782 },
783 Some(d) => match value.checked_mul(10).and_then(|v| v.checked_add(d as $t)) {
784 None => return Err(Err::Error(E::from_error_kind(input, ErrorKind::Digit))),
785 Some(v) => {
786 pos += c.len();
787 value = v;
788 },
789 }
790 }
791 }
792 } else {
793 let mut pos = 0;
794 for c in i.iter_elements() {
795 match c.as_char().to_digit(10) {
796 None => {
797 if pos == 0 {
798 return Err(Err::Error(E::from_error_kind(input, ErrorKind::Digit)));
799 } else {
800 return Ok((i.take_from(pos), value));
801 }
802 },
803 Some(d) => match value.checked_mul(10).and_then(|v| v.checked_sub(d as $t)) {
804 None => return Err(Err::Error(E::from_error_kind(input, ErrorKind::Digit))),
805 Some(v) => {
806 pos += c.len();
807 value = v;
808 },
809 }
810 }
811 }
812 }
813
814 Ok((i.take_from(i.input_len()), value))
815 }
816 )+
817 }
818}
819
820ints! { i8 i16 i32 i64 i128 isize }
821
822#[doc(hidden)]
823macro_rules! uints {
824 ($($t:tt)+) => {
825 $(
826 pub fn $t<T, E: ParseError<T>>(input: T) -> IResult<T, $t, E>
830 where
831 T: Input ,
832 <T as Input>::Item: AsChar,
833 {
834 let i = input;
835
836 if i.input_len() == 0 {
837 return Err(Err::Error(E::from_error_kind(i, ErrorKind::Digit)));
838 }
839
840 let mut value: $t = 0;
841 let mut pos = 0;
842 for c in i.iter_elements() {
843 match c.as_char().to_digit(10) {
844 None => {
845 if pos == 0 {
846 return Err(Err::Error(E::from_error_kind(i, ErrorKind::Digit)));
847 } else {
848 return Ok((i.take_from(pos), value));
849 }
850 },
851 Some(d) => match value.checked_mul(10).and_then(|v| v.checked_add(d as $t)) {
852 None => return Err(Err::Error(E::from_error_kind(i, ErrorKind::Digit))),
853 Some(v) => {
854 pos += c.len();
855 value = v;
856 },
857 }
858 }
859 }
860
861 Ok((i.take_from(i.input_len()), value))
862 }
863 )+
864 }
865}
866
867uints! { u8 u16 u32 u64 u128 usize }
868
869#[cfg(test)]
870mod tests {
871 use super::*;
872 use crate::internal::Err;
873 use crate::traits::ParseTo;
874 use crate::Parser;
875 use proptest::prelude::*;
876
877 macro_rules! assert_parse(
878 ($left: expr, $right: expr) => {
879 let res: $crate::IResult<_, _, (_, ErrorKind)> = $left;
880 assert_eq!(res, $right);
881 };
882 );
883
884 #[test]
885 fn character() {
886 let empty: &[u8] = b"";
887 let a: &[u8] = b"abcd";
888 let b: &[u8] = b"1234";
889 let c: &[u8] = b"a123";
890 let d: &[u8] = "azé12".as_bytes();
891 let e: &[u8] = b" ";
892 let f: &[u8] = b" ;";
893 assert_parse!(alpha1(a), Ok((empty, a)));
895 assert_eq!(alpha1(b), Err(Err::Error((b, ErrorKind::Alpha))));
896 assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], &b"a"[..])));
897 assert_eq!(
898 alpha1::<_, (_, ErrorKind)>(d),
899 Ok(("é12".as_bytes(), &b"az"[..]))
900 );
901 assert_eq!(digit1(a), Err(Err::Error((a, ErrorKind::Digit))));
902 assert_eq!(digit1::<_, (_, ErrorKind)>(b), Ok((empty, b)));
903 assert_eq!(digit1(c), Err(Err::Error((c, ErrorKind::Digit))));
904 assert_eq!(digit1(d), Err(Err::Error((d, ErrorKind::Digit))));
905 assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Ok((empty, a)));
906 assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b)));
907 assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Ok((empty, c)));
908 assert_eq!(
909 hex_digit1::<_, (_, ErrorKind)>(d),
910 Ok(("zé12".as_bytes(), &b"a"[..]))
911 );
912 assert_eq!(hex_digit1(e), Err(Err::Error((e, ErrorKind::HexDigit))));
913 assert_eq!(oct_digit1(a), Err(Err::Error((a, ErrorKind::OctDigit))));
914 assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b)));
915 assert_eq!(oct_digit1(c), Err(Err::Error((c, ErrorKind::OctDigit))));
916 assert_eq!(oct_digit1(d), Err(Err::Error((d, ErrorKind::OctDigit))));
917 assert_eq!(bin_digit1(a), Err(Err::Error((a, ErrorKind::BinDigit))));
918 assert_eq!(
919 bin_digit1::<_, (_, ErrorKind)>(b),
920 Ok((&b"234"[..], &b"1"[..]))
921 );
922 assert_eq!(bin_digit1(c), Err(Err::Error((c, ErrorKind::BinDigit))));
923 assert_eq!(bin_digit1(d), Err(Err::Error((d, ErrorKind::BinDigit))));
924 assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(a), Ok((empty, a)));
925 assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(c), Ok((empty, c)));
927 assert_eq!(
928 alphanumeric1::<_, (_, ErrorKind)>(d),
929 Ok(("é12".as_bytes(), &b"az"[..]))
930 );
931 assert_eq!(space1::<_, (_, ErrorKind)>(e), Ok((empty, e)));
932 assert_eq!(space1::<_, (_, ErrorKind)>(f), Ok((&b";"[..], &b" "[..])));
933 }
934
935 #[cfg(feature = "alloc")]
936 #[test]
937 fn character_s() {
938 let empty = "";
939 let a = "abcd";
940 let b = "1234";
941 let c = "a123";
942 let d = "azé12";
943 let e = " ";
944 assert_eq!(alpha1::<_, (_, ErrorKind)>(a), Ok((empty, a)));
945 assert_eq!(alpha1(b), Err(Err::Error((b, ErrorKind::Alpha))));
946 assert_eq!(alpha1::<_, (_, ErrorKind)>(c), Ok((&c[1..], "a")));
947 assert_eq!(alpha1::<_, (_, ErrorKind)>(d), Ok(("é12", "az")));
948 assert_eq!(digit1(a), Err(Err::Error((a, ErrorKind::Digit))));
949 assert_eq!(digit1::<_, (_, ErrorKind)>(b), Ok((empty, b)));
950 assert_eq!(digit1(c), Err(Err::Error((c, ErrorKind::Digit))));
951 assert_eq!(digit1(d), Err(Err::Error((d, ErrorKind::Digit))));
952 assert_eq!(hex_digit1::<_, (_, ErrorKind)>(a), Ok((empty, a)));
953 assert_eq!(hex_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b)));
954 assert_eq!(hex_digit1::<_, (_, ErrorKind)>(c), Ok((empty, c)));
955 assert_eq!(hex_digit1::<_, (_, ErrorKind)>(d), Ok(("zé12", "a")));
956 assert_eq!(hex_digit1(e), Err(Err::Error((e, ErrorKind::HexDigit))));
957 assert_eq!(oct_digit1(a), Err(Err::Error((a, ErrorKind::OctDigit))));
958 assert_eq!(oct_digit1::<_, (_, ErrorKind)>(b), Ok((empty, b)));
959 assert_eq!(oct_digit1(c), Err(Err::Error((c, ErrorKind::OctDigit))));
960 assert_eq!(oct_digit1(d), Err(Err::Error((d, ErrorKind::OctDigit))));
961 assert_eq!(bin_digit1(a), Err(Err::Error((a, ErrorKind::BinDigit))));
962 assert_eq!(bin_digit1::<_, (_, ErrorKind)>(b), Ok(("234", "1")));
963 assert_eq!(bin_digit1(c), Err(Err::Error((c, ErrorKind::BinDigit))));
964 assert_eq!(bin_digit1(d), Err(Err::Error((d, ErrorKind::BinDigit))));
965 assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(a), Ok((empty, a)));
966 assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(c), Ok((empty, c)));
968 assert_eq!(alphanumeric1::<_, (_, ErrorKind)>(d), Ok(("é12", "az")));
969 assert_eq!(space1::<_, (_, ErrorKind)>(e), Ok((empty, e)));
970 }
971
972 use crate::traits::Offset;
973 #[test]
974 fn offset() {
975 let a = &b"abcd;"[..];
976 let b = &b"1234;"[..];
977 let c = &b"a123;"[..];
978 let d = &b" \t;"[..];
979 let e = &b" \t\r\n;"[..];
980 let f = &b"123abcDEF;"[..];
981
982 match alpha1::<_, (_, ErrorKind)>(a) {
983 Ok((i, _)) => {
984 assert_eq!(a.offset(i) + i.len(), a.len());
985 }
986 _ => panic!("wrong return type in offset test for alpha"),
987 }
988 match digit1::<_, (_, ErrorKind)>(b) {
989 Ok((i, _)) => {
990 assert_eq!(b.offset(i) + i.len(), b.len());
991 }
992 _ => panic!("wrong return type in offset test for digit"),
993 }
994 match alphanumeric1::<_, (_, ErrorKind)>(c) {
995 Ok((i, _)) => {
996 assert_eq!(c.offset(i) + i.len(), c.len());
997 }
998 _ => panic!("wrong return type in offset test for alphanumeric"),
999 }
1000 match space1::<_, (_, ErrorKind)>(d) {
1001 Ok((i, _)) => {
1002 assert_eq!(d.offset(i) + i.len(), d.len());
1003 }
1004 _ => panic!("wrong return type in offset test for space"),
1005 }
1006 match multispace1::<_, (_, ErrorKind)>(e) {
1007 Ok((i, _)) => {
1008 assert_eq!(e.offset(i) + i.len(), e.len());
1009 }
1010 _ => panic!("wrong return type in offset test for multispace"),
1011 }
1012 match hex_digit1::<_, (_, ErrorKind)>(f) {
1013 Ok((i, _)) => {
1014 assert_eq!(f.offset(i) + i.len(), f.len());
1015 }
1016 _ => panic!("wrong return type in offset test for hex_digit"),
1017 }
1018 match oct_digit1::<_, (_, ErrorKind)>(f) {
1019 Ok((i, _)) => {
1020 assert_eq!(f.offset(i) + i.len(), f.len());
1021 }
1022 _ => panic!("wrong return type in offset test for oct_digit"),
1023 }
1024 match bin_digit1::<_, (_, ErrorKind)>(f) {
1025 Ok((i, _)) => {
1026 assert_eq!(f.offset(i) + i.len(), f.len());
1027 }
1028 _ => panic!("wrong return type in offset test for bin_digit"),
1029 }
1030 }
1031
1032 #[test]
1033 fn is_not_line_ending_bytes() {
1034 let a: &[u8] = b"ab12cd\nefgh";
1035 assert_eq!(
1036 not_line_ending::<_, (_, ErrorKind)>(a),
1037 Ok((&b"\nefgh"[..], &b"ab12cd"[..]))
1038 );
1039
1040 let b: &[u8] = b"ab12cd\nefgh\nijkl";
1041 assert_eq!(
1042 not_line_ending::<_, (_, ErrorKind)>(b),
1043 Ok((&b"\nefgh\nijkl"[..], &b"ab12cd"[..]))
1044 );
1045
1046 let c: &[u8] = b"ab12cd\r\nefgh\nijkl";
1047 assert_eq!(
1048 not_line_ending::<_, (_, ErrorKind)>(c),
1049 Ok((&b"\r\nefgh\nijkl"[..], &b"ab12cd"[..]))
1050 );
1051
1052 let d: &[u8] = b"ab12cd";
1053 assert_eq!(not_line_ending::<_, (_, ErrorKind)>(d), Ok((&[][..], d)));
1054 }
1055
1056 #[test]
1057 fn is_not_line_ending_str() {
1058 let f = "βèƒôřè\rÂßÇáƒƭèř";
1076 assert_eq!(not_line_ending(f), Err(Err::Error((f, ErrorKind::Tag))));
1077
1078 let g2: &str = "ab12cd";
1079 assert_eq!(not_line_ending::<_, (_, ErrorKind)>(g2), Ok(("", g2)));
1080 }
1081
1082 #[test]
1083 fn hex_digit_test() {
1084 let i = &b"0123456789abcdefABCDEF;"[..];
1085 assert_parse!(hex_digit1(i), Ok((&b";"[..], &i[..i.len() - 1])));
1086
1087 let i = &b"g"[..];
1088 assert_parse!(
1089 hex_digit1(i),
1090 Err(Err::Error(error_position!(i, ErrorKind::HexDigit)))
1091 );
1092
1093 let i = &b"G"[..];
1094 assert_parse!(
1095 hex_digit1(i),
1096 Err(Err::Error(error_position!(i, ErrorKind::HexDigit)))
1097 );
1098
1099 assert!(AsChar::is_hex_digit(b'0'));
1100 assert!(AsChar::is_hex_digit(b'9'));
1101 assert!(AsChar::is_hex_digit(b'a'));
1102 assert!(AsChar::is_hex_digit(b'f'));
1103 assert!(AsChar::is_hex_digit(b'A'));
1104 assert!(AsChar::is_hex_digit(b'F'));
1105 assert!(!AsChar::is_hex_digit(b'g'));
1106 assert!(!AsChar::is_hex_digit(b'G'));
1107 assert!(!AsChar::is_hex_digit(b'/'));
1108 assert!(!AsChar::is_hex_digit(b':'));
1109 assert!(!AsChar::is_hex_digit(b'@'));
1110 assert!(!AsChar::is_hex_digit(b'\x60'));
1111 }
1112
1113 #[test]
1114 fn oct_digit_test() {
1115 let i = &b"01234567;"[..];
1116 assert_parse!(oct_digit1(i), Ok((&b";"[..], &i[..i.len() - 1])));
1117
1118 let i = &b"8"[..];
1119 assert_parse!(
1120 oct_digit1(i),
1121 Err(Err::Error(error_position!(i, ErrorKind::OctDigit)))
1122 );
1123
1124 assert!(AsChar::is_oct_digit(b'0'));
1125 assert!(AsChar::is_oct_digit(b'7'));
1126 assert!(!AsChar::is_oct_digit(b'8'));
1127 assert!(!AsChar::is_oct_digit(b'9'));
1128 assert!(!AsChar::is_oct_digit(b'a'));
1129 assert!(!AsChar::is_oct_digit(b'A'));
1130 assert!(!AsChar::is_oct_digit(b'/'));
1131 assert!(!AsChar::is_oct_digit(b':'));
1132 assert!(!AsChar::is_oct_digit(b'@'));
1133 assert!(!AsChar::is_oct_digit(b'\x60'));
1134 }
1135
1136 #[test]
1137 fn bin_digit_test() {
1138 let i = &b"101010;"[..];
1139 assert_parse!(bin_digit1(i), Ok((&b";"[..], &i[..i.len() - 1])));
1140
1141 let i = &b"2"[..];
1142 assert_parse!(
1143 bin_digit1(i),
1144 Err(Err::Error(error_position!(i, ErrorKind::BinDigit)))
1145 );
1146
1147 assert!(crate::character::is_bin_digit(b'0'));
1148 assert!(crate::character::is_bin_digit(b'1'));
1149 assert!(!crate::character::is_bin_digit(b'8'));
1150 assert!(!crate::character::is_bin_digit(b'9'));
1151 assert!(!crate::character::is_bin_digit(b'a'));
1152 assert!(!crate::character::is_bin_digit(b'A'));
1153 assert!(!crate::character::is_bin_digit(b'/'));
1154 assert!(!crate::character::is_bin_digit(b':'));
1155 assert!(!crate::character::is_bin_digit(b'@'));
1156 assert!(!crate::character::is_bin_digit(b'\x60'));
1157 }
1158
1159 #[test]
1160 fn full_line_windows() {
1161 use crate::sequence::pair;
1162 fn take_full_line(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
1163 pair(not_line_ending, line_ending).parse(i)
1164 }
1165 let input = b"abc\r\n";
1166 let output = take_full_line(input);
1167 assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\r\n"[..]))));
1168 }
1169
1170 #[test]
1171 fn full_line_unix() {
1172 use crate::sequence::pair;
1173 fn take_full_line(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
1174 pair(not_line_ending, line_ending).parse(i)
1175 }
1176 let input = b"abc\n";
1177 let output = take_full_line(input);
1178 assert_eq!(output, Ok((&b""[..], (&b"abc"[..], &b"\n"[..]))));
1179 }
1180
1181 #[test]
1182 fn check_windows_lineending() {
1183 let input = b"\r\n";
1184 let output = line_ending(&input[..]);
1185 assert_parse!(output, Ok((&b""[..], &b"\r\n"[..])));
1186 }
1187
1188 #[test]
1189 fn check_unix_lineending() {
1190 let input = b"\n";
1191 let output = line_ending(&input[..]);
1192 assert_parse!(output, Ok((&b""[..], &b"\n"[..])));
1193 }
1194
1195 #[test]
1196 fn cr_lf() {
1197 assert_parse!(crlf(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..])));
1198 assert_parse!(
1199 crlf(&b"\r"[..]),
1200 Err(Err::Error(error_position!(&b"\r"[..], ErrorKind::CrLf)))
1201 );
1202 assert_parse!(
1203 crlf(&b"\ra"[..]),
1204 Err(Err::Error(error_position!(&b"\ra"[..], ErrorKind::CrLf)))
1205 );
1206
1207 assert_parse!(crlf("\r\na"), Ok(("a", "\r\n")));
1208 assert_parse!(
1209 crlf("\r"),
1210 Err(Err::Error(error_position!("\r", ErrorKind::CrLf)))
1211 );
1212 assert_parse!(
1213 crlf("\ra"),
1214 Err(Err::Error(error_position!("\ra", ErrorKind::CrLf)))
1215 );
1216 }
1217
1218 #[test]
1219 fn end_of_line() {
1220 assert_parse!(line_ending(&b"\na"[..]), Ok((&b"a"[..], &b"\n"[..])));
1221 assert_parse!(line_ending(&b"\r\na"[..]), Ok((&b"a"[..], &b"\r\n"[..])));
1222 assert_parse!(
1223 line_ending(&b"\r"[..]),
1224 Err(Err::Error(error_position!(&b"\r"[..], ErrorKind::CrLf)))
1225 );
1226 assert_parse!(
1227 line_ending(&b"\ra"[..]),
1228 Err(Err::Error(error_position!(&b"\ra"[..], ErrorKind::CrLf)))
1229 );
1230
1231 assert_parse!(line_ending("\na"), Ok(("a", "\n")));
1232 assert_parse!(line_ending("\r\na"), Ok(("a", "\r\n")));
1233 assert_parse!(
1234 line_ending("\r"),
1235 Err(Err::Error(error_position!("\r", ErrorKind::CrLf)))
1236 );
1237 assert_parse!(
1238 line_ending("\ra"),
1239 Err(Err::Error(error_position!("\ra", ErrorKind::CrLf)))
1240 );
1241 }
1242
1243 fn digit_to_i16(input: &str) -> IResult<&str, i16> {
1244 let i = input;
1245 let (i, opt_sign) = opt(alt((char('+'), char('-')))).parse(i)?;
1246 let sign = match opt_sign {
1247 Some('+') => true,
1248 Some('-') => false,
1249 _ => true,
1250 };
1251
1252 let (i, s) = match digit1::<_, crate::error::Error<_>>(i) {
1253 Ok((i, s)) => (i, s),
1254 Err(_) => {
1255 return Err(Err::Error(crate::error::Error::from_error_kind(
1256 input,
1257 ErrorKind::Digit,
1258 )))
1259 }
1260 };
1261
1262 match s.parse_to() {
1263 Some(n) => {
1264 if sign {
1265 Ok((i, n))
1266 } else {
1267 Ok((i, -n))
1268 }
1269 }
1270 None => Err(Err::Error(crate::error::Error::from_error_kind(
1271 i,
1272 ErrorKind::Digit,
1273 ))),
1274 }
1275 }
1276
1277 fn digit_to_u32(i: &str) -> IResult<&str, u32> {
1278 let (i, s) = digit1(i)?;
1279 match s.parse_to() {
1280 Some(n) => Ok((i, n)),
1281 None => Err(Err::Error(crate::error::Error::from_error_kind(
1282 i,
1283 ErrorKind::Digit,
1284 ))),
1285 }
1286 }
1287
1288 proptest! {
1289 #[test]
1290 fn ints(s in "\\PC*") {
1291 let res1 = digit_to_i16(&s);
1292 let res2 = i16(s.as_str());
1293 assert_eq!(res1, res2);
1294 }
1295
1296 #[test]
1297 fn uints(s in "\\PC*") {
1298 let res1 = digit_to_u32(&s);
1299 let res2 = u32(s.as_str());
1300 assert_eq!(res1, res2);
1301 }
1302 }
1303}