# Function nom::bytes::complete::escaped_transform

source · [−]

    pub fn escaped_transform<Input, Error, F, G, O1, O2, ExtendItem, Output>(
        normal: F,
        control_char: char,
        transform: G
    ) -> impl FnMut(Input) -> IResult<Input, Output, Error> where
        Input: Clone + Offset + InputLength + InputTake + InputTakeAtPosition + Slice<RangeFrom<usize>> + InputIter,
        Input: ExtendInto<Item = ExtendItem, Extender = Output>,
        O1: ExtendInto<Item = ExtendItem, Extender = Output>,
        O2: ExtendInto<Item = ExtendItem, Extender = Output>,
        <Input as InputIter>::Item: AsChar,
        F: Parser<Input, O1, Error>,
        G: Parser<Input, O2, Error>,
        Error: ParseError<Input>,

Expand description

Matches a byte string with escaped characters.

- The first argument matches the normal characters (it must not match the control character)
- The second argument is the control character (like `\` in most languages)
- The third argument matches the escaped characters and transforms them

As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character)

    use nom::bytes::complete::{escaped_transform, tag};
    use nom::character::complete::alpha1;
    use nom::branch::alt;
    use nom::combinator::value;

    fn parser(input: &str) -> IResult<&str, String> {
      escaped_transform(
        alpha1,
        '\\',
        alt((
          value("\\", tag("\\")),
          value("\"", tag("\"")),
          value("\n", tag("n")),
        ))
      )(input)
    }

    assert_eq!(parser("ab\\\"cd"), Ok(("", String::from("ab\"cd"))));
    assert_eq!(parser("ab\\ncd"), Ok(("", String::from("ab\ncd"))));