/rust/registry/src/index.crates.io-1949cf8c6b5b557f/nom-7.1.3/src/bytes/complete.rs
Line | Count | Source |
1 | | //! Parsers recognizing bytes streams, complete input version |
2 | | |
3 | | use crate::error::ErrorKind; |
4 | | use crate::error::ParseError; |
5 | | use crate::internal::{Err, IResult, Parser}; |
6 | | use crate::lib::std::ops::RangeFrom; |
7 | | use crate::lib::std::result::Result::*; |
8 | | use crate::traits::{ |
9 | | Compare, CompareResult, FindSubstring, FindToken, InputIter, InputLength, InputTake, |
10 | | InputTakeAtPosition, Slice, ToUsize, |
11 | | }; |
12 | | |
13 | | /// Recognizes a pattern |
14 | | /// |
15 | | /// The input data will be compared to the tag combinator's argument and will return the part of |
16 | | /// the input that matches the argument |
17 | | /// |
18 | | /// It will return `Err(Err::Error((_, ErrorKind::Tag)))` if the input doesn't match the pattern |
19 | | /// # Example |
20 | | /// ```rust |
21 | | /// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult}; |
22 | | /// use nom::bytes::complete::tag; |
23 | | /// |
24 | | /// fn parser(s: &str) -> IResult<&str, &str> { |
25 | | /// tag("Hello")(s) |
26 | | /// } |
27 | | /// |
28 | | /// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); |
29 | | /// assert_eq!(parser("Something"), Err(Err::Error(Error::new("Something", ErrorKind::Tag)))); |
30 | | /// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Tag)))); |
31 | | /// ``` |
32 | 2.73M | pub fn tag<T, Input, Error: ParseError<Input>>( |
33 | 2.73M | tag: T, |
34 | 2.73M | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
35 | 2.73M | where |
36 | 2.73M | Input: InputTake + Compare<T>, |
37 | 2.73M | T: InputLength + Clone, |
38 | | { |
39 | 2.01M | move |i: Input| { |
40 | 2.01M | let tag_len = tag.input_len(); |
41 | 2.01M | let t = tag.clone(); |
42 | 2.01M | let res: IResult<_, _, Error> = match i.compare(t) { |
43 | 851k | CompareResult::Ok => Ok(i.take_split(tag_len)), |
44 | | _ => { |
45 | 1.15M | let e: ErrorKind = ErrorKind::Tag; |
46 | 1.15M | Err(Err::Error(Error::from_error_kind(i, e))) |
47 | | } |
48 | | }; |
49 | 2.01M | res |
50 | 2.01M | } nom::bytes::complete::tag::<&str, &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 39 | 3.03k | move |i: Input| { | 40 | 3.03k | let tag_len = tag.input_len(); | 41 | 3.03k | let t = tag.clone(); | 42 | 3.03k | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | 243 | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | 2.79k | let e: ErrorKind = ErrorKind::Tag; | 46 | 2.79k | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | 3.03k | res | 50 | 3.03k | } |
Unexecuted instantiation: nom::bytes::complete::tag::<_, _, _>::{closure#0}nom::bytes::complete::tag::<&str, &str, suricata::detect::error::RuleParseError<&str>>::{closure#0}Line | Count | Source | 39 | 105k | move |i: Input| { | 40 | 105k | let tag_len = tag.input_len(); | 41 | 105k | let t = tag.clone(); | 42 | 105k | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | 89.6k | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | 16.3k | let e: ErrorKind = ErrorKind::Tag; | 46 | 16.3k | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | 105k | res | 50 | 105k | } |
nom::bytes::complete::tag::<&str, &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 39 | 1.04M | move |i: Input| { | 40 | 1.04M | let tag_len = tag.input_len(); | 41 | 1.04M | let t = tag.clone(); | 42 | 1.04M | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | 610k | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | 433k | let e: ErrorKind = ErrorKind::Tag; | 46 | 433k | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | 1.04M | res | 50 | 1.04M | } |
nom::bytes::complete::tag::<&str, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 39 | 858k | move |i: Input| { | 40 | 858k | let tag_len = tag.input_len(); | 41 | 858k | let t = tag.clone(); | 42 | 858k | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | 151k | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | 706k | let e: ErrorKind = ErrorKind::Tag; | 46 | 706k | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | 858k | res | 50 | 858k | } |
Unexecuted instantiation: nom::bytes::complete::tag::<_, _, _>::{closure#0} |
51 | 2.73M | } nom::bytes::complete::tag::<&str, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 32 | 3.03k | pub fn tag<T, Input, Error: ParseError<Input>>( | 33 | 3.03k | tag: T, | 34 | 3.03k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 35 | 3.03k | where | 36 | 3.03k | Input: InputTake + Compare<T>, | 37 | 3.03k | T: InputLength + Clone, | 38 | | { | 39 | | move |i: Input| { | 40 | | let tag_len = tag.input_len(); | 41 | | let t = tag.clone(); | 42 | | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | | let e: ErrorKind = ErrorKind::Tag; | 46 | | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | | res | 50 | | } | 51 | 3.03k | } |
Unexecuted instantiation: nom::bytes::complete::tag::<_, _, _> nom::bytes::complete::tag::<&str, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 32 | 1.54M | pub fn tag<T, Input, Error: ParseError<Input>>( | 33 | 1.54M | tag: T, | 34 | 1.54M | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 35 | 1.54M | where | 36 | 1.54M | Input: InputTake + Compare<T>, | 37 | 1.54M | T: InputLength + Clone, | 38 | | { | 39 | | move |i: Input| { | 40 | | let tag_len = tag.input_len(); | 41 | | let t = tag.clone(); | 42 | | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | | let e: ErrorKind = ErrorKind::Tag; | 46 | | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | | res | 50 | | } | 51 | 1.54M | } |
nom::bytes::complete::tag::<&str, &str, suricata::detect::error::RuleParseError<&str>> Line | Count | Source | 32 | 18.5k | pub fn tag<T, Input, Error: ParseError<Input>>( | 33 | 18.5k | tag: T, | 34 | 18.5k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 35 | 18.5k | where | 36 | 18.5k | Input: InputTake + Compare<T>, | 37 | 18.5k | T: InputLength + Clone, | 38 | | { | 39 | | move |i: Input| { | 40 | | let tag_len = tag.input_len(); | 41 | | let t = tag.clone(); | 42 | | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | | let e: ErrorKind = ErrorKind::Tag; | 46 | | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | | res | 50 | | } | 51 | 18.5k | } |
nom::bytes::complete::tag::<&str, &str, nom::error::Error<&str>> Line | Count | Source | 32 | 1.16M | pub fn tag<T, Input, Error: ParseError<Input>>( | 33 | 1.16M | tag: T, | 34 | 1.16M | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 35 | 1.16M | where | 36 | 1.16M | Input: InputTake + Compare<T>, | 37 | 1.16M | T: InputLength + Clone, | 38 | | { | 39 | | move |i: Input| { | 40 | | let tag_len = tag.input_len(); | 41 | | let t = tag.clone(); | 42 | | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | | let e: ErrorKind = ErrorKind::Tag; | 46 | | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | | res | 50 | | } | 51 | 1.16M | } |
Unexecuted instantiation: nom::bytes::complete::tag::<_, _, _> |
52 | | |
53 | | /// Recognizes a case insensitive pattern. |
54 | | /// |
55 | | /// The input data will be compared to the tag combinator's argument and will return the part of |
56 | | /// the input that matches the argument with no regard to case. |
57 | | /// |
58 | | /// It will return `Err(Err::Error((_, ErrorKind::Tag)))` if the input doesn't match the pattern. |
59 | | /// # Example |
60 | | /// ```rust |
61 | | /// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult}; |
62 | | /// use nom::bytes::complete::tag_no_case; |
63 | | /// |
64 | | /// fn parser(s: &str) -> IResult<&str, &str> { |
65 | | /// tag_no_case("hello")(s) |
66 | | /// } |
67 | | /// |
68 | | /// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello"))); |
69 | | /// assert_eq!(parser("hello, World!"), Ok((", World!", "hello"))); |
70 | | /// assert_eq!(parser("HeLlO, World!"), Ok((", World!", "HeLlO"))); |
71 | | /// assert_eq!(parser("Something"), Err(Err::Error(Error::new("Something", ErrorKind::Tag)))); |
72 | | /// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Tag)))); |
73 | | /// ``` |
74 | 245k | pub fn tag_no_case<T, Input, Error: ParseError<Input>>( |
75 | 245k | tag: T, |
76 | 245k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
77 | 245k | where |
78 | 245k | Input: InputTake + Compare<T>, |
79 | 245k | T: InputLength + Clone, |
80 | | { |
81 | 244k | move |i: Input| { |
82 | 244k | let tag_len = tag.input_len(); |
83 | 244k | let t = tag.clone(); |
84 | | |
85 | 244k | let res: IResult<_, _, Error> = match (i).compare_no_case(t) { |
86 | 1.69k | CompareResult::Ok => Ok(i.take_split(tag_len)), |
87 | | _ => { |
88 | 242k | let e: ErrorKind = ErrorKind::Tag; |
89 | 242k | Err(Err::Error(Error::from_error_kind(i, e))) |
90 | | } |
91 | | }; |
92 | 244k | res |
93 | 244k | } Unexecuted instantiation: nom::bytes::complete::tag_no_case::<_, _, _>::{closure#0}nom::bytes::complete::tag_no_case::<&str, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 81 | 244k | move |i: Input| { | 82 | 244k | let tag_len = tag.input_len(); | 83 | 244k | let t = tag.clone(); | 84 | | | 85 | 244k | let res: IResult<_, _, Error> = match (i).compare_no_case(t) { | 86 | 1.69k | CompareResult::Ok => Ok(i.take_split(tag_len)), | 87 | | _ => { | 88 | 242k | let e: ErrorKind = ErrorKind::Tag; | 89 | 242k | Err(Err::Error(Error::from_error_kind(i, e))) | 90 | | } | 91 | | }; | 92 | 244k | res | 93 | 244k | } |
Unexecuted instantiation: nom::bytes::complete::tag_no_case::<_, _, _>::{closure#0} |
94 | 245k | } Unexecuted instantiation: nom::bytes::complete::tag_no_case::<_, _, _> nom::bytes::complete::tag_no_case::<&str, &str, nom::error::Error<&str>> Line | Count | Source | 74 | 245k | pub fn tag_no_case<T, Input, Error: ParseError<Input>>( | 75 | 245k | tag: T, | 76 | 245k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 77 | 245k | where | 78 | 245k | Input: InputTake + Compare<T>, | 79 | 245k | T: InputLength + Clone, | 80 | | { | 81 | | move |i: Input| { | 82 | | let tag_len = tag.input_len(); | 83 | | let t = tag.clone(); | 84 | | | 85 | | let res: IResult<_, _, Error> = match (i).compare_no_case(t) { | 86 | | CompareResult::Ok => Ok(i.take_split(tag_len)), | 87 | | _ => { | 88 | | let e: ErrorKind = ErrorKind::Tag; | 89 | | Err(Err::Error(Error::from_error_kind(i, e))) | 90 | | } | 91 | | }; | 92 | | res | 93 | | } | 94 | 245k | } |
Unexecuted instantiation: nom::bytes::complete::tag_no_case::<_, _, _> |
95 | | |
96 | | /// Parse till certain characters are met. |
97 | | /// |
98 | | /// The parser will return the longest slice till one of the characters of the combinator's argument are met. |
99 | | /// |
100 | | /// It doesn't consume the matched character. |
101 | | /// |
102 | | /// It will return a `Err::Error(("", ErrorKind::IsNot))` if the pattern wasn't met. |
103 | | /// # Example |
104 | | /// ```rust |
105 | | /// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult}; |
106 | | /// use nom::bytes::complete::is_not; |
107 | | /// |
108 | | /// fn not_space(s: &str) -> IResult<&str, &str> { |
109 | | /// is_not(" \t\r\n")(s) |
110 | | /// } |
111 | | /// |
112 | | /// assert_eq!(not_space("Hello, World!"), Ok((" World!", "Hello,"))); |
113 | | /// assert_eq!(not_space("Sometimes\t"), Ok(("\t", "Sometimes"))); |
114 | | /// assert_eq!(not_space("Nospace"), Ok(("", "Nospace"))); |
115 | | /// assert_eq!(not_space(""), Err(Err::Error(Error::new("", ErrorKind::IsNot)))); |
116 | | /// ``` |
117 | 557k | pub fn is_not<T, Input, Error: ParseError<Input>>( |
118 | 557k | arr: T, |
119 | 557k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
120 | 557k | where |
121 | 557k | Input: InputTakeAtPosition, |
122 | 557k | T: FindToken<<Input as InputTakeAtPosition>::Item>, |
123 | | { |
124 | 647k | move |i: Input| { |
125 | 647k | let e: ErrorKind = ErrorKind::IsNot; |
126 | 12.5M | i.split_at_position1_complete(|c| arr.find_token(c), e) Unexecuted instantiation: nom::bytes::complete::is_not::<_, _, _>::{closure#0}::{closure#0}nom::bytes::complete::is_not::<&str, &[u8], nom::error::Error<&[u8]>>::{closure#0}::{closure#0}Line | Count | Source | 126 | 10.6M | i.split_at_position1_complete(|c| arr.find_token(c), e) |
nom::bytes::complete::is_not::<&str, &str, suricata::detect::error::RuleParseError<&str>>::{closure#0}::{closure#0}Line | Count | Source | 126 | 1.89M | i.split_at_position1_complete(|c| arr.find_token(c), e) |
Unexecuted instantiation: nom::bytes::complete::is_not::<_, _, _>::{closure#0}::{closure#0} |
127 | 647k | } Unexecuted instantiation: nom::bytes::complete::is_not::<_, _, _>::{closure#0}nom::bytes::complete::is_not::<&str, &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 124 | 444k | move |i: Input| { | 125 | 444k | let e: ErrorKind = ErrorKind::IsNot; | 126 | 444k | i.split_at_position1_complete(|c| arr.find_token(c), e) | 127 | 444k | } |
nom::bytes::complete::is_not::<&str, &str, suricata::detect::error::RuleParseError<&str>>::{closure#0}Line | Count | Source | 124 | 202k | move |i: Input| { | 125 | 202k | let e: ErrorKind = ErrorKind::IsNot; | 126 | 202k | i.split_at_position1_complete(|c| arr.find_token(c), e) | 127 | 202k | } |
Unexecuted instantiation: nom::bytes::complete::is_not::<_, _, _>::{closure#0} |
128 | 557k | } nom::bytes::complete::is_not::<&str, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 117 | 444k | pub fn is_not<T, Input, Error: ParseError<Input>>( | 118 | 444k | arr: T, | 119 | 444k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 120 | 444k | where | 121 | 444k | Input: InputTakeAtPosition, | 122 | 444k | T: FindToken<<Input as InputTakeAtPosition>::Item>, | 123 | | { | 124 | | move |i: Input| { | 125 | | let e: ErrorKind = ErrorKind::IsNot; | 126 | | i.split_at_position1_complete(|c| arr.find_token(c), e) | 127 | | } | 128 | 444k | } |
Unexecuted instantiation: nom::bytes::complete::is_not::<_, _, _> nom::bytes::complete::is_not::<&str, &str, suricata::detect::error::RuleParseError<&str>> Line | Count | Source | 117 | 112k | pub fn is_not<T, Input, Error: ParseError<Input>>( | 118 | 112k | arr: T, | 119 | 112k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 120 | 112k | where | 121 | 112k | Input: InputTakeAtPosition, | 122 | 112k | T: FindToken<<Input as InputTakeAtPosition>::Item>, | 123 | | { | 124 | | move |i: Input| { | 125 | | let e: ErrorKind = ErrorKind::IsNot; | 126 | | i.split_at_position1_complete(|c| arr.find_token(c), e) | 127 | | } | 128 | 112k | } |
Unexecuted instantiation: nom::bytes::complete::is_not::<_, _, _> |
129 | | |
130 | | /// Returns the longest slice of the matches the pattern. |
131 | | /// |
132 | | /// The parser will return the longest slice consisting of the characters in provided in the |
133 | | /// combinator's argument. |
134 | | /// |
135 | | /// It will return a `Err(Err::Error((_, ErrorKind::IsA)))` if the pattern wasn't met. |
136 | | /// # Example |
137 | | /// ```rust |
138 | | /// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult}; |
139 | | /// use nom::bytes::complete::is_a; |
140 | | /// |
141 | | /// fn hex(s: &str) -> IResult<&str, &str> { |
142 | | /// is_a("1234567890ABCDEF")(s) |
143 | | /// } |
144 | | /// |
145 | | /// assert_eq!(hex("123 and voila"), Ok((" and voila", "123"))); |
146 | | /// assert_eq!(hex("DEADBEEF and others"), Ok((" and others", "DEADBEEF"))); |
147 | | /// assert_eq!(hex("BADBABEsomething"), Ok(("something", "BADBABE"))); |
148 | | /// assert_eq!(hex("D15EA5E"), Ok(("", "D15EA5E"))); |
149 | | /// assert_eq!(hex(""), Err(Err::Error(Error::new("", ErrorKind::IsA)))); |
150 | | /// ``` |
151 | 540k | pub fn is_a<T, Input, Error: ParseError<Input>>( |
152 | 540k | arr: T, |
153 | 540k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
154 | 540k | where |
155 | 540k | Input: InputTakeAtPosition, |
156 | 540k | T: FindToken<<Input as InputTakeAtPosition>::Item>, |
157 | | { |
158 | 540k | move |i: Input| { |
159 | 540k | let e: ErrorKind = ErrorKind::IsA; |
160 | 540k | i.split_at_position1_complete(|c| !arr.find_token(c), e) Unexecuted instantiation: nom::bytes::complete::is_a::<_, _, _>::{closure#0}::{closure#0}nom::bytes::complete::is_a::<&str, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 160 | 502k | i.split_at_position1_complete(|c| !arr.find_token(c), e) |
Unexecuted instantiation: nom::bytes::complete::is_a::<_, _, _>::{closure#0}::{closure#0} |
161 | 540k | } Unexecuted instantiation: nom::bytes::complete::is_a::<_, _, _>::{closure#0}nom::bytes::complete::is_a::<&str, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 158 | 540k | move |i: Input| { | 159 | 540k | let e: ErrorKind = ErrorKind::IsA; | 160 | 540k | i.split_at_position1_complete(|c| !arr.find_token(c), e) | 161 | 540k | } |
Unexecuted instantiation: nom::bytes::complete::is_a::<_, _, _>::{closure#0} |
162 | 540k | } Unexecuted instantiation: nom::bytes::complete::is_a::<_, _, _> nom::bytes::complete::is_a::<&str, &str, nom::error::Error<&str>> Line | Count | Source | 151 | 540k | pub fn is_a<T, Input, Error: ParseError<Input>>( | 152 | 540k | arr: T, | 153 | 540k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 154 | 540k | where | 155 | 540k | Input: InputTakeAtPosition, | 156 | 540k | T: FindToken<<Input as InputTakeAtPosition>::Item>, | 157 | | { | 158 | | move |i: Input| { | 159 | | let e: ErrorKind = ErrorKind::IsA; | 160 | | i.split_at_position1_complete(|c| !arr.find_token(c), e) | 161 | | } | 162 | 540k | } |
Unexecuted instantiation: nom::bytes::complete::is_a::<_, _, _> |
163 | | |
164 | | /// Returns the longest input slice (if any) that matches the predicate. |
165 | | /// |
166 | | /// The parser will return the longest slice that matches the given predicate *(a function that |
167 | | /// takes the input and returns a bool)*. |
168 | | /// # Example |
169 | | /// ```rust |
170 | | /// # use nom::{Err, error::ErrorKind, Needed, IResult}; |
171 | | /// use nom::bytes::complete::take_while; |
172 | | /// use nom::character::is_alphabetic; |
173 | | /// |
174 | | /// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { |
175 | | /// take_while(is_alphabetic)(s) |
176 | | /// } |
177 | | /// |
178 | | /// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); |
179 | | /// assert_eq!(alpha(b"12345"), Ok((&b"12345"[..], &b""[..]))); |
180 | | /// assert_eq!(alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); |
181 | | /// assert_eq!(alpha(b""), Ok((&b""[..], &b""[..]))); |
182 | | /// ``` |
183 | 208k | pub fn take_while<F, Input, Error: ParseError<Input>>( |
184 | 208k | cond: F, |
185 | 208k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
186 | 208k | where |
187 | 208k | Input: InputTakeAtPosition, |
188 | 208k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, |
189 | | { |
190 | 363k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) Unexecuted instantiation: nom::bytes::complete::take_while::<_, _, _>::{closure#0}nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u8>::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 190 | 33.3k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u32>::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 190 | 33.3k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u16>::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 190 | 22.3k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u64>::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 190 | 35.0k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint_inclusive<u32>::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 190 | 20.2k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::krb::detect::detect_parse_encryption::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 190 | 3.23k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::stream_size::detect_parse_stream_size::{closure#1}, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 190 | 5.19k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::requires::parse_key_value::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 190 | 55.9k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
Unexecuted instantiation: nom::bytes::complete::take_while::<_, _, _>::{closure#0}Unexecuted instantiation: nom::bytes::complete::take_while::<_, _, _>::{closure#0}::{closure#0}nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u8>::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 190 | 3.40k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u32>::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 190 | 3.14k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u16>::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 190 | 1.09k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u64>::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 190 | 4.28k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint_inclusive<u32>::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 190 | 3.97k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::krb::detect::detect_parse_encryption::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 190 | 749 | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::stream_size::detect_parse_stream_size::{closure#1}, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 190 | 1.22k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
nom::bytes::complete::take_while::<suricata::detect::requires::parse_key_value::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 190 | 346k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
Unexecuted instantiation: nom::bytes::complete::take_while::<_, _, _>::{closure#0}::{closure#0} |
191 | 208k | } Unexecuted instantiation: nom::bytes::complete::take_while::<_, _, _> nom::bytes::complete::take_while::<suricata::krb::detect::detect_parse_encryption::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 3.23k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 3.23k | cond: F, | 185 | 3.23k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 3.23k | where | 187 | 3.23k | Input: InputTakeAtPosition, | 188 | 3.23k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 3.23k | } |
nom::bytes::complete::take_while::<suricata::detect::stream_size::detect_parse_stream_size::{closure#1}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 5.19k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 5.19k | cond: F, | 185 | 5.19k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 5.19k | where | 187 | 5.19k | Input: InputTakeAtPosition, | 188 | 5.19k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 5.19k | } |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u8>::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 33.3k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 33.3k | cond: F, | 185 | 33.3k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 33.3k | where | 187 | 33.3k | Input: InputTakeAtPosition, | 188 | 33.3k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 33.3k | } |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u32>::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 33.3k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 33.3k | cond: F, | 185 | 33.3k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 33.3k | where | 187 | 33.3k | Input: InputTakeAtPosition, | 188 | 33.3k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 33.3k | } |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u16>::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 22.3k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 22.3k | cond: F, | 185 | 22.3k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 22.3k | where | 187 | 22.3k | Input: InputTakeAtPosition, | 188 | 22.3k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 22.3k | } |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u64>::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 35.0k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 35.0k | cond: F, | 185 | 35.0k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 35.0k | where | 187 | 35.0k | Input: InputTakeAtPosition, | 188 | 35.0k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 35.0k | } |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint_inclusive<u32>::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 20.2k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 20.2k | cond: F, | 185 | 20.2k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 20.2k | where | 187 | 20.2k | Input: InputTakeAtPosition, | 188 | 20.2k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 20.2k | } |
nom::bytes::complete::take_while::<suricata::detect::requires::parse_key_value::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 55.9k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 55.9k | cond: F, | 185 | 55.9k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 55.9k | where | 187 | 55.9k | Input: InputTakeAtPosition, | 188 | 55.9k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 55.9k | } |
Unexecuted instantiation: nom::bytes::complete::take_while::<_, _, _> |
192 | | |
193 | | /// Returns the longest (at least 1) input slice that matches the predicate. |
194 | | /// |
195 | | /// The parser will return the longest slice that matches the given predicate *(a function that |
196 | | /// takes the input and returns a bool)*. |
197 | | /// |
198 | | /// It will return an `Err(Err::Error((_, ErrorKind::TakeWhile1)))` if the pattern wasn't met. |
199 | | /// # Example |
200 | | /// ```rust |
201 | | /// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult}; |
202 | | /// use nom::bytes::complete::take_while1; |
203 | | /// use nom::character::is_alphabetic; |
204 | | /// |
205 | | /// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { |
206 | | /// take_while1(is_alphabetic)(s) |
207 | | /// } |
208 | | /// |
209 | | /// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); |
210 | | /// assert_eq!(alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); |
211 | | /// assert_eq!(alpha(b"12345"), Err(Err::Error(Error::new(&b"12345"[..], ErrorKind::TakeWhile1)))); |
212 | | /// ``` |
213 | 13.9k | pub fn take_while1<F, Input, Error: ParseError<Input>>( |
214 | 13.9k | cond: F, |
215 | 13.9k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
216 | 13.9k | where |
217 | 13.9k | Input: InputTakeAtPosition, |
218 | 13.9k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, |
219 | | { |
220 | 13.9k | move |i: Input| { |
221 | 13.9k | let e: ErrorKind = ErrorKind::TakeWhile1; |
222 | 62.2k | i.split_at_position1_complete(|c| !cond(c), e) Unexecuted instantiation: nom::bytes::complete::take_while1::<_, _, _>::{closure#0}::{closure#0}nom::bytes::complete::take_while1::<nom::character::is_digit, &[u8], nom::error::Error<&[u8]>>::{closure#0}::{closure#0}Line | Count | Source | 222 | 13.6k | i.split_at_position1_complete(|c| !cond(c), e) |
nom::bytes::complete::take_while1::<suricata::krb::detect::is_alphanumeric_or_dash, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 222 | 48.6k | i.split_at_position1_complete(|c| !cond(c), e) |
Unexecuted instantiation: nom::bytes::complete::take_while1::<_, _, _>::{closure#0}::{closure#0} |
223 | 13.9k | } Unexecuted instantiation: nom::bytes::complete::take_while1::<_, _, _>::{closure#0}nom::bytes::complete::take_while1::<nom::character::is_digit, &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 220 | 7.14k | move |i: Input| { | 221 | 7.14k | let e: ErrorKind = ErrorKind::TakeWhile1; | 222 | 7.14k | i.split_at_position1_complete(|c| !cond(c), e) | 223 | 7.14k | } |
nom::bytes::complete::take_while1::<suricata::krb::detect::is_alphanumeric_or_dash, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 220 | 6.79k | move |i: Input| { | 221 | 6.79k | let e: ErrorKind = ErrorKind::TakeWhile1; | 222 | 6.79k | i.split_at_position1_complete(|c| !cond(c), e) | 223 | 6.79k | } |
Unexecuted instantiation: nom::bytes::complete::take_while1::<_, _, _>::{closure#0} |
224 | 13.9k | } Unexecuted instantiation: nom::bytes::complete::take_while1::<_, _, _> nom::bytes::complete::take_while1::<nom::character::is_digit, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 213 | 7.14k | pub fn take_while1<F, Input, Error: ParseError<Input>>( | 214 | 7.14k | cond: F, | 215 | 7.14k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 216 | 7.14k | where | 217 | 7.14k | Input: InputTakeAtPosition, | 218 | 7.14k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 219 | | { | 220 | | move |i: Input| { | 221 | | let e: ErrorKind = ErrorKind::TakeWhile1; | 222 | | i.split_at_position1_complete(|c| !cond(c), e) | 223 | | } | 224 | 7.14k | } |
nom::bytes::complete::take_while1::<suricata::krb::detect::is_alphanumeric_or_dash, &str, nom::error::Error<&str>> Line | Count | Source | 213 | 6.79k | pub fn take_while1<F, Input, Error: ParseError<Input>>( | 214 | 6.79k | cond: F, | 215 | 6.79k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 216 | 6.79k | where | 217 | 6.79k | Input: InputTakeAtPosition, | 218 | 6.79k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 219 | | { | 220 | | move |i: Input| { | 221 | | let e: ErrorKind = ErrorKind::TakeWhile1; | 222 | | i.split_at_position1_complete(|c| !cond(c), e) | 223 | | } | 224 | 6.79k | } |
Unexecuted instantiation: nom::bytes::complete::take_while1::<_, _, _> |
225 | | |
226 | | /// Returns the longest (m <= len <= n) input slice that matches the predicate. |
227 | | /// |
228 | | /// The parser will return the longest slice that matches the given predicate *(a function that |
229 | | /// takes the input and returns a bool)*. |
230 | | /// |
231 | | /// It will return an `Err::Error((_, ErrorKind::TakeWhileMN))` if the pattern wasn't met or is out |
232 | | /// of range (m <= len <= n). |
233 | | /// # Example |
234 | | /// ```rust |
235 | | /// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult}; |
236 | | /// use nom::bytes::complete::take_while_m_n; |
237 | | /// use nom::character::is_alphabetic; |
238 | | /// |
239 | | /// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> { |
240 | | /// take_while_m_n(3, 6, is_alphabetic)(s) |
241 | | /// } |
242 | | /// |
243 | | /// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..]))); |
244 | | /// assert_eq!(short_alpha(b"lengthy"), Ok((&b"y"[..], &b"length"[..]))); |
245 | | /// assert_eq!(short_alpha(b"latin"), Ok((&b""[..], &b"latin"[..]))); |
246 | | /// assert_eq!(short_alpha(b"ed"), Err(Err::Error(Error::new(&b"ed"[..], ErrorKind::TakeWhileMN)))); |
247 | | /// assert_eq!(short_alpha(b"12345"), Err(Err::Error(Error::new(&b"12345"[..], ErrorKind::TakeWhileMN)))); |
248 | | /// ``` |
249 | 0 | pub fn take_while_m_n<F, Input, Error: ParseError<Input>>( |
250 | 0 | m: usize, |
251 | 0 | n: usize, |
252 | 0 | cond: F, |
253 | 0 | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
254 | 0 | where |
255 | 0 | Input: InputTake + InputIter + InputLength + Slice<RangeFrom<usize>>, |
256 | 0 | F: Fn(<Input as InputIter>::Item) -> bool, |
257 | | { |
258 | 0 | move |i: Input| { |
259 | 0 | let input = i; |
260 | | |
261 | 0 | match input.position(|c| !cond(c)) {Unexecuted instantiation: nom::bytes::complete::take_while_m_n::<_, _, _>::{closure#0}::{closure#0}Unexecuted instantiation: nom::bytes::complete::take_while_m_n::<_, _, _>::{closure#0}::{closure#0} |
262 | 0 | Some(idx) => { |
263 | 0 | if idx >= m { |
264 | 0 | if idx <= n { |
265 | 0 | let res: IResult<_, _, Error> = if let Ok(index) = input.slice_index(idx) { |
266 | 0 | Ok(input.take_split(index)) |
267 | | } else { |
268 | 0 | Err(Err::Error(Error::from_error_kind( |
269 | 0 | input, |
270 | 0 | ErrorKind::TakeWhileMN, |
271 | 0 | ))) |
272 | | }; |
273 | 0 | res |
274 | | } else { |
275 | 0 | let res: IResult<_, _, Error> = if let Ok(index) = input.slice_index(n) { |
276 | 0 | Ok(input.take_split(index)) |
277 | | } else { |
278 | 0 | Err(Err::Error(Error::from_error_kind( |
279 | 0 | input, |
280 | 0 | ErrorKind::TakeWhileMN, |
281 | 0 | ))) |
282 | | }; |
283 | 0 | res |
284 | | } |
285 | | } else { |
286 | 0 | let e = ErrorKind::TakeWhileMN; |
287 | 0 | Err(Err::Error(Error::from_error_kind(input, e))) |
288 | | } |
289 | | } |
290 | | None => { |
291 | 0 | let len = input.input_len(); |
292 | 0 | if len >= n { |
293 | 0 | match input.slice_index(n) { |
294 | 0 | Ok(index) => Ok(input.take_split(index)), |
295 | 0 | Err(_needed) => Err(Err::Error(Error::from_error_kind( |
296 | 0 | input, |
297 | 0 | ErrorKind::TakeWhileMN, |
298 | 0 | ))), |
299 | | } |
300 | 0 | } else if len >= m && len <= n { |
301 | 0 | let res: IResult<_, _, Error> = Ok((input.slice(len..), input)); |
302 | 0 | res |
303 | | } else { |
304 | 0 | let e = ErrorKind::TakeWhileMN; |
305 | 0 | Err(Err::Error(Error::from_error_kind(input, e))) |
306 | | } |
307 | | } |
308 | | } |
309 | 0 | } Unexecuted instantiation: nom::bytes::complete::take_while_m_n::<_, _, _>::{closure#0}Unexecuted instantiation: nom::bytes::complete::take_while_m_n::<_, _, _>::{closure#0} |
310 | 0 | } Unexecuted instantiation: nom::bytes::complete::take_while_m_n::<_, _, _> Unexecuted instantiation: nom::bytes::complete::take_while_m_n::<_, _, _> |
311 | | |
312 | | /// Returns the longest input slice (if any) till a predicate is met. |
313 | | /// |
314 | | /// The parser will return the longest slice till the given predicate *(a function that |
315 | | /// takes the input and returns a bool)*. |
316 | | /// # Example |
317 | | /// ```rust |
318 | | /// # use nom::{Err, error::ErrorKind, Needed, IResult}; |
319 | | /// use nom::bytes::complete::take_till; |
320 | | /// |
321 | | /// fn till_colon(s: &str) -> IResult<&str, &str> { |
322 | | /// take_till(|c| c == ':')(s) |
323 | | /// } |
324 | | /// |
325 | | /// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); |
326 | | /// assert_eq!(till_colon(":empty matched"), Ok((":empty matched", ""))); //allowed |
327 | | /// assert_eq!(till_colon("12345"), Ok(("", "12345"))); |
328 | | /// assert_eq!(till_colon(""), Ok(("", ""))); |
329 | | /// ``` |
330 | 183k | pub fn take_till<F, Input, Error: ParseError<Input>>( |
331 | 183k | cond: F, |
332 | 183k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
333 | 183k | where |
334 | 183k | Input: InputTakeAtPosition, |
335 | 183k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, |
336 | | { |
337 | 1.23M | move |i: Input| i.split_at_position_complete(|c| cond(c)) Unexecuted instantiation: nom::bytes::complete::take_till::<_, _, _>::{closure#0}nom::bytes::complete::take_till::<suricata::detect::requires::parse_key_value::{closure#1}, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 337 | 55.9k | move |i: Input| i.split_at_position_complete(|c| cond(c)) |
nom::bytes::complete::take_till::<suricata::detect::requires::parse_next_version_part::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 337 | 127k | move |i: Input| i.split_at_position_complete(|c| cond(c)) |
Unexecuted instantiation: nom::bytes::complete::take_till::<_, _, _>::{closure#0}Unexecuted instantiation: nom::bytes::complete::take_till::<_, _, _>::{closure#0}::{closure#0}nom::bytes::complete::take_till::<suricata::detect::requires::parse_key_value::{closure#1}, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 337 | 930k | move |i: Input| i.split_at_position_complete(|c| cond(c)) |
nom::bytes::complete::take_till::<suricata::detect::requires::parse_next_version_part::{closure#0}, &str, nom::error::Error<&str>>::{closure#0}::{closure#0}Line | Count | Source | 337 | 302k | move |i: Input| i.split_at_position_complete(|c| cond(c)) |
Unexecuted instantiation: nom::bytes::complete::take_till::<_, _, _>::{closure#0}::{closure#0} |
338 | 183k | } Unexecuted instantiation: nom::bytes::complete::take_till::<_, _, _> nom::bytes::complete::take_till::<suricata::detect::requires::parse_key_value::{closure#1}, &str, nom::error::Error<&str>>Line | Count | Source | 330 | 55.9k | pub fn take_till<F, Input, Error: ParseError<Input>>( | 331 | 55.9k | cond: F, | 332 | 55.9k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 333 | 55.9k | where | 334 | 55.9k | Input: InputTakeAtPosition, | 335 | 55.9k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 336 | | { | 337 | | move |i: Input| i.split_at_position_complete(|c| cond(c)) | 338 | 55.9k | } |
nom::bytes::complete::take_till::<suricata::detect::requires::parse_next_version_part::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 330 | 127k | pub fn take_till<F, Input, Error: ParseError<Input>>( | 331 | 127k | cond: F, | 332 | 127k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 333 | 127k | where | 334 | 127k | Input: InputTakeAtPosition, | 335 | 127k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 336 | | { | 337 | | move |i: Input| i.split_at_position_complete(|c| cond(c)) | 338 | 127k | } |
Unexecuted instantiation: nom::bytes::complete::take_till::<_, _, _> |
339 | | |
340 | | /// Returns the longest (at least 1) input slice till a predicate is met. |
341 | | /// |
342 | | /// The parser will return the longest slice till the given predicate *(a function that |
343 | | /// takes the input and returns a bool)*. |
344 | | /// |
345 | | /// It will return `Err(Err::Error((_, ErrorKind::TakeTill1)))` if the input is empty or the |
346 | | /// predicate matches the first input. |
347 | | /// # Example |
348 | | /// ```rust |
349 | | /// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult}; |
350 | | /// use nom::bytes::complete::take_till1; |
351 | | /// |
352 | | /// fn till_colon(s: &str) -> IResult<&str, &str> { |
353 | | /// take_till1(|c| c == ':')(s) |
354 | | /// } |
355 | | /// |
356 | | /// assert_eq!(till_colon("latin:123"), Ok((":123", "latin"))); |
357 | | /// assert_eq!(till_colon(":empty matched"), Err(Err::Error(Error::new(":empty matched", ErrorKind::TakeTill1)))); |
358 | | /// assert_eq!(till_colon("12345"), Ok(("", "12345"))); |
359 | | /// assert_eq!(till_colon(""), Err(Err::Error(Error::new("", ErrorKind::TakeTill1)))); |
360 | | /// ``` |
361 | 0 | pub fn take_till1<F, Input, Error: ParseError<Input>>( |
362 | 0 | cond: F, |
363 | 0 | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
364 | 0 | where |
365 | 0 | Input: InputTakeAtPosition, |
366 | 0 | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, |
367 | | { |
368 | 0 | move |i: Input| { |
369 | 0 | let e: ErrorKind = ErrorKind::TakeTill1; |
370 | 0 | i.split_at_position1_complete(|c| cond(c), e) Unexecuted instantiation: nom::bytes::complete::take_till1::<_, _, _>::{closure#0}::{closure#0}Unexecuted instantiation: nom::bytes::complete::take_till1::<_, _, _>::{closure#0}::{closure#0} |
371 | 0 | } Unexecuted instantiation: nom::bytes::complete::take_till1::<_, _, _>::{closure#0}Unexecuted instantiation: nom::bytes::complete::take_till1::<_, _, _>::{closure#0} |
372 | 0 | } Unexecuted instantiation: nom::bytes::complete::take_till1::<_, _, _> Unexecuted instantiation: nom::bytes::complete::take_till1::<_, _, _> |
373 | | |
374 | | /// Returns an input slice containing the first N input elements (Input[..N]). |
375 | | /// |
376 | | /// It will return `Err(Err::Error((_, ErrorKind::Eof)))` if the input is shorter than the argument. |
377 | | /// # Example |
378 | | /// ```rust |
379 | | /// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult}; |
380 | | /// use nom::bytes::complete::take; |
381 | | /// |
382 | | /// fn take6(s: &str) -> IResult<&str, &str> { |
383 | | /// take(6usize)(s) |
384 | | /// } |
385 | | /// |
386 | | /// assert_eq!(take6("1234567"), Ok(("7", "123456"))); |
387 | | /// assert_eq!(take6("things"), Ok(("", "things"))); |
388 | | /// assert_eq!(take6("short"), Err(Err::Error(Error::new("short", ErrorKind::Eof)))); |
389 | | /// assert_eq!(take6(""), Err(Err::Error(Error::new("", ErrorKind::Eof)))); |
390 | | /// ``` |
391 | | /// |
392 | | /// The units that are taken will depend on the input type. For example, for a |
393 | | /// `&str` it will take a number of `char`'s, whereas for a `&[u8]` it will |
394 | | /// take that many `u8`'s: |
395 | | /// |
396 | | /// ```rust |
397 | | /// use nom::error::Error; |
398 | | /// use nom::bytes::complete::take; |
399 | | /// |
400 | | /// assert_eq!(take::<_, _, Error<_>>(1usize)("💙"), Ok(("", "💙"))); |
401 | | /// assert_eq!(take::<_, _, Error<_>>(1usize)("💙".as_bytes()), Ok((b"\x9F\x92\x99".as_ref(), b"\xF0".as_ref()))); |
402 | | /// ``` |
403 | 7.10M | pub fn take<C, Input, Error: ParseError<Input>>( |
404 | 7.10M | count: C, |
405 | 7.10M | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
406 | 7.10M | where |
407 | 7.10M | Input: InputIter + InputTake, |
408 | 7.10M | C: ToUsize, |
409 | | { |
410 | 7.10M | let c = count.to_usize(); |
411 | 7.10M | move |i: Input| match i.slice_index(c) { |
412 | 19.3k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), |
413 | 7.08M | Ok(index) => Ok(i.take_split(index)), |
414 | 7.10M | } nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error>::{closure#0}Line | Count | Source | 411 | 48.5k | move |i: Input| match i.slice_index(c) { | 412 | 60 | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 48.4k | Ok(index) => Ok(i.take_split(index)), | 414 | 48.5k | } |
nom::bytes::complete::take::<usize, &[u8], x509_parser::error::X509Error>::{closure#0}Line | Count | Source | 411 | 124k | move |i: Input| match i.slice_index(c) { | 412 | 3.44k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 121k | Ok(index) => Ok(i.take_split(index)), | 414 | 124k | } |
nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error>::{closure#0}Line | Count | Source | 411 | 32.0k | move |i: Input| match i.slice_index(c) { | 412 | 656 | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 31.4k | Ok(index) => Ok(i.take_split(index)), | 414 | 32.0k | } |
Unexecuted instantiation: nom::bytes::complete::take::<_, _, _>::{closure#0}nom::bytes::complete::take::<usize, &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 411 | 1.09M | move |i: Input| match i.slice_index(c) { | 412 | 4.82k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 1.08M | Ok(index) => Ok(i.take_split(index)), | 414 | 1.09M | } |
nom::bytes::complete::take::<u8, &[u8], suricata::quic::error::QuicError>::{closure#0}Line | Count | Source | 411 | 239k | move |i: Input| match i.slice_index(c) { | 412 | 897 | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 238k | Ok(index) => Ok(i.take_split(index)), | 414 | 239k | } |
nom::bytes::complete::take::<usize, &[u8], suricata::quic::error::QuicError>::{closure#0}Line | Count | Source | 411 | 5.32M | move |i: Input| match i.slice_index(c) { | 412 | 3.84k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 5.31M | Ok(index) => Ok(i.take_split(index)), | 414 | 5.32M | } |
nom::bytes::complete::take::<u32, &[u8], suricata::quic::error::QuicError>::{closure#0}Line | Count | Source | 411 | 72.0k | move |i: Input| match i.slice_index(c) { | 412 | 1.99k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 70.0k | Ok(index) => Ok(i.take_split(index)), | 414 | 72.0k | } |
nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error>::{closure#0}Line | Count | Source | 411 | 52.1k | move |i: Input| match i.slice_index(c) { | 412 | 131 | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 51.9k | Ok(index) => Ok(i.take_split(index)), | 414 | 52.1k | } |
nom::bytes::complete::take::<usize, &[u8], x509_parser::error::X509Error>::{closure#0}Line | Count | Source | 411 | 117k | move |i: Input| match i.slice_index(c) { | 412 | 3.50k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 113k | Ok(index) => Ok(i.take_split(index)), | 414 | 117k | } |
Unexecuted instantiation: nom::bytes::complete::take::<_, _, _>::{closure#0} |
415 | 7.10M | } nom::bytes::complete::take::<usize, &[u8], x509_parser::error::X509Error> Line | Count | Source | 403 | 124k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 124k | count: C, | 405 | 124k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 124k | where | 407 | 124k | Input: InputIter + InputTake, | 408 | 124k | C: ToUsize, | 409 | | { | 410 | 124k | let c = count.to_usize(); | 411 | | move |i: Input| match i.slice_index(c) { | 412 | | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | | Ok(index) => Ok(i.take_split(index)), | 414 | | } | 415 | 124k | } |
nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error> Line | Count | Source | 403 | 48.5k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 48.5k | count: C, | 405 | 48.5k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 48.5k | where | 407 | 48.5k | Input: InputIter + InputTake, | 408 | 48.5k | C: ToUsize, | 409 | | { | 410 | 48.5k | let c = count.to_usize(); | 411 | | move |i: Input| match i.slice_index(c) { | 412 | | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | | Ok(index) => Ok(i.take_split(index)), | 414 | | } | 415 | 48.5k | } |
nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error> Line | Count | Source | 403 | 32.0k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 32.0k | count: C, | 405 | 32.0k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 32.0k | where | 407 | 32.0k | Input: InputIter + InputTake, | 408 | 32.0k | C: ToUsize, | 409 | | { | 410 | 32.0k | let c = count.to_usize(); | 411 | | move |i: Input| match i.slice_index(c) { | 412 | | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | | Ok(index) => Ok(i.take_split(index)), | 414 | | } | 415 | 32.0k | } |
Unexecuted instantiation: nom::bytes::complete::take::<_, _, _> nom::bytes::complete::take::<u8, &[u8], suricata::quic::error::QuicError> Line | Count | Source | 403 | 239k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 239k | count: C, | 405 | 239k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 239k | where | 407 | 239k | Input: InputIter + InputTake, | 408 | 239k | C: ToUsize, | 409 | | { | 410 | 239k | let c = count.to_usize(); | 411 | | move |i: Input| match i.slice_index(c) { | 412 | | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | | Ok(index) => Ok(i.take_split(index)), | 414 | | } | 415 | 239k | } |
nom::bytes::complete::take::<usize, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 403 | 1.09M | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 1.09M | count: C, | 405 | 1.09M | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 1.09M | where | 407 | 1.09M | Input: InputIter + InputTake, | 408 | 1.09M | C: ToUsize, | 409 | | { | 410 | 1.09M | let c = count.to_usize(); | 411 | | move |i: Input| match i.slice_index(c) { | 412 | | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | | Ok(index) => Ok(i.take_split(index)), | 414 | | } | 415 | 1.09M | } |
nom::bytes::complete::take::<usize, &[u8], suricata::quic::error::QuicError> Line | Count | Source | 403 | 5.32M | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 5.32M | count: C, | 405 | 5.32M | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 5.32M | where | 407 | 5.32M | Input: InputIter + InputTake, | 408 | 5.32M | C: ToUsize, | 409 | | { | 410 | 5.32M | let c = count.to_usize(); | 411 | | move |i: Input| match i.slice_index(c) { | 412 | | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | | Ok(index) => Ok(i.take_split(index)), | 414 | | } | 415 | 5.32M | } |
nom::bytes::complete::take::<u32, &[u8], suricata::quic::error::QuicError> Line | Count | Source | 403 | 72.0k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 72.0k | count: C, | 405 | 72.0k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 72.0k | where | 407 | 72.0k | Input: InputIter + InputTake, | 408 | 72.0k | C: ToUsize, | 409 | | { | 410 | 72.0k | let c = count.to_usize(); | 411 | | move |i: Input| match i.slice_index(c) { | 412 | | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | | Ok(index) => Ok(i.take_split(index)), | 414 | | } | 415 | 72.0k | } |
nom::bytes::complete::take::<usize, &[u8], x509_parser::error::X509Error> Line | Count | Source | 403 | 117k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 117k | count: C, | 405 | 117k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 117k | where | 407 | 117k | Input: InputIter + InputTake, | 408 | 117k | C: ToUsize, | 409 | | { | 410 | 117k | let c = count.to_usize(); | 411 | | move |i: Input| match i.slice_index(c) { | 412 | | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | | Ok(index) => Ok(i.take_split(index)), | 414 | | } | 415 | 117k | } |
nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error> Line | Count | Source | 403 | 52.1k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 52.1k | count: C, | 405 | 52.1k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 52.1k | where | 407 | 52.1k | Input: InputIter + InputTake, | 408 | 52.1k | C: ToUsize, | 409 | | { | 410 | 52.1k | let c = count.to_usize(); | 411 | | move |i: Input| match i.slice_index(c) { | 412 | | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | | Ok(index) => Ok(i.take_split(index)), | 414 | | } | 415 | 52.1k | } |
Unexecuted instantiation: nom::bytes::complete::take::<_, _, _> |
416 | | |
417 | | /// Returns the input slice up to the first occurrence of the pattern. |
418 | | /// |
419 | | /// It doesn't consume the pattern. It will return `Err(Err::Error((_, ErrorKind::TakeUntil)))` |
420 | | /// if the pattern wasn't met. |
421 | | /// # Example |
422 | | /// ```rust |
423 | | /// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult}; |
424 | | /// use nom::bytes::complete::take_until; |
425 | | /// |
426 | | /// fn until_eof(s: &str) -> IResult<&str, &str> { |
427 | | /// take_until("eof")(s) |
428 | | /// } |
429 | | /// |
430 | | /// assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world"))); |
431 | | /// assert_eq!(until_eof("hello, world"), Err(Err::Error(Error::new("hello, world", ErrorKind::TakeUntil)))); |
432 | | /// assert_eq!(until_eof(""), Err(Err::Error(Error::new("", ErrorKind::TakeUntil)))); |
433 | | /// assert_eq!(until_eof("1eof2eof"), Ok(("eof2eof", "1"))); |
434 | | /// ``` |
435 | 870k | pub fn take_until<T, Input, Error: ParseError<Input>>( |
436 | 870k | tag: T, |
437 | 870k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
438 | 870k | where |
439 | 870k | Input: InputTake + FindSubstring<T>, |
440 | 870k | T: InputLength + Clone, |
441 | | { |
442 | 22.0M | move |i: Input| { |
443 | 22.0M | let t = tag.clone(); |
444 | 22.0M | let res: IResult<_, _, Error> = match i.find_substring(t) { |
445 | 318k | None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), |
446 | 21.7M | Some(index) => Ok(i.take_split(index)), |
447 | | }; |
448 | 22.0M | res |
449 | 22.0M | } Unexecuted instantiation: nom::bytes::complete::take_until::<_, _, _>::{closure#0}nom::bytes::complete::take_until::<&[u8], &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 442 | 21.9M | move |i: Input| { | 443 | 21.9M | let t = tag.clone(); | 444 | 21.9M | let res: IResult<_, _, Error> = match i.find_substring(t) { | 445 | 302k | None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), | 446 | 21.6M | Some(index) => Ok(i.take_split(index)), | 447 | | }; | 448 | 21.9M | res | 449 | 21.9M | } |
nom::bytes::complete::take_until::<&str, &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 442 | 109k | move |i: Input| { | 443 | 109k | let t = tag.clone(); | 444 | 109k | let res: IResult<_, _, Error> = match i.find_substring(t) { | 445 | 15.2k | None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), | 446 | 94.6k | Some(index) => Ok(i.take_split(index)), | 447 | | }; | 448 | 109k | res | 449 | 109k | } |
Unexecuted instantiation: nom::bytes::complete::take_until::<_, _, _>::{closure#0} |
450 | 870k | } nom::bytes::complete::take_until::<&[u8], &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 435 | 760k | pub fn take_until<T, Input, Error: ParseError<Input>>( | 436 | 760k | tag: T, | 437 | 760k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 438 | 760k | where | 439 | 760k | Input: InputTake + FindSubstring<T>, | 440 | 760k | T: InputLength + Clone, | 441 | | { | 442 | | move |i: Input| { | 443 | | let t = tag.clone(); | 444 | | let res: IResult<_, _, Error> = match i.find_substring(t) { | 445 | | None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), | 446 | | Some(index) => Ok(i.take_split(index)), | 447 | | }; | 448 | | res | 449 | | } | 450 | 760k | } |
Unexecuted instantiation: nom::bytes::complete::take_until::<_, _, _> nom::bytes::complete::take_until::<&str, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 435 | 109k | pub fn take_until<T, Input, Error: ParseError<Input>>( | 436 | 109k | tag: T, | 437 | 109k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 438 | 109k | where | 439 | 109k | Input: InputTake + FindSubstring<T>, | 440 | 109k | T: InputLength + Clone, | 441 | | { | 442 | | move |i: Input| { | 443 | | let t = tag.clone(); | 444 | | let res: IResult<_, _, Error> = match i.find_substring(t) { | 445 | | None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), | 446 | | Some(index) => Ok(i.take_split(index)), | 447 | | }; | 448 | | res | 449 | | } | 450 | 109k | } |
Unexecuted instantiation: nom::bytes::complete::take_until::<_, _, _> |
451 | | |
452 | | /// Returns the non empty input slice up to the first occurrence of the pattern. |
453 | | /// |
454 | | /// It doesn't consume the pattern. It will return `Err(Err::Error((_, ErrorKind::TakeUntil)))` |
455 | | /// if the pattern wasn't met. |
456 | | /// # Example |
457 | | /// ```rust |
458 | | /// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult}; |
459 | | /// use nom::bytes::complete::take_until1; |
460 | | /// |
461 | | /// fn until_eof(s: &str) -> IResult<&str, &str> { |
462 | | /// take_until1("eof")(s) |
463 | | /// } |
464 | | /// |
465 | | /// assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world"))); |
466 | | /// assert_eq!(until_eof("hello, world"), Err(Err::Error(Error::new("hello, world", ErrorKind::TakeUntil)))); |
467 | | /// assert_eq!(until_eof(""), Err(Err::Error(Error::new("", ErrorKind::TakeUntil)))); |
468 | | /// assert_eq!(until_eof("1eof2eof"), Ok(("eof2eof", "1"))); |
469 | | /// assert_eq!(until_eof("eof"), Err(Err::Error(Error::new("eof", ErrorKind::TakeUntil)))); |
470 | | /// ``` |
471 | 0 | pub fn take_until1<T, Input, Error: ParseError<Input>>( |
472 | 0 | tag: T, |
473 | 0 | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
474 | 0 | where |
475 | 0 | Input: InputTake + FindSubstring<T>, |
476 | 0 | T: InputLength + Clone, |
477 | | { |
478 | 0 | move |i: Input| { |
479 | 0 | let t = tag.clone(); |
480 | 0 | let res: IResult<_, _, Error> = match i.find_substring(t) { |
481 | 0 | None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), |
482 | 0 | Some(0) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), |
483 | 0 | Some(index) => Ok(i.take_split(index)), |
484 | | }; |
485 | 0 | res |
486 | 0 | } Unexecuted instantiation: nom::bytes::complete::take_until1::<_, _, _>::{closure#0}Unexecuted instantiation: nom::bytes::complete::take_until1::<_, _, _>::{closure#0} |
487 | 0 | } Unexecuted instantiation: nom::bytes::complete::take_until1::<_, _, _> Unexecuted instantiation: nom::bytes::complete::take_until1::<_, _, _> |
488 | | |
489 | | /// Matches a byte string with escaped characters. |
490 | | /// |
491 | | /// * The first argument matches the normal characters (it must not accept the control character) |
492 | | /// * The second argument is the control character (like `\` in most languages) |
493 | | /// * The third argument matches the escaped characters |
494 | | /// # Example |
495 | | /// ``` |
496 | | /// # use nom::{Err, error::ErrorKind, Needed, IResult}; |
497 | | /// # use nom::character::complete::digit1; |
498 | | /// use nom::bytes::complete::escaped; |
499 | | /// use nom::character::complete::one_of; |
500 | | /// |
501 | | /// fn esc(s: &str) -> IResult<&str, &str> { |
502 | | /// escaped(digit1, '\\', one_of(r#""n\"#))(s) |
503 | | /// } |
504 | | /// |
505 | | /// assert_eq!(esc("123;"), Ok((";", "123"))); |
506 | | /// assert_eq!(esc(r#"12\"34;"#), Ok((";", r#"12\"34"#))); |
507 | | /// ``` |
508 | | /// |
509 | 0 | pub fn escaped<'a, Input: 'a, Error, F, G, O1, O2>( |
510 | 0 | mut normal: F, |
511 | 0 | control_char: char, |
512 | 0 | mut escapable: G, |
513 | 0 | ) -> impl FnMut(Input) -> IResult<Input, Input, Error> |
514 | 0 | where |
515 | 0 | Input: Clone |
516 | 0 | + crate::traits::Offset |
517 | 0 | + InputLength |
518 | 0 | + InputTake |
519 | 0 | + InputTakeAtPosition |
520 | 0 | + Slice<RangeFrom<usize>> |
521 | 0 | + InputIter, |
522 | 0 | <Input as InputIter>::Item: crate::traits::AsChar, |
523 | 0 | F: Parser<Input, O1, Error>, |
524 | 0 | G: Parser<Input, O2, Error>, |
525 | 0 | Error: ParseError<Input>, |
526 | | { |
527 | | use crate::traits::AsChar; |
528 | | |
529 | 0 | move |input: Input| { |
530 | 0 | let mut i = input.clone(); |
531 | | |
532 | 0 | while i.input_len() > 0 { |
533 | 0 | let current_len = i.input_len(); |
534 | | |
535 | 0 | match normal.parse(i.clone()) { |
536 | 0 | Ok((i2, _)) => { |
537 | | // return if we consumed everything or if the normal parser |
538 | | // does not consume anything |
539 | 0 | if i2.input_len() == 0 { |
540 | 0 | return Ok((input.slice(input.input_len()..), input)); |
541 | 0 | } else if i2.input_len() == current_len { |
542 | 0 | let index = input.offset(&i2); |
543 | 0 | return Ok(input.take_split(index)); |
544 | 0 | } else { |
545 | 0 | i = i2; |
546 | 0 | } |
547 | | } |
548 | | Err(Err::Error(_)) => { |
549 | | // unwrap() should be safe here since index < $i.input_len() |
550 | 0 | if i.iter_elements().next().unwrap().as_char() == control_char { |
551 | 0 | let next = control_char.len_utf8(); |
552 | 0 | if next >= i.input_len() { |
553 | 0 | return Err(Err::Error(Error::from_error_kind( |
554 | 0 | input, |
555 | 0 | ErrorKind::Escaped, |
556 | 0 | ))); |
557 | | } else { |
558 | 0 | match escapable.parse(i.slice(next..)) { |
559 | 0 | Ok((i2, _)) => { |
560 | 0 | if i2.input_len() == 0 { |
561 | 0 | return Ok((input.slice(input.input_len()..), input)); |
562 | 0 | } else { |
563 | 0 | i = i2; |
564 | 0 | } |
565 | | } |
566 | 0 | Err(e) => return Err(e), |
567 | | } |
568 | | } |
569 | | } else { |
570 | 0 | let index = input.offset(&i); |
571 | 0 | if index == 0 { |
572 | 0 | return Err(Err::Error(Error::from_error_kind( |
573 | 0 | input, |
574 | 0 | ErrorKind::Escaped, |
575 | 0 | ))); |
576 | 0 | } |
577 | 0 | return Ok(input.take_split(index)); |
578 | | } |
579 | | } |
580 | 0 | Err(e) => { |
581 | 0 | return Err(e); |
582 | | } |
583 | | } |
584 | | } |
585 | | |
586 | 0 | Ok((input.slice(input.input_len()..), input)) |
587 | 0 | } Unexecuted instantiation: nom::bytes::complete::escaped::<_, _, _, _, _, _>::{closure#0}Unexecuted instantiation: nom::bytes::complete::escaped::<_, _, _, _, _, _>::{closure#0} |
588 | 0 | } Unexecuted instantiation: nom::bytes::complete::escaped::<_, _, _, _, _, _> Unexecuted instantiation: nom::bytes::complete::escaped::<_, _, _, _, _, _> |
589 | | |
590 | | /// Matches a byte string with escaped characters. |
591 | | /// |
592 | | /// * The first argument matches the normal characters (it must not match the control character) |
593 | | /// * The second argument is the control character (like `\` in most languages) |
594 | | /// * The third argument matches the escaped characters and transforms them |
595 | | /// |
596 | | /// As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character) |
597 | | /// |
598 | | /// ``` |
599 | | /// # use nom::{Err, error::ErrorKind, Needed, IResult}; |
600 | | /// # use std::str::from_utf8; |
601 | | /// use nom::bytes::complete::{escaped_transform, tag}; |
602 | | /// use nom::character::complete::alpha1; |
603 | | /// use nom::branch::alt; |
604 | | /// use nom::combinator::value; |
605 | | /// |
606 | | /// fn parser(input: &str) -> IResult<&str, String> { |
607 | | /// escaped_transform( |
608 | | /// alpha1, |
609 | | /// '\\', |
610 | | /// alt(( |
611 | | /// value("\\", tag("\\")), |
612 | | /// value("\"", tag("\"")), |
613 | | /// value("\n", tag("n")), |
614 | | /// )) |
615 | | /// )(input) |
616 | | /// } |
617 | | /// |
618 | | /// assert_eq!(parser("ab\\\"cd"), Ok(("", String::from("ab\"cd")))); |
619 | | /// assert_eq!(parser("ab\\ncd"), Ok(("", String::from("ab\ncd")))); |
620 | | /// ``` |
621 | | #[cfg(feature = "alloc")] |
622 | | #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))] |
623 | 0 | pub fn escaped_transform<Input, Error, F, G, O1, O2, ExtendItem, Output>( |
624 | 0 | mut normal: F, |
625 | 0 | control_char: char, |
626 | 0 | mut transform: G, |
627 | 0 | ) -> impl FnMut(Input) -> IResult<Input, Output, Error> |
628 | 0 | where |
629 | 0 | Input: Clone |
630 | 0 | + crate::traits::Offset |
631 | 0 | + InputLength |
632 | 0 | + InputTake |
633 | 0 | + InputTakeAtPosition |
634 | 0 | + Slice<RangeFrom<usize>> |
635 | 0 | + InputIter, |
636 | 0 | Input: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, |
637 | 0 | O1: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, |
638 | 0 | O2: crate::traits::ExtendInto<Item = ExtendItem, Extender = Output>, |
639 | 0 | <Input as InputIter>::Item: crate::traits::AsChar, |
640 | 0 | F: Parser<Input, O1, Error>, |
641 | 0 | G: Parser<Input, O2, Error>, |
642 | 0 | Error: ParseError<Input>, |
643 | | { |
644 | | use crate::traits::AsChar; |
645 | | |
646 | 0 | move |input: Input| { |
647 | 0 | let mut index = 0; |
648 | 0 | let mut res = input.new_builder(); |
649 | | |
650 | 0 | let i = input.clone(); |
651 | | |
652 | 0 | while index < i.input_len() { |
653 | 0 | let current_len = i.input_len(); |
654 | 0 | let remainder = i.slice(index..); |
655 | 0 | match normal.parse(remainder.clone()) { |
656 | 0 | Ok((i2, o)) => { |
657 | 0 | o.extend_into(&mut res); |
658 | 0 | if i2.input_len() == 0 { |
659 | 0 | return Ok((i.slice(i.input_len()..), res)); |
660 | 0 | } else if i2.input_len() == current_len { |
661 | 0 | return Ok((remainder, res)); |
662 | 0 | } else { |
663 | 0 | index = input.offset(&i2); |
664 | 0 | } |
665 | | } |
666 | | Err(Err::Error(_)) => { |
667 | | // unwrap() should be safe here since index < $i.input_len() |
668 | 0 | if remainder.iter_elements().next().unwrap().as_char() == control_char { |
669 | 0 | let next = index + control_char.len_utf8(); |
670 | 0 | let input_len = input.input_len(); |
671 | | |
672 | 0 | if next >= input_len { |
673 | 0 | return Err(Err::Error(Error::from_error_kind( |
674 | 0 | remainder, |
675 | 0 | ErrorKind::EscapedTransform, |
676 | 0 | ))); |
677 | | } else { |
678 | 0 | match transform.parse(i.slice(next..)) { |
679 | 0 | Ok((i2, o)) => { |
680 | 0 | o.extend_into(&mut res); |
681 | 0 | if i2.input_len() == 0 { |
682 | 0 | return Ok((i.slice(i.input_len()..), res)); |
683 | 0 | } else { |
684 | 0 | index = input.offset(&i2); |
685 | 0 | } |
686 | | } |
687 | 0 | Err(e) => return Err(e), |
688 | | } |
689 | | } |
690 | | } else { |
691 | 0 | if index == 0 { |
692 | 0 | return Err(Err::Error(Error::from_error_kind( |
693 | 0 | remainder, |
694 | 0 | ErrorKind::EscapedTransform, |
695 | 0 | ))); |
696 | 0 | } |
697 | 0 | return Ok((remainder, res)); |
698 | | } |
699 | | } |
700 | 0 | Err(e) => return Err(e), |
701 | | } |
702 | | } |
703 | 0 | Ok((input.slice(index..), res)) |
704 | 0 | } Unexecuted instantiation: nom::bytes::complete::escaped_transform::<_, _, _, _, _, _, _, _>::{closure#0}Unexecuted instantiation: nom::bytes::complete::escaped_transform::<_, _, _, _, _, _, _, _>::{closure#0} |
705 | 0 | } Unexecuted instantiation: nom::bytes::complete::escaped_transform::<_, _, _, _, _, _, _, _> Unexecuted instantiation: nom::bytes::complete::escaped_transform::<_, _, _, _, _, _, _, _> |
706 | | |
707 | | #[cfg(test)] |
708 | | mod tests { |
709 | | use super::*; |
710 | | |
711 | | #[test] |
712 | | fn complete_take_while_m_n_utf8_all_matching() { |
713 | | let result: IResult<&str, &str> = |
714 | | super::take_while_m_n(1, 4, |c: char| c.is_alphabetic())("øn"); |
715 | | assert_eq!(result, Ok(("", "øn"))); |
716 | | } |
717 | | |
718 | | #[test] |
719 | | fn complete_take_while_m_n_utf8_all_matching_substring() { |
720 | | let result: IResult<&str, &str> = |
721 | | super::take_while_m_n(1, 1, |c: char| c.is_alphabetic())("øn"); |
722 | | assert_eq!(result, Ok(("n", "ø"))); |
723 | | } |
724 | | |
725 | | // issue #1336 "escaped hangs if normal parser accepts empty" |
726 | | fn escaped_string(input: &str) -> IResult<&str, &str> { |
727 | | use crate::character::complete::{alpha0, one_of}; |
728 | | escaped(alpha0, '\\', one_of("n"))(input) |
729 | | } |
730 | | |
731 | | // issue #1336 "escaped hangs if normal parser accepts empty" |
732 | | #[test] |
733 | | fn escaped_hang() { |
734 | | escaped_string("7").unwrap(); |
735 | | escaped_string("a7").unwrap(); |
736 | | } |
737 | | |
738 | | // issue ##1118 escaped does not work with empty string |
739 | | fn unquote<'a>(input: &'a str) -> IResult<&'a str, &'a str> { |
740 | | use crate::bytes::complete::*; |
741 | | use crate::character::complete::*; |
742 | | use crate::combinator::opt; |
743 | | use crate::sequence::delimited; |
744 | | |
745 | | delimited( |
746 | | char('"'), |
747 | | escaped(opt(none_of(r#"\""#)), '\\', one_of(r#"\"rnt"#)), |
748 | | char('"'), |
749 | | )(input) |
750 | | } |
751 | | |
752 | | #[test] |
753 | | fn escaped_hang_1118() { |
754 | | assert_eq!(unquote(r#""""#), Ok(("", ""))); |
755 | | } |
756 | | } |