/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 | 1.21M | pub fn tag<T, Input, Error: ParseError<Input>>( |
33 | 1.21M | tag: T, |
34 | 1.21M | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
35 | 1.21M | where |
36 | 1.21M | Input: InputTake + Compare<T>, |
37 | 1.21M | T: InputLength + Clone, |
38 | | { |
39 | 997k | move |i: Input| { |
40 | 997k | let tag_len = tag.input_len(); |
41 | 997k | let t = tag.clone(); |
42 | 997k | let res: IResult<_, _, Error> = match i.compare(t) { |
43 | 467k | CompareResult::Ok => Ok(i.take_split(tag_len)), |
44 | | _ => { |
45 | 530k | let e: ErrorKind = ErrorKind::Tag; |
46 | 530k | Err(Err::Error(Error::from_error_kind(i, e))) |
47 | | } |
48 | | }; |
49 | 997k | res |
50 | 997k | } nom::bytes::complete::tag::<&str, &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 39 | 5.19k | move |i: Input| { | 40 | 5.19k | let tag_len = tag.input_len(); | 41 | 5.19k | let t = tag.clone(); | 42 | 5.19k | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | 352 | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | 4.84k | let e: ErrorKind = ErrorKind::Tag; | 46 | 4.84k | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | 5.19k | res | 50 | 5.19k | } |
Unexecuted instantiation: nom::bytes::complete::tag::<_, _, _>::{closure#0}nom::bytes::complete::tag::<&str, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 39 | 454k | move |i: Input| { | 40 | 454k | let tag_len = tag.input_len(); | 41 | 454k | let t = tag.clone(); | 42 | 454k | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | 86.6k | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | 367k | let e: ErrorKind = ErrorKind::Tag; | 46 | 367k | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | 454k | res | 50 | 454k | } |
nom::bytes::complete::tag::<&str, &str, suricata::detect::error::RuleParseError<&str>>::{closure#0}Line | Count | Source | 39 | 75.4k | move |i: Input| { | 40 | 75.4k | let tag_len = tag.input_len(); | 41 | 75.4k | let t = tag.clone(); | 42 | 75.4k | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | 68.4k | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | 6.95k | let e: ErrorKind = ErrorKind::Tag; | 46 | 6.95k | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | 75.4k | res | 50 | 75.4k | } |
nom::bytes::complete::tag::<&str, &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 39 | 462k | move |i: Input| { | 40 | 462k | let tag_len = tag.input_len(); | 41 | 462k | let t = tag.clone(); | 42 | 462k | let res: IResult<_, _, Error> = match i.compare(t) { | 43 | 311k | CompareResult::Ok => Ok(i.take_split(tag_len)), | 44 | | _ => { | 45 | 150k | let e: ErrorKind = ErrorKind::Tag; | 46 | 150k | Err(Err::Error(Error::from_error_kind(i, e))) | 47 | | } | 48 | | }; | 49 | 462k | res | 50 | 462k | } |
Unexecuted instantiation: nom::bytes::complete::tag::<_, _, _>::{closure#0} |
51 | 1.21M | } nom::bytes::complete::tag::<&str, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 32 | 5.19k | pub fn tag<T, Input, Error: ParseError<Input>>( | 33 | 5.19k | tag: T, | 34 | 5.19k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 35 | 5.19k | where | 36 | 5.19k | Input: InputTake + Compare<T>, | 37 | 5.19k | 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 | 5.19k | } |
Unexecuted instantiation: nom::bytes::complete::tag::<_, _, _> nom::bytes::complete::tag::<&str, &str, nom::error::Error<&str>> Line | Count | Source | 32 | 598k | pub fn tag<T, Input, Error: ParseError<Input>>( | 33 | 598k | tag: T, | 34 | 598k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 35 | 598k | where | 36 | 598k | Input: InputTake + Compare<T>, | 37 | 598k | 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 | 598k | } |
nom::bytes::complete::tag::<&str, &str, suricata::detect::error::RuleParseError<&str>> Line | Count | Source | 32 | 9.51k | pub fn tag<T, Input, Error: ParseError<Input>>( | 33 | 9.51k | tag: T, | 34 | 9.51k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 35 | 9.51k | where | 36 | 9.51k | Input: InputTake + Compare<T>, | 37 | 9.51k | 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 | 9.51k | } |
nom::bytes::complete::tag::<&str, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 32 | 600k | pub fn tag<T, Input, Error: ParseError<Input>>( | 33 | 600k | tag: T, | 34 | 600k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 35 | 600k | where | 36 | 600k | Input: InputTake + Compare<T>, | 37 | 600k | 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 | 600k | } |
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 | 110k | pub fn tag_no_case<T, Input, Error: ParseError<Input>>( |
75 | 110k | tag: T, |
76 | 110k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
77 | 110k | where |
78 | 110k | Input: InputTake + Compare<T>, |
79 | 110k | T: InputLength + Clone, |
80 | | { |
81 | 108k | move |i: Input| { |
82 | 108k | let tag_len = tag.input_len(); |
83 | 108k | let t = tag.clone(); |
84 | | |
85 | 108k | let res: IResult<_, _, Error> = match (i).compare_no_case(t) { |
86 | 1.93k | CompareResult::Ok => Ok(i.take_split(tag_len)), |
87 | | _ => { |
88 | 106k | let e: ErrorKind = ErrorKind::Tag; |
89 | 106k | Err(Err::Error(Error::from_error_kind(i, e))) |
90 | | } |
91 | | }; |
92 | 108k | res |
93 | 108k | } 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 | 108k | move |i: Input| { | 82 | 108k | let tag_len = tag.input_len(); | 83 | 108k | let t = tag.clone(); | 84 | | | 85 | 108k | let res: IResult<_, _, Error> = match (i).compare_no_case(t) { | 86 | 1.93k | CompareResult::Ok => Ok(i.take_split(tag_len)), | 87 | | _ => { | 88 | 106k | let e: ErrorKind = ErrorKind::Tag; | 89 | 106k | Err(Err::Error(Error::from_error_kind(i, e))) | 90 | | } | 91 | | }; | 92 | 108k | res | 93 | 108k | } |
Unexecuted instantiation: nom::bytes::complete::tag_no_case::<_, _, _>::{closure#0} |
94 | 110k | } Unexecuted instantiation: nom::bytes::complete::tag_no_case::<_, _, _> nom::bytes::complete::tag_no_case::<&str, &str, nom::error::Error<&str>> Line | Count | Source | 74 | 110k | pub fn tag_no_case<T, Input, Error: ParseError<Input>>( | 75 | 110k | tag: T, | 76 | 110k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 77 | 110k | where | 78 | 110k | Input: InputTake + Compare<T>, | 79 | 110k | 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 | 110k | } |
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 | 494k | pub fn is_not<T, Input, Error: ParseError<Input>>( |
118 | 494k | arr: T, |
119 | 494k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
120 | 494k | where |
121 | 494k | Input: InputTakeAtPosition, |
122 | 494k | T: FindToken<<Input as InputTakeAtPosition>::Item>, |
123 | | { |
124 | 563k | move |i: Input| { |
125 | 563k | let e: ErrorKind = ErrorKind::IsNot; |
126 | 22.1M | i.split_at_position1_complete(|c| arr.find_token(c), e) nom::bytes::complete::is_not::<&str, &[u8], nom::error::Error<&[u8]>>::{closure#0}::{closure#0}Line | Count | Source | 126 | 20.9M | 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, &str, suricata::detect::error::RuleParseError<&str>>::{closure#0}::{closure#0}Line | Count | Source | 126 | 1.21M | i.split_at_position1_complete(|c| arr.find_token(c), e) |
Unexecuted instantiation: nom::bytes::complete::is_not::<_, _, _>::{closure#0}::{closure#0} |
127 | 563k | } nom::bytes::complete::is_not::<&str, &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 124 | 441k | move |i: Input| { | 125 | 441k | let e: ErrorKind = ErrorKind::IsNot; | 126 | 441k | i.split_at_position1_complete(|c| arr.find_token(c), e) | 127 | 441k | } |
Unexecuted instantiation: nom::bytes::complete::is_not::<_, _, _>::{closure#0}nom::bytes::complete::is_not::<&str, &str, suricata::detect::error::RuleParseError<&str>>::{closure#0}Line | Count | Source | 124 | 122k | move |i: Input| { | 125 | 122k | let e: ErrorKind = ErrorKind::IsNot; | 126 | 122k | i.split_at_position1_complete(|c| arr.find_token(c), e) | 127 | 122k | } |
Unexecuted instantiation: nom::bytes::complete::is_not::<_, _, _>::{closure#0} |
128 | 494k | } nom::bytes::complete::is_not::<&str, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 117 | 441k | pub fn is_not<T, Input, Error: ParseError<Input>>( | 118 | 441k | arr: T, | 119 | 441k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 120 | 441k | where | 121 | 441k | Input: InputTakeAtPosition, | 122 | 441k | 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 | 441k | } |
Unexecuted instantiation: nom::bytes::complete::is_not::<_, _, _> nom::bytes::complete::is_not::<&str, &str, suricata::detect::error::RuleParseError<&str>> Line | Count | Source | 117 | 53.8k | pub fn is_not<T, Input, Error: ParseError<Input>>( | 118 | 53.8k | arr: T, | 119 | 53.8k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 120 | 53.8k | where | 121 | 53.8k | Input: InputTakeAtPosition, | 122 | 53.8k | 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 | 53.8k | } |
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 | 219k | pub fn is_a<T, Input, Error: ParseError<Input>>( |
152 | 219k | arr: T, |
153 | 219k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
154 | 219k | where |
155 | 219k | Input: InputTakeAtPosition, |
156 | 219k | T: FindToken<<Input as InputTakeAtPosition>::Item>, |
157 | | { |
158 | 219k | move |i: Input| { |
159 | 219k | let e: ErrorKind = ErrorKind::IsA; |
160 | 223k | 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 | 223k | i.split_at_position1_complete(|c| !arr.find_token(c), e) |
Unexecuted instantiation: nom::bytes::complete::is_a::<_, _, _>::{closure#0}::{closure#0} |
161 | 219k | } 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 | 219k | move |i: Input| { | 159 | 219k | let e: ErrorKind = ErrorKind::IsA; | 160 | 219k | i.split_at_position1_complete(|c| !arr.find_token(c), e) | 161 | 219k | } |
Unexecuted instantiation: nom::bytes::complete::is_a::<_, _, _>::{closure#0} |
162 | 219k | } Unexecuted instantiation: nom::bytes::complete::is_a::<_, _, _> nom::bytes::complete::is_a::<&str, &str, nom::error::Error<&str>> Line | Count | Source | 151 | 219k | pub fn is_a<T, Input, Error: ParseError<Input>>( | 152 | 219k | arr: T, | 153 | 219k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 154 | 219k | where | 155 | 219k | Input: InputTakeAtPosition, | 156 | 219k | 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 | 219k | } |
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 | 110k | pub fn take_while<F, Input, Error: ParseError<Input>>( |
184 | 110k | cond: F, |
185 | 110k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
186 | 110k | where |
187 | 110k | Input: InputTakeAtPosition, |
188 | 110k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, |
189 | | { |
190 | 319k | 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 | 14.7k | 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 | 7.08k | 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 | 19.1k | 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 | 13.1k | 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 | 6.05k | 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 | 1.24k | 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 | 1.06k | 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 | 48.4k | 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 | 1.49k | 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 | 1.18k | 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.75k | 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 | 1.34k | 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 | 1.70k | 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 | 202 | 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 | 338 | 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 | 311k | move |i: Input| i.split_at_position_complete(|c| !cond(c)) |
Unexecuted instantiation: nom::bytes::complete::take_while::<_, _, _>::{closure#0}::{closure#0} |
191 | 110k | } Unexecuted instantiation: nom::bytes::complete::take_while::<_, _, _> nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u8>::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 14.7k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 14.7k | cond: F, | 185 | 14.7k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 14.7k | where | 187 | 14.7k | Input: InputTakeAtPosition, | 188 | 14.7k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 14.7k | } |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u32>::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 7.08k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 7.08k | cond: F, | 185 | 7.08k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 7.08k | where | 187 | 7.08k | Input: InputTakeAtPosition, | 188 | 7.08k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 7.08k | } |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u16>::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 19.1k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 19.1k | cond: F, | 185 | 19.1k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 19.1k | where | 187 | 19.1k | Input: InputTakeAtPosition, | 188 | 19.1k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 19.1k | } |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint<u64>::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 13.1k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 13.1k | cond: F, | 185 | 13.1k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 13.1k | where | 187 | 13.1k | Input: InputTakeAtPosition, | 188 | 13.1k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 13.1k | } |
nom::bytes::complete::take_while::<suricata::detect::uint::detect_parse_uint_inclusive<u32>::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 6.05k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 6.05k | cond: F, | 185 | 6.05k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 6.05k | where | 187 | 6.05k | Input: InputTakeAtPosition, | 188 | 6.05k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 6.05k | } |
nom::bytes::complete::take_while::<suricata::detect::requires::parse_key_value::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 48.4k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 48.4k | cond: F, | 185 | 48.4k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 48.4k | where | 187 | 48.4k | Input: InputTakeAtPosition, | 188 | 48.4k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 48.4k | } |
nom::bytes::complete::take_while::<suricata::krb::detect::detect_parse_encryption::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 1.24k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 1.24k | cond: F, | 185 | 1.24k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 1.24k | where | 187 | 1.24k | Input: InputTakeAtPosition, | 188 | 1.24k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 1.24k | } |
nom::bytes::complete::take_while::<suricata::detect::stream_size::detect_parse_stream_size::{closure#1}, &str, nom::error::Error<&str>>Line | Count | Source | 183 | 1.06k | pub fn take_while<F, Input, Error: ParseError<Input>>( | 184 | 1.06k | cond: F, | 185 | 1.06k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 186 | 1.06k | where | 187 | 1.06k | Input: InputTakeAtPosition, | 188 | 1.06k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 189 | | { | 190 | | move |i: Input| i.split_at_position_complete(|c| !cond(c)) | 191 | 1.06k | } |
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 | 6.80k | pub fn take_while1<F, Input, Error: ParseError<Input>>( |
214 | 6.80k | cond: F, |
215 | 6.80k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
216 | 6.80k | where |
217 | 6.80k | Input: InputTakeAtPosition, |
218 | 6.80k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, |
219 | | { |
220 | 6.80k | move |i: Input| { |
221 | 6.80k | let e: ErrorKind = ErrorKind::TakeWhile1; |
222 | 28.7k | 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 | 7.45k | 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 | 21.3k | i.split_at_position1_complete(|c| !cond(c), e) |
Unexecuted instantiation: nom::bytes::complete::take_while1::<_, _, _>::{closure#0}::{closure#0} |
223 | 6.80k | } 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 | 3.83k | move |i: Input| { | 221 | 3.83k | let e: ErrorKind = ErrorKind::TakeWhile1; | 222 | 3.83k | i.split_at_position1_complete(|c| !cond(c), e) | 223 | 3.83k | } |
nom::bytes::complete::take_while1::<suricata::krb::detect::is_alphanumeric_or_dash, &str, nom::error::Error<&str>>::{closure#0}Line | Count | Source | 220 | 2.96k | move |i: Input| { | 221 | 2.96k | let e: ErrorKind = ErrorKind::TakeWhile1; | 222 | 2.96k | i.split_at_position1_complete(|c| !cond(c), e) | 223 | 2.96k | } |
Unexecuted instantiation: nom::bytes::complete::take_while1::<_, _, _>::{closure#0} |
224 | 6.80k | } Unexecuted instantiation: nom::bytes::complete::take_while1::<_, _, _> nom::bytes::complete::take_while1::<suricata::krb::detect::is_alphanumeric_or_dash, &str, nom::error::Error<&str>> Line | Count | Source | 213 | 2.96k | pub fn take_while1<F, Input, Error: ParseError<Input>>( | 214 | 2.96k | cond: F, | 215 | 2.96k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 216 | 2.96k | where | 217 | 2.96k | Input: InputTakeAtPosition, | 218 | 2.96k | 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 | 2.96k | } |
nom::bytes::complete::take_while1::<nom::character::is_digit, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 213 | 3.83k | pub fn take_while1<F, Input, Error: ParseError<Input>>( | 214 | 3.83k | cond: F, | 215 | 3.83k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 216 | 3.83k | where | 217 | 3.83k | Input: InputTakeAtPosition, | 218 | 3.83k | 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 | 3.83k | } |
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 | 150k | pub fn take_till<F, Input, Error: ParseError<Input>>( |
331 | 150k | cond: F, |
332 | 150k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
333 | 150k | where |
334 | 150k | Input: InputTakeAtPosition, |
335 | 150k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, |
336 | | { |
337 | 1.28M | 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 | 48.4k | 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 | 101k | 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 | 1.04M | 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 | 241k | move |i: Input| i.split_at_position_complete(|c| cond(c)) |
Unexecuted instantiation: nom::bytes::complete::take_till::<_, _, _>::{closure#0}::{closure#0} |
338 | 150k | } 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 | 48.4k | pub fn take_till<F, Input, Error: ParseError<Input>>( | 331 | 48.4k | cond: F, | 332 | 48.4k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 333 | 48.4k | where | 334 | 48.4k | Input: InputTakeAtPosition, | 335 | 48.4k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 336 | | { | 337 | | move |i: Input| i.split_at_position_complete(|c| cond(c)) | 338 | 48.4k | } |
nom::bytes::complete::take_till::<suricata::detect::requires::parse_next_version_part::{closure#0}, &str, nom::error::Error<&str>>Line | Count | Source | 330 | 101k | pub fn take_till<F, Input, Error: ParseError<Input>>( | 331 | 101k | cond: F, | 332 | 101k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 333 | 101k | where | 334 | 101k | Input: InputTakeAtPosition, | 335 | 101k | F: Fn(<Input as InputTakeAtPosition>::Item) -> bool, | 336 | | { | 337 | | move |i: Input| i.split_at_position_complete(|c| cond(c)) | 338 | 101k | } |
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 | 6.89M | pub fn take<C, Input, Error: ParseError<Input>>( |
404 | 6.89M | count: C, |
405 | 6.89M | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
406 | 6.89M | where |
407 | 6.89M | Input: InputIter + InputTake, |
408 | 6.89M | C: ToUsize, |
409 | | { |
410 | 6.89M | let c = count.to_usize(); |
411 | 6.89M | move |i: Input| match i.slice_index(c) { |
412 | 20.1k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), |
413 | 6.87M | Ok(index) => Ok(i.take_split(index)), |
414 | 6.89M | } nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error>::{closure#0}Line | Count | Source | 411 | 57.1k | move |i: Input| match i.slice_index(c) { | 412 | 67 | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 57.1k | Ok(index) => Ok(i.take_split(index)), | 414 | 57.1k | } |
nom::bytes::complete::take::<usize, &[u8], x509_parser::error::X509Error>::{closure#0}Line | Count | Source | 411 | 126k | move |i: Input| match i.slice_index(c) { | 412 | 3.32k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 123k | Ok(index) => Ok(i.take_split(index)), | 414 | 126k | } |
nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error>::{closure#0}Line | Count | Source | 411 | 32.3k | move |i: Input| match i.slice_index(c) { | 412 | 717 | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 31.6k | Ok(index) => Ok(i.take_split(index)), | 414 | 32.3k | } |
Unexecuted instantiation: nom::bytes::complete::take::<_, _, _>::{closure#0}nom::bytes::complete::take::<u8, &[u8], suricata::quic::error::QuicError>::{closure#0}Line | Count | Source | 411 | 240k | move |i: Input| match i.slice_index(c) { | 412 | 1.43k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 239k | Ok(index) => Ok(i.take_split(index)), | 414 | 240k | } |
nom::bytes::complete::take::<usize, &[u8], suricata::quic::error::QuicError>::{closure#0}Line | Count | Source | 411 | 4.93M | move |i: Input| match i.slice_index(c) { | 412 | 3.98k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 4.92M | Ok(index) => Ok(i.take_split(index)), | 414 | 4.93M | } |
nom::bytes::complete::take::<u32, &[u8], suricata::quic::error::QuicError>::{closure#0}Line | Count | Source | 411 | 74.2k | move |i: Input| match i.slice_index(c) { | 412 | 1.83k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 72.3k | Ok(index) => Ok(i.take_split(index)), | 414 | 74.2k | } |
nom::bytes::complete::take::<usize, &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 411 | 1.27M | move |i: Input| match i.slice_index(c) { | 412 | 4.78k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 1.27M | Ok(index) => Ok(i.take_split(index)), | 414 | 1.27M | } |
nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error>::{closure#0}Line | Count | Source | 411 | 45.2k | move |i: Input| match i.slice_index(c) { | 412 | 177 | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 45.1k | Ok(index) => Ok(i.take_split(index)), | 414 | 45.2k | } |
nom::bytes::complete::take::<usize, &[u8], x509_parser::error::X509Error>::{closure#0}Line | Count | Source | 411 | 109k | move |i: Input| match i.slice_index(c) { | 412 | 3.85k | Err(_needed) => Err(Err::Error(Error::from_error_kind(i, ErrorKind::Eof))), | 413 | 105k | Ok(index) => Ok(i.take_split(index)), | 414 | 109k | } |
Unexecuted instantiation: nom::bytes::complete::take::<_, _, _>::{closure#0} |
415 | 6.89M | } nom::bytes::complete::take::<usize, &[u8], x509_parser::error::X509Error> Line | Count | Source | 403 | 126k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 126k | count: C, | 405 | 126k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 126k | where | 407 | 126k | Input: InputIter + InputTake, | 408 | 126k | C: ToUsize, | 409 | | { | 410 | 126k | 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 | 126k | } |
nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error> Line | Count | Source | 403 | 57.1k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 57.1k | count: C, | 405 | 57.1k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 57.1k | where | 407 | 57.1k | Input: InputIter + InputTake, | 408 | 57.1k | C: ToUsize, | 409 | | { | 410 | 57.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 | 57.1k | } |
nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error> Line | Count | Source | 403 | 32.3k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 32.3k | count: C, | 405 | 32.3k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 32.3k | where | 407 | 32.3k | Input: InputIter + InputTake, | 408 | 32.3k | C: ToUsize, | 409 | | { | 410 | 32.3k | 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.3k | } |
Unexecuted instantiation: nom::bytes::complete::take::<_, _, _> nom::bytes::complete::take::<u8, &[u8], suricata::quic::error::QuicError> Line | Count | Source | 403 | 240k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 240k | count: C, | 405 | 240k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 240k | where | 407 | 240k | Input: InputIter + InputTake, | 408 | 240k | C: ToUsize, | 409 | | { | 410 | 240k | 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 | 240k | } |
nom::bytes::complete::take::<usize, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 403 | 1.27M | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 1.27M | count: C, | 405 | 1.27M | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 1.27M | where | 407 | 1.27M | Input: InputIter + InputTake, | 408 | 1.27M | C: ToUsize, | 409 | | { | 410 | 1.27M | 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.27M | } |
nom::bytes::complete::take::<usize, &[u8], suricata::quic::error::QuicError> Line | Count | Source | 403 | 4.93M | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 4.93M | count: C, | 405 | 4.93M | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 4.93M | where | 407 | 4.93M | Input: InputIter + InputTake, | 408 | 4.93M | C: ToUsize, | 409 | | { | 410 | 4.93M | 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 | 4.93M | } |
nom::bytes::complete::take::<u32, &[u8], suricata::quic::error::QuicError> Line | Count | Source | 403 | 74.2k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 74.2k | count: C, | 405 | 74.2k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 74.2k | where | 407 | 74.2k | Input: InputIter + InputTake, | 408 | 74.2k | C: ToUsize, | 409 | | { | 410 | 74.2k | 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 | 74.2k | } |
nom::bytes::complete::take::<usize, &[u8], x509_parser::error::X509Error> Line | Count | Source | 403 | 109k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 109k | count: C, | 405 | 109k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 109k | where | 407 | 109k | Input: InputIter + InputTake, | 408 | 109k | C: ToUsize, | 409 | | { | 410 | 109k | 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 | 109k | } |
nom::bytes::complete::take::<usize, &[u8], asn1_rs::error::Error> Line | Count | Source | 403 | 45.2k | pub fn take<C, Input, Error: ParseError<Input>>( | 404 | 45.2k | count: C, | 405 | 45.2k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 406 | 45.2k | where | 407 | 45.2k | Input: InputIter + InputTake, | 408 | 45.2k | C: ToUsize, | 409 | | { | 410 | 45.2k | 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 | 45.2k | } |
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 | 819k | pub fn take_until<T, Input, Error: ParseError<Input>>( |
436 | 819k | tag: T, |
437 | 819k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> |
438 | 819k | where |
439 | 819k | Input: InputTake + FindSubstring<T>, |
440 | 819k | T: InputLength + Clone, |
441 | | { |
442 | 13.9M | move |i: Input| { |
443 | 13.9M | let t = tag.clone(); |
444 | 13.9M | let res: IResult<_, _, Error> = match i.find_substring(t) { |
445 | 309k | None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), |
446 | 13.6M | Some(index) => Ok(i.take_split(index)), |
447 | | }; |
448 | 13.9M | res |
449 | 13.9M | } nom::bytes::complete::take_until::<&[u8], &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 442 | 13.8M | move |i: Input| { | 443 | 13.8M | let t = tag.clone(); | 444 | 13.8M | let res: IResult<_, _, Error> = match i.find_substring(t) { | 445 | 293k | None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), | 446 | 13.5M | Some(index) => Ok(i.take_split(index)), | 447 | | }; | 448 | 13.8M | res | 449 | 13.8M | } |
Unexecuted instantiation: nom::bytes::complete::take_until::<_, _, _>::{closure#0}nom::bytes::complete::take_until::<&str, &[u8], nom::error::Error<&[u8]>>::{closure#0}Line | Count | Source | 442 | 77.8k | move |i: Input| { | 443 | 77.8k | let t = tag.clone(); | 444 | 77.8k | let res: IResult<_, _, Error> = match i.find_substring(t) { | 445 | 16.4k | None => Err(Err::Error(Error::from_error_kind(i, ErrorKind::TakeUntil))), | 446 | 61.3k | Some(index) => Ok(i.take_split(index)), | 447 | | }; | 448 | 77.8k | res | 449 | 77.8k | } |
Unexecuted instantiation: nom::bytes::complete::take_until::<_, _, _>::{closure#0} |
450 | 819k | } nom::bytes::complete::take_until::<&[u8], &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 435 | 741k | pub fn take_until<T, Input, Error: ParseError<Input>>( | 436 | 741k | tag: T, | 437 | 741k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 438 | 741k | where | 439 | 741k | Input: InputTake + FindSubstring<T>, | 440 | 741k | 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 | 741k | } |
Unexecuted instantiation: nom::bytes::complete::take_until::<_, _, _> nom::bytes::complete::take_until::<&str, &[u8], nom::error::Error<&[u8]>> Line | Count | Source | 435 | 77.8k | pub fn take_until<T, Input, Error: ParseError<Input>>( | 436 | 77.8k | tag: T, | 437 | 77.8k | ) -> impl Fn(Input) -> IResult<Input, Input, Error> | 438 | 77.8k | where | 439 | 77.8k | Input: InputTake + FindSubstring<T>, | 440 | 77.8k | 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 | 77.8k | } |
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 | | } |