winnow::ascii

Function take_escaped

Source
pub fn take_escaped<'i, Input, Error, Normal, Escapable, NormalOutput, EscapableOutput>(
    normal: Normal,
    control_char: char,
    escapable: Escapable,
) -> impl Parser<Input, <Input as Stream>::Slice, Error>
where Input: StreamIsPartial + Stream + Compare<char> + 'i, Normal: Parser<Input, NormalOutput, Error>, Escapable: Parser<Input, EscapableOutput, Error>, Error: ParserError<Input>,
Expand description

Recognize the input slice with escaped characters.

Arguments:

  • normal: unescapeable characters
    • Must not include control
  • control_char: e.g. \ for strings in most languages
  • escape: parse and transform the escaped character

Parsing ends when:

  • alt(normal, control._char) Backtracks
  • normal doesn’t advance the input stream
  • (complete) input stream is exhausted

See also escaped_transform

§Example

use winnow::ascii::take_escaped;
use winnow::token::one_of;

fn esc(s: &str) -> IResult<&str, &str> {
  take_escaped(digit1, '\\', one_of(['"', 'n', '\\'])).parse_peek(s)
}

assert_eq!(esc("123;"), Ok((";", "123")));
assert_eq!(esc(r#"12\"34;"#), Ok((";", r#"12\"34"#)));
use winnow::ascii::take_escaped;
use winnow::token::one_of;

fn esc(s: Partial<&str>) -> IResult<Partial<&str>, &str> {
  take_escaped(digit1, '\\', one_of(['"', 'n', '\\'])).parse_peek(s)
}

assert_eq!(esc(Partial::new("123;")), Ok((Partial::new(";"), "123")));
assert_eq!(esc(Partial::new("12\\\"34;")), Ok((Partial::new(";"), "12\\\"34")));