nom8/bytes/mod.rs
1//! Parsers recognizing bytes streams
2
3pub mod complete;
4pub mod streaming;
5#[cfg(test)]
6mod tests;
7
8use crate::error::ParseError;
9use crate::input::{
10 Compare, FindSubstring, FindToken, InputIsStreaming, InputIter, InputLength, InputTake,
11 InputTakeAtPosition, IntoOutput, Slice, ToUsize,
12};
13use crate::lib::std::ops::RangeFrom;
14use crate::{IResult, Parser};
15
16/// Matches one token
17///
18/// *Complete version*: Will return an error if there's not enough input data.
19///
20/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there's not enough input data.
21///
22/// # Example
23///
24/// ```
25/// # use nom8::{bytes::any, Err, error::{Error, ErrorKind}, IResult};
26/// fn parser(input: &str) -> IResult<&str, char> {
27/// any(input)
28/// }
29///
30/// assert_eq!(parser("abc"), Ok(("bc",'a')));
31/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Eof))));
32/// ```
33///
34/// ```
35/// # use nom8::{bytes::any, Err, error::ErrorKind, IResult, Needed};
36/// # use nom8::input::Streaming;
37/// assert_eq!(any::<_, (_, ErrorKind), true>(Streaming("abc")), Ok((Streaming("bc"),'a')));
38/// assert_eq!(any::<_, (_, ErrorKind), true>(Streaming("")), Err(Err::Incomplete(Needed::new(1))));
39/// ```
40#[inline(always)]
41pub fn any<I, E: ParseError<I>, const STREAMING: bool>(
42 input: I,
43) -> IResult<I, <I as InputIter>::Item, E>
44where
45 I: InputIter + InputLength + Slice<RangeFrom<usize>> + InputIsStreaming<STREAMING>,
46{
47 if STREAMING {
48 streaming::any(input)
49 } else {
50 complete::any(input)
51 }
52}
53
54/// Recognizes a pattern
55///
56/// The input data will be compared to the tag combinator's argument and will return the part of
57/// the input that matches the argument
58///
59/// It will return `Err(Err::Error((_, ErrorKind::Tag)))` if the input doesn't match the pattern
60///
61/// **Note:** [`Parser`] is implemented for strings and byte strings as a convenience (complete
62/// only)
63///
64/// # Example
65/// ```rust
66/// # use nom8::prelude::*;
67/// # use nom8::{Err, error::{Error, ErrorKind}, Needed};
68/// use nom8::bytes::tag;
69///
70/// fn parser(s: &str) -> IResult<&str, &str> {
71/// tag("Hello")(s)
72/// }
73///
74/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello")));
75/// assert_eq!(parser("Something"), Err(Err::Error(Error::new("Something", ErrorKind::Tag))));
76/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Tag))));
77/// ```
78///
79/// ```rust
80/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
81/// # use nom8::input::Streaming;
82/// use nom8::bytes::tag;
83///
84/// fn parser(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
85/// tag("Hello")(s)
86/// }
87///
88/// assert_eq!(parser(Streaming("Hello, World!")), Ok((Streaming(", World!"), "Hello")));
89/// assert_eq!(parser(Streaming("Something")), Err(Err::Error(Error::new(Streaming("Something"), ErrorKind::Tag))));
90/// assert_eq!(parser(Streaming("S")), Err(Err::Error(Error::new(Streaming("S"), ErrorKind::Tag))));
91/// assert_eq!(parser(Streaming("H")), Err(Err::Incomplete(Needed::new(4))));
92/// ```
93#[inline(always)]
94pub fn tag<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
95 tag: T,
96) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
97where
98 Input: InputTake + InputLength + Compare<T> + InputIsStreaming<STREAMING>,
99 Input: IntoOutput,
100 T: InputLength + Clone,
101{
102 move |i: Input| {
103 let t = tag.clone();
104 if STREAMING {
105 streaming::tag_internal(i, t)
106 } else {
107 complete::tag_internal(i, t)
108 }
109 }
110}
111
112/// Recognizes a case insensitive pattern.
113///
114/// The input data will be compared to the tag combinator's argument and will return the part of
115/// the input that matches the argument with no regard to case.
116///
117/// It will return `Err(Err::Error((_, ErrorKind::Tag)))` if the input doesn't match the pattern.
118/// # Example
119/// ```rust
120/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
121/// use nom8::bytes::tag_no_case;
122///
123/// fn parser(s: &str) -> IResult<&str, &str> {
124/// tag_no_case("hello")(s)
125/// }
126///
127/// assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello")));
128/// assert_eq!(parser("hello, World!"), Ok((", World!", "hello")));
129/// assert_eq!(parser("HeLlO, World!"), Ok((", World!", "HeLlO")));
130/// assert_eq!(parser("Something"), Err(Err::Error(Error::new("Something", ErrorKind::Tag))));
131/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Tag))));
132/// ```
133///
134/// ```rust
135/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
136/// # use nom8::input::Streaming;
137/// use nom8::bytes::tag_no_case;
138///
139/// fn parser(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
140/// tag_no_case("hello")(s)
141/// }
142///
143/// assert_eq!(parser(Streaming("Hello, World!")), Ok((Streaming(", World!"), "Hello")));
144/// assert_eq!(parser(Streaming("hello, World!")), Ok((Streaming(", World!"), "hello")));
145/// assert_eq!(parser(Streaming("HeLlO, World!")), Ok((Streaming(", World!"), "HeLlO")));
146/// assert_eq!(parser(Streaming("Something")), Err(Err::Error(Error::new(Streaming("Something"), ErrorKind::Tag))));
147/// assert_eq!(parser(Streaming("")), Err(Err::Incomplete(Needed::new(5))));
148/// ```
149#[inline(always)]
150pub fn tag_no_case<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
151 tag: T,
152) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
153where
154 Input: InputTake + InputLength + Compare<T> + InputIsStreaming<STREAMING>,
155 Input: IntoOutput,
156 T: InputLength + Clone,
157{
158 move |i: Input| {
159 let t = tag.clone();
160 if STREAMING {
161 streaming::tag_no_case_internal(i, t)
162 } else {
163 complete::tag_no_case_internal(i, t)
164 }
165 }
166}
167
168/// Returns a token that matches the [pattern][FindToken]
169///
170/// **Note:** [`Parser`] is implemented as a convenience (complete
171/// only) for
172/// - `u8`
173/// - `char`
174///
175/// *Complete version*: Will return an error if there's not enough input data.
176///
177/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there's not enough input data.
178///
179/// # Example
180///
181/// ```
182/// # use nom8::*;
183/// # use nom8::{Err, error::ErrorKind, error::Error};
184/// # use nom8::bytes::one_of;
185/// assert_eq!(one_of::<_, _, (&str, ErrorKind), false>("abc")("b"), Ok(("", 'b')));
186/// assert_eq!(one_of::<_, _, (&str, ErrorKind), false>("a")("bc"), Err(Err::Error(("bc", ErrorKind::OneOf))));
187/// assert_eq!(one_of::<_, _, (&str, ErrorKind), false>("a")(""), Err(Err::Error(("", ErrorKind::OneOf))));
188///
189/// fn parser_fn(i: &str) -> IResult<&str, char> {
190/// one_of(|c| c == 'a' || c == 'b')(i)
191/// }
192/// assert_eq!(parser_fn("abc"), Ok(("bc", 'a')));
193/// assert_eq!(parser_fn("cd"), Err(Err::Error(Error::new("cd", ErrorKind::OneOf))));
194/// assert_eq!(parser_fn(""), Err(Err::Error(Error::new("", ErrorKind::OneOf))));
195/// ```
196///
197/// ```
198/// # use nom8::*;
199/// # use nom8::{Err, error::ErrorKind, error::Error, Needed};
200/// # use nom8::input::Streaming;
201/// # use nom8::bytes::one_of;
202/// assert_eq!(one_of::<_, _, (_, ErrorKind), true>("abc")(Streaming("b")), Ok((Streaming(""), 'b')));
203/// assert_eq!(one_of::<_, _, (_, ErrorKind), true>("a")(Streaming("bc")), Err(Err::Error((Streaming("bc"), ErrorKind::OneOf))));
204/// assert_eq!(one_of::<_, _, (_, ErrorKind), true>("a")(Streaming("")), Err(Err::Incomplete(Needed::new(1))));
205///
206/// fn parser_fn(i: Streaming<&str>) -> IResult<Streaming<&str>, char> {
207/// one_of(|c| c == 'a' || c == 'b')(i)
208/// }
209/// assert_eq!(parser_fn(Streaming("abc")), Ok((Streaming("bc"), 'a')));
210/// assert_eq!(parser_fn(Streaming("cd")), Err(Err::Error(Error::new(Streaming("cd"), ErrorKind::OneOf))));
211/// assert_eq!(parser_fn(Streaming("")), Err(Err::Incomplete(Needed::new(1))));
212/// ```
213#[inline(always)]
214pub fn one_of<I, T, Error: ParseError<I>, const STREAMING: bool>(
215 list: T,
216) -> impl Fn(I) -> IResult<I, <I as InputIter>::Item, Error>
217where
218 I: Slice<RangeFrom<usize>> + InputIter + InputLength + InputIsStreaming<STREAMING>,
219 <I as InputIter>::Item: Copy,
220 T: FindToken<<I as InputIter>::Item>,
221{
222 move |i: I| {
223 if STREAMING {
224 streaming::one_of_internal(i, &list)
225 } else {
226 complete::one_of_internal(i, &list)
227 }
228 }
229}
230
231/// Returns a token that does not match the [pattern][FindToken]
232///
233/// *Complete version*: Will return an error if there's not enough input data.
234///
235/// *Streaming version*: Will return `Err(nom8::Err::Incomplete(_))` if there's not enough input data.
236///
237/// # Example
238///
239/// ```
240/// # use nom8::{Err, error::ErrorKind};
241/// # use nom8::bytes::none_of;
242/// assert_eq!(none_of::<_, _, (&str, ErrorKind), false>("abc")("z"), Ok(("", 'z')));
243/// assert_eq!(none_of::<_, _, (&str, ErrorKind), false>("ab")("a"), Err(Err::Error(("a", ErrorKind::NoneOf))));
244/// assert_eq!(none_of::<_, _, (&str, ErrorKind), false>("a")(""), Err(Err::Error(("", ErrorKind::NoneOf))));
245/// ```
246///
247/// ```
248/// # use nom8::{Err, error::ErrorKind, Needed};
249/// # use nom8::input::Streaming;
250/// # use nom8::bytes::none_of;
251/// assert_eq!(none_of::<_, _, (_, ErrorKind), true>("abc")(Streaming("z")), Ok((Streaming(""), 'z')));
252/// assert_eq!(none_of::<_, _, (_, ErrorKind), true>("ab")(Streaming("a")), Err(Err::Error((Streaming("a"), ErrorKind::NoneOf))));
253/// assert_eq!(none_of::<_, _, (_, ErrorKind), true>("a")(Streaming("")), Err(Err::Incomplete(Needed::new(1))));
254/// ```
255#[inline(always)]
256pub fn none_of<I, T, Error: ParseError<I>, const STREAMING: bool>(
257 list: T,
258) -> impl Fn(I) -> IResult<I, <I as InputIter>::Item, Error>
259where
260 I: Slice<RangeFrom<usize>> + InputIter + InputLength + InputIsStreaming<STREAMING>,
261 <I as InputIter>::Item: Copy,
262 T: FindToken<<I as InputIter>::Item>,
263{
264 move |i: I| {
265 if STREAMING {
266 streaming::none_of_internal(i, &list)
267 } else {
268 complete::none_of_internal(i, &list)
269 }
270 }
271}
272
273/// Returns the longest input slice (if any) that matches the [pattern][FindToken]
274///
275/// *Streaming version*: will return a `Err::Incomplete(Needed::new(1))` if the pattern reaches the end of the input.
276/// # Example
277/// ```rust
278/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
279/// use nom8::bytes::take_while;
280/// use nom8::input::AsChar;
281///
282/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
283/// take_while(AsChar::is_alpha)(s)
284/// }
285///
286/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
287/// assert_eq!(alpha(b"12345"), Ok((&b"12345"[..], &b""[..])));
288/// assert_eq!(alpha(b"latin"), Ok((&b""[..], &b"latin"[..])));
289/// assert_eq!(alpha(b""), Ok((&b""[..], &b""[..])));
290/// ```
291///
292/// ```rust
293/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
294/// # use nom8::input::Streaming;
295/// use nom8::bytes::take_while;
296/// use nom8::input::AsChar;
297///
298/// fn alpha(s: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, &[u8]> {
299/// take_while(AsChar::is_alpha)(s)
300/// }
301///
302/// assert_eq!(alpha(Streaming(b"latin123")), Ok((Streaming(&b"123"[..]), &b"latin"[..])));
303/// assert_eq!(alpha(Streaming(b"12345")), Ok((Streaming(&b"12345"[..]), &b""[..])));
304/// assert_eq!(alpha(Streaming(b"latin")), Err(Err::Incomplete(Needed::new(1))));
305/// assert_eq!(alpha(Streaming(b"")), Err(Err::Incomplete(Needed::new(1))));
306/// ```
307#[inline(always)]
308pub fn take_while<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
309 list: T,
310) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
311where
312 Input: InputTakeAtPosition + InputIsStreaming<STREAMING>,
313 Input: IntoOutput,
314 T: FindToken<<Input as InputTakeAtPosition>::Item>,
315 Input: InputTakeAtPosition,
316{
317 move |i: Input| {
318 if STREAMING {
319 streaming::take_while_internal(i, &list)
320 } else {
321 complete::take_while_internal(i, &list)
322 }
323 }
324}
325
326/// Returns the longest (at least 1) input slice that matches the [pattern][FindToken]
327///
328/// It will return an `Err(Err::Error((_, ErrorKind::TakeWhile1)))` if the pattern wasn't met.
329///
330/// *Streaming version* will return a `Err::Incomplete(Needed::new(1))` or if the pattern reaches the end of the input.
331///
332/// # Example
333/// ```rust
334/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
335/// use nom8::bytes::take_while1;
336/// use nom8::input::AsChar;
337///
338/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
339/// take_while1(AsChar::is_alpha)(s)
340/// }
341///
342/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
343/// assert_eq!(alpha(b"latin"), Ok((&b""[..], &b"latin"[..])));
344/// assert_eq!(alpha(b"12345"), Err(Err::Error(Error::new(&b"12345"[..], ErrorKind::TakeWhile1))));
345///
346/// fn hex(s: &str) -> IResult<&str, &str> {
347/// take_while1("1234567890ABCDEF")(s)
348/// }
349///
350/// assert_eq!(hex("123 and voila"), Ok((" and voila", "123")));
351/// assert_eq!(hex("DEADBEEF and others"), Ok((" and others", "DEADBEEF")));
352/// assert_eq!(hex("BADBABEsomething"), Ok(("something", "BADBABE")));
353/// assert_eq!(hex("D15EA5E"), Ok(("", "D15EA5E")));
354/// assert_eq!(hex(""), Err(Err::Error(Error::new("", ErrorKind::TakeWhile1))));
355/// ```
356///
357/// ```rust
358/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
359/// # use nom8::input::Streaming;
360/// use nom8::bytes::take_while1;
361/// use nom8::input::AsChar;
362///
363/// fn alpha(s: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, &[u8]> {
364/// take_while1(AsChar::is_alpha)(s)
365/// }
366///
367/// assert_eq!(alpha(Streaming(b"latin123")), Ok((Streaming(&b"123"[..]), &b"latin"[..])));
368/// assert_eq!(alpha(Streaming(b"latin")), Err(Err::Incomplete(Needed::new(1))));
369/// assert_eq!(alpha(Streaming(b"12345")), Err(Err::Error(Error::new(Streaming(&b"12345"[..]), ErrorKind::TakeWhile1))));
370///
371/// fn hex(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
372/// take_while1("1234567890ABCDEF")(s)
373/// }
374///
375/// assert_eq!(hex(Streaming("123 and voila")), Ok((Streaming(" and voila"), "123")));
376/// assert_eq!(hex(Streaming("DEADBEEF and others")), Ok((Streaming(" and others"), "DEADBEEF")));
377/// assert_eq!(hex(Streaming("BADBABEsomething")), Ok((Streaming("something"), "BADBABE")));
378/// assert_eq!(hex(Streaming("D15EA5E")), Err(Err::Incomplete(Needed::new(1))));
379/// assert_eq!(hex(Streaming("")), Err(Err::Incomplete(Needed::new(1))));
380/// ```
381#[inline(always)]
382pub fn take_while1<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
383 list: T,
384) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
385where
386 Input: InputTakeAtPosition + InputIsStreaming<STREAMING>,
387 Input: IntoOutput,
388 T: FindToken<<Input as InputTakeAtPosition>::Item>,
389{
390 move |i: Input| {
391 if STREAMING {
392 streaming::take_while1_internal(i, &list)
393 } else {
394 complete::take_while1_internal(i, &list)
395 }
396 }
397}
398
399/// Returns the longest (m <= len <= n) input slice that matches the [pattern][FindToken]
400///
401/// It will return an `Err::Error((_, ErrorKind::TakeWhileMN))` if the pattern wasn't met or is out
402/// of range (m <= len <= n).
403///
404/// *Streaming version* will return a `Err::Incomplete(Needed::new(1))` if the pattern reaches the end of the input or is too short.
405///
406/// # Example
407/// ```rust
408/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
409/// use nom8::bytes::take_while_m_n;
410/// use nom8::input::AsChar;
411///
412/// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
413/// take_while_m_n(3, 6, AsChar::is_alpha)(s)
414/// }
415///
416/// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
417/// assert_eq!(short_alpha(b"lengthy"), Ok((&b"y"[..], &b"length"[..])));
418/// assert_eq!(short_alpha(b"latin"), Ok((&b""[..], &b"latin"[..])));
419/// assert_eq!(short_alpha(b"ed"), Err(Err::Error(Error::new(&b"ed"[..], ErrorKind::TakeWhileMN))));
420/// assert_eq!(short_alpha(b"12345"), Err(Err::Error(Error::new(&b"12345"[..], ErrorKind::TakeWhileMN))));
421/// ```
422///
423/// ```rust
424/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
425/// # use nom8::input::Streaming;
426/// use nom8::bytes::take_while_m_n;
427/// use nom8::input::AsChar;
428///
429/// fn short_alpha(s: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, &[u8]> {
430/// take_while_m_n(3, 6, AsChar::is_alpha)(s)
431/// }
432///
433/// assert_eq!(short_alpha(Streaming(b"latin123")), Ok((Streaming(&b"123"[..]), &b"latin"[..])));
434/// assert_eq!(short_alpha(Streaming(b"lengthy")), Ok((Streaming(&b"y"[..]), &b"length"[..])));
435/// assert_eq!(short_alpha(Streaming(b"latin")), Err(Err::Incomplete(Needed::new(1))));
436/// assert_eq!(short_alpha(Streaming(b"ed")), Err(Err::Incomplete(Needed::new(1))));
437/// assert_eq!(short_alpha(Streaming(b"12345")), Err(Err::Error(Error::new(Streaming(&b"12345"[..]), ErrorKind::TakeWhileMN))));
438/// ```
439#[inline(always)]
440pub fn take_while_m_n<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
441 m: usize,
442 n: usize,
443 list: T,
444) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
445where
446 Input:
447 InputTake + InputIter + InputLength + Slice<RangeFrom<usize>> + InputIsStreaming<STREAMING>,
448 Input: IntoOutput,
449 T: FindToken<<Input as InputIter>::Item>,
450{
451 move |i: Input| {
452 if STREAMING {
453 streaming::take_while_m_n_internal(i, m, n, &list)
454 } else {
455 complete::take_while_m_n_internal(i, m, n, &list)
456 }
457 }
458}
459
460/// Returns the longest input slice (if any) till a [pattern][FindToken] is met.
461///
462/// *Streaming version* will return a `Err::Incomplete(Needed::new(1))` if the match reaches the
463/// end of input or if there was not match.
464///
465/// # Example
466/// ```rust
467/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
468/// use nom8::bytes::take_till;
469///
470/// fn till_colon(s: &str) -> IResult<&str, &str> {
471/// take_till(|c| c == ':')(s)
472/// }
473///
474/// assert_eq!(till_colon("latin:123"), Ok((":123", "latin")));
475/// assert_eq!(till_colon(":empty matched"), Ok((":empty matched", ""))); //allowed
476/// assert_eq!(till_colon("12345"), Ok(("", "12345")));
477/// assert_eq!(till_colon(""), Ok(("", "")));
478/// ```
479///
480/// ```rust
481/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
482/// # use nom8::input::Streaming;
483/// use nom8::bytes::take_till;
484///
485/// fn till_colon(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
486/// take_till(|c| c == ':')(s)
487/// }
488///
489/// assert_eq!(till_colon(Streaming("latin:123")), Ok((Streaming(":123"), "latin")));
490/// assert_eq!(till_colon(Streaming(":empty matched")), Ok((Streaming(":empty matched"), ""))); //allowed
491/// assert_eq!(till_colon(Streaming("12345")), Err(Err::Incomplete(Needed::new(1))));
492/// assert_eq!(till_colon(Streaming("")), Err(Err::Incomplete(Needed::new(1))));
493/// ```
494#[inline(always)]
495pub fn take_till<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
496 list: T,
497) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
498where
499 Input: InputTakeAtPosition + InputIsStreaming<STREAMING>,
500 Input: IntoOutput,
501 T: FindToken<<Input as InputTakeAtPosition>::Item>,
502{
503 move |i: Input| {
504 if STREAMING {
505 streaming::take_till_internal(i, &list)
506 } else {
507 complete::take_till_internal(i, &list)
508 }
509 }
510}
511
512/// Returns the longest (at least 1) input slice till a [pattern][FindToken] is met.
513///
514/// It will return `Err(Err::Error((_, ErrorKind::TakeTill1)))` if the input is empty or the
515/// predicate matches the first input.
516///
517/// *Streaming version* will return a `Err::Incomplete(Needed::new(1))` if the match reaches the
518/// end of input or if there was not match.
519///
520/// # Example
521/// ```rust
522/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
523/// use nom8::bytes::take_till1;
524///
525/// fn till_colon(s: &str) -> IResult<&str, &str> {
526/// take_till1(|c| c == ':')(s)
527/// }
528///
529/// assert_eq!(till_colon("latin:123"), Ok((":123", "latin")));
530/// assert_eq!(till_colon(":empty matched"), Err(Err::Error(Error::new(":empty matched", ErrorKind::TakeTill1))));
531/// assert_eq!(till_colon("12345"), Ok(("", "12345")));
532/// assert_eq!(till_colon(""), Err(Err::Error(Error::new("", ErrorKind::TakeTill1))));
533///
534/// fn not_space(s: &str) -> IResult<&str, &str> {
535/// take_till1(" \t\r\n")(s)
536/// }
537///
538/// assert_eq!(not_space("Hello, World!"), Ok((" World!", "Hello,")));
539/// assert_eq!(not_space("Sometimes\t"), Ok(("\t", "Sometimes")));
540/// assert_eq!(not_space("Nospace"), Ok(("", "Nospace")));
541/// assert_eq!(not_space(""), Err(Err::Error(Error::new("", ErrorKind::TakeTill1))));
542/// ```
543///
544/// ```rust
545/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
546/// # use nom8::input::Streaming;
547/// use nom8::bytes::take_till1;
548///
549/// fn till_colon(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
550/// take_till1(|c| c == ':')(s)
551/// }
552///
553/// assert_eq!(till_colon(Streaming("latin:123")), Ok((Streaming(":123"), "latin")));
554/// assert_eq!(till_colon(Streaming(":empty matched")), Err(Err::Error(Error::new(Streaming(":empty matched"), ErrorKind::TakeTill1))));
555/// assert_eq!(till_colon(Streaming("12345")), Err(Err::Incomplete(Needed::new(1))));
556/// assert_eq!(till_colon(Streaming("")), Err(Err::Incomplete(Needed::new(1))));
557///
558/// fn not_space(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
559/// take_till1(" \t\r\n")(s)
560/// }
561///
562/// assert_eq!(not_space(Streaming("Hello, World!")), Ok((Streaming(" World!"), "Hello,")));
563/// assert_eq!(not_space(Streaming("Sometimes\t")), Ok((Streaming("\t"), "Sometimes")));
564/// assert_eq!(not_space(Streaming("Nospace")), Err(Err::Incomplete(Needed::new(1))));
565/// assert_eq!(not_space(Streaming("")), Err(Err::Incomplete(Needed::new(1))));
566/// ```
567#[inline(always)]
568pub fn take_till1<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
569 list: T,
570) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
571where
572 Input: InputTakeAtPosition + InputIsStreaming<STREAMING>,
573 Input: IntoOutput,
574 T: FindToken<<Input as InputTakeAtPosition>::Item>,
575{
576 move |i: Input| {
577 if STREAMING {
578 streaming::take_till1_internal(i, &list)
579 } else {
580 complete::take_till1_internal(i, &list)
581 }
582 }
583}
584
585/// Returns an input slice containing the first N input elements (Input[..N]).
586///
587/// *Complete version*: It will return `Err(Err::Error((_, ErrorKind::Eof)))` if the input is shorter than the argument.
588///
589/// *Streaming version*: if the input has less than N elements, `take` will
590/// return a `Err::Incomplete(Needed::new(M))` where M is the number of
591/// additional bytes the parser would need to succeed.
592/// It is well defined for `&[u8]` as the number of elements is the byte size,
593/// but for types like `&str`, we cannot know how many bytes correspond for
594/// the next few chars, so the result will be `Err::Incomplete(Needed::Unknown)`
595///
596/// # Example
597/// ```rust
598/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
599/// use nom8::bytes::take;
600///
601/// fn take6(s: &str) -> IResult<&str, &str> {
602/// take(6usize)(s)
603/// }
604///
605/// assert_eq!(take6("1234567"), Ok(("7", "123456")));
606/// assert_eq!(take6("things"), Ok(("", "things")));
607/// assert_eq!(take6("short"), Err(Err::Error(Error::new("short", ErrorKind::Eof))));
608/// assert_eq!(take6(""), Err(Err::Error(Error::new("", ErrorKind::Eof))));
609/// ```
610///
611/// The units that are taken will depend on the input type. For example, for a
612/// `&str` it will take a number of `char`'s, whereas for a `&[u8]` it will
613/// take that many `u8`'s:
614///
615/// ```rust
616/// use nom8::error::Error;
617/// use nom8::bytes::take;
618///
619/// assert_eq!(take::<_, _, Error<_>, false>(1usize)("💙"), Ok(("", "💙")));
620/// assert_eq!(take::<_, _, Error<_>, false>(1usize)("💙".as_bytes()), Ok((b"\x9F\x92\x99".as_ref(), b"\xF0".as_ref())));
621/// ```
622///
623/// ```rust
624/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
625/// # use nom8::input::Streaming;
626/// use nom8::bytes::take;
627///
628/// fn take6(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
629/// take(6usize)(s)
630/// }
631///
632/// assert_eq!(take6(Streaming("1234567")), Ok((Streaming("7"), "123456")));
633/// assert_eq!(take6(Streaming("things")), Ok((Streaming(""), "things")));
634/// // `Unknown` as we don't know the number of bytes that `count` corresponds to
635/// assert_eq!(take6(Streaming("short")), Err(Err::Incomplete(Needed::Unknown)));
636/// ```
637#[inline(always)]
638pub fn take<C, Input, Error: ParseError<Input>, const STREAMING: bool>(
639 count: C,
640) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
641where
642 Input: InputIter + InputLength + InputTake + InputIsStreaming<STREAMING>,
643 Input: IntoOutput,
644 C: ToUsize,
645{
646 let c = count.to_usize();
647 move |i: Input| {
648 if STREAMING {
649 streaming::take_internal(i, c)
650 } else {
651 complete::take_internal(i, c)
652 }
653 }
654}
655
656/// Returns the input slice up to the first occurrence of the pattern.
657///
658/// It doesn't consume the pattern.
659///
660/// *Complete version*: It will return `Err(Err::Error((_, ErrorKind::TakeUntil)))`
661/// if the pattern wasn't met.
662///
663/// *Streaming version*: will return a `Err::Incomplete(Needed::new(N))` if the input doesn't
664/// contain the pattern or if the input is smaller than the pattern.
665/// # Example
666/// ```rust
667/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
668/// use nom8::bytes::take_until;
669///
670/// fn until_eof(s: &str) -> IResult<&str, &str> {
671/// take_until("eof")(s)
672/// }
673///
674/// assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world")));
675/// assert_eq!(until_eof("hello, world"), Err(Err::Error(Error::new("hello, world", ErrorKind::TakeUntil))));
676/// assert_eq!(until_eof(""), Err(Err::Error(Error::new("", ErrorKind::TakeUntil))));
677/// assert_eq!(until_eof("1eof2eof"), Ok(("eof2eof", "1")));
678/// ```
679///
680/// ```rust
681/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
682/// # use nom8::input::Streaming;
683/// use nom8::bytes::take_until;
684///
685/// fn until_eof(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
686/// take_until("eof")(s)
687/// }
688///
689/// assert_eq!(until_eof(Streaming("hello, worldeof")), Ok((Streaming("eof"), "hello, world")));
690/// assert_eq!(until_eof(Streaming("hello, world")), Err(Err::Incomplete(Needed::Unknown)));
691/// assert_eq!(until_eof(Streaming("hello, worldeo")), Err(Err::Incomplete(Needed::Unknown)));
692/// assert_eq!(until_eof(Streaming("1eof2eof")), Ok((Streaming("eof2eof"), "1")));
693/// ```
694#[inline(always)]
695pub fn take_until<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
696 tag: T,
697) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
698where
699 Input: InputTake + InputLength + FindSubstring<T> + InputIsStreaming<STREAMING>,
700 Input: IntoOutput,
701 T: InputLength + Clone,
702{
703 move |i: Input| {
704 if STREAMING {
705 streaming::take_until_internal(i, tag.clone())
706 } else {
707 complete::take_until_internal(i, tag.clone())
708 }
709 }
710}
711
712/// Returns the non empty input slice up to the first occurrence of the pattern.
713///
714/// It doesn't consume the pattern.
715///
716/// *Complete version*: It will return `Err(Err::Error((_, ErrorKind::TakeUntil)))`
717/// if the pattern wasn't met.
718///
719/// *Streaming version*: will return a `Err::Incomplete(Needed::new(N))` if the input doesn't
720/// contain the pattern or if the input is smaller than the pattern.
721///
722/// # Example
723/// ```rust
724/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
725/// use nom8::bytes::take_until1;
726///
727/// fn until_eof(s: &str) -> IResult<&str, &str> {
728/// take_until1("eof")(s)
729/// }
730///
731/// assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world")));
732/// assert_eq!(until_eof("hello, world"), Err(Err::Error(Error::new("hello, world", ErrorKind::TakeUntil))));
733/// assert_eq!(until_eof(""), Err(Err::Error(Error::new("", ErrorKind::TakeUntil))));
734/// assert_eq!(until_eof("1eof2eof"), Ok(("eof2eof", "1")));
735/// assert_eq!(until_eof("eof"), Err(Err::Error(Error::new("eof", ErrorKind::TakeUntil))));
736/// ```
737///
738/// ```rust
739/// # use nom8::{Err, error::{Error, ErrorKind}, Needed, IResult};
740/// # use nom8::input::Streaming;
741/// use nom8::bytes::take_until1;
742///
743/// fn until_eof(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
744/// take_until1("eof")(s)
745/// }
746///
747/// assert_eq!(until_eof(Streaming("hello, worldeof")), Ok((Streaming("eof"), "hello, world")));
748/// assert_eq!(until_eof(Streaming("hello, world")), Err(Err::Incomplete(Needed::Unknown)));
749/// assert_eq!(until_eof(Streaming("hello, worldeo")), Err(Err::Incomplete(Needed::Unknown)));
750/// assert_eq!(until_eof(Streaming("1eof2eof")), Ok((Streaming("eof2eof"), "1")));
751/// assert_eq!(until_eof(Streaming("eof")), Err(Err::Error(Error::new(Streaming("eof"), ErrorKind::TakeUntil))));
752/// ```
753#[inline(always)]
754pub fn take_until1<T, Input, Error: ParseError<Input>, const STREAMING: bool>(
755 tag: T,
756) -> impl Fn(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
757where
758 Input: InputTake + InputLength + FindSubstring<T> + InputIsStreaming<STREAMING>,
759 Input: IntoOutput,
760 T: InputLength + Clone,
761{
762 move |i: Input| {
763 if STREAMING {
764 streaming::take_until1_internal(i, tag.clone())
765 } else {
766 complete::take_until1_internal(i, tag.clone())
767 }
768 }
769}
770
771/// Matches a byte string with escaped characters.
772///
773/// * The first argument matches the normal characters (it must not accept the control character)
774/// * The second argument is the control character (like `\` in most languages)
775/// * The third argument matches the escaped characters
776/// # Example
777/// ```
778/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
779/// # use nom8::character::digit1;
780/// use nom8::bytes::escaped;
781/// use nom8::bytes::one_of;
782///
783/// fn esc(s: &str) -> IResult<&str, &str> {
784/// escaped(digit1, '\\', one_of(r#""n\"#))(s)
785/// }
786///
787/// assert_eq!(esc("123;"), Ok((";", "123")));
788/// assert_eq!(esc(r#"12\"34;"#), Ok((";", r#"12\"34"#)));
789/// ```
790///
791/// ```
792/// # use nom8::{Err, error::ErrorKind, Needed, IResult};
793/// # use nom8::character::digit1;
794/// # use nom8::input::Streaming;
795/// use nom8::bytes::escaped;
796/// use nom8::bytes::one_of;
797///
798/// fn esc(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
799/// escaped(digit1, '\\', one_of("\"n\\"))(s)
800/// }
801///
802/// assert_eq!(esc(Streaming("123;")), Ok((Streaming(";"), "123")));
803/// assert_eq!(esc(Streaming("12\\\"34;")), Ok((Streaming(";"), "12\\\"34")));
804/// ```
805#[inline(always)]
806pub fn escaped<'a, Input: 'a, Error, F, G, O1, O2, const STREAMING: bool>(
807 mut normal: F,
808 control_char: char,
809 mut escapable: G,
810) -> impl FnMut(Input) -> IResult<Input, <Input as IntoOutput>::Output, Error>
811where
812 Input: Clone
813 + crate::input::Offset
814 + InputLength
815 + InputTake
816 + InputTakeAtPosition
817 + Slice<RangeFrom<usize>>
818 + InputIter
819 + InputIsStreaming<STREAMING>,
820 Input: IntoOutput,
821 <Input as InputIter>::Item: crate::input::AsChar,
822 F: Parser<Input, O1, Error>,
823 G: Parser<Input, O2, Error>,
824 Error: ParseError<Input>,
825{
826 move |input: Input| {
827 if STREAMING {
828 streaming::escaped_internal(input, &mut normal, control_char, &mut escapable)
829 } else {
830 complete::escaped_internal(input, &mut normal, control_char, &mut escapable)
831 }
832 }
833}
834
835/// Matches a byte string with escaped characters.
836///
837/// * The first argument matches the normal characters (it must not match the control character)
838/// * The second argument is the control character (like `\` in most languages)
839/// * The third argument matches the escaped characters and transforms them
840///
841/// As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character)
842///
843/// ```
844/// # use nom8::prelude::*;
845/// # use nom8::{Err, error::ErrorKind, Needed};
846/// # use std::str::from_utf8;
847/// use nom8::bytes::{escaped_transform, tag};
848/// use nom8::character::alpha1;
849/// use nom8::branch::alt;
850/// use nom8::combinator::value;
851///
852/// fn parser(input: &str) -> IResult<&str, String> {
853/// escaped_transform(
854/// alpha1,
855/// '\\',
856/// alt((
857/// tag("\\").value("\\"),
858/// tag("\"").value("\""),
859/// tag("n").value("\n"),
860/// ))
861/// )(input)
862/// }
863///
864/// assert_eq!(parser("ab\\\"cd"), Ok(("", String::from("ab\"cd"))));
865/// assert_eq!(parser("ab\\ncd"), Ok(("", String::from("ab\ncd"))));
866/// ```
867///
868/// ```
869/// # use nom8::prelude::*;
870/// # use nom8::{Err, error::ErrorKind, Needed};
871/// # use std::str::from_utf8;
872/// # use nom8::input::Streaming;
873/// use nom8::bytes::{escaped_transform, tag};
874/// use nom8::character::alpha1;
875/// use nom8::branch::alt;
876/// use nom8::combinator::value;
877///
878/// fn parser(input: Streaming<&str>) -> IResult<Streaming<&str>, String> {
879/// escaped_transform(
880/// alpha1,
881/// '\\',
882/// alt((
883/// tag("\\").value("\\"),
884/// tag("\"").value("\""),
885/// tag("n").value("\n"),
886/// ))
887/// )(input)
888/// }
889///
890/// assert_eq!(parser(Streaming("ab\\\"cd\"")), Ok((Streaming("\""), String::from("ab\"cd"))));
891/// ```
892#[cfg(feature = "alloc")]
893#[inline(always)]
894pub fn escaped_transform<Input, Error, F, G, O1, O2, ExtendItem, Output, const STREAMING: bool>(
895 mut normal: F,
896 control_char: char,
897 mut transform: G,
898) -> impl FnMut(Input) -> IResult<Input, Output, Error>
899where
900 Input: Clone
901 + crate::input::Offset
902 + InputLength
903 + InputTake
904 + InputTakeAtPosition
905 + Slice<RangeFrom<usize>>
906 + InputIter
907 + InputIsStreaming<STREAMING>,
908 Input: IntoOutput,
909 Input: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
910 O1: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
911 O2: crate::input::ExtendInto<Item = ExtendItem, Extender = Output>,
912 <Input as InputIter>::Item: crate::input::AsChar,
913 F: Parser<Input, O1, Error>,
914 G: Parser<Input, O2, Error>,
915 Error: ParseError<Input>,
916{
917 move |input: Input| {
918 if STREAMING {
919 streaming::escaped_transform_internal(input, &mut normal, control_char, &mut transform)
920 } else {
921 complete::escaped_transform_internal(input, &mut normal, control_char, &mut transform)
922 }
923 }
924}