Function winnow::character::escaped

source ·
pub fn escaped<'a, I, Error, F, G, O1, O2>(
    normal: F,
    control_char: char,
    escapable: G
) -> impl FnMut(I) -> IResult<I, <I as Stream>::Slice, Error>where
    I: StreamIsPartial + Stream + Offset + 'a,
    <I as Stream>::Token: AsChar,
    F: Parser<I, O1, Error>,
    G: Parser<I, O2, Error>,
    Error: ParseError<I>,
Expand description

Matches a byte string with escaped characters.

  • The first argument matches the normal characters (it must not accept the control character)
  • The second argument is the control character (like \ in most languages)
  • The third argument matches the escaped characters

Example

use winnow::character::escaped;
use winnow::bytes::one_of;

fn esc(s: &str) -> IResult<&str, &str> {
  escaped(digit1, '\\', one_of(r#""n\"#))(s)
}

assert_eq!(esc("123;"), Ok((";", "123")));
assert_eq!(esc(r#"12\"34;"#), Ok((";", r#"12\"34"#)));
use winnow::character::escaped;
use winnow::bytes::one_of;

fn esc(s: Partial<&str>) -> IResult<Partial<&str>, &str> {
  escaped(digit1, '\\', one_of("\"n\\"))(s)
}

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