From 74569b57cd735cdeaf225593f36eca14e5936413 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 24 Jan 2025 12:28:18 -0600 Subject: [PATCH] fix(ascii): Allow separate normal/transform types for escaped_transform Fixes #686 --- src/ascii/mod.rs | 34 +++++++++++++++++++++++----------- src/ascii/tests.rs | 34 ++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/ascii/mod.rs b/src/ascii/mod.rs index e2321fec..46caff68 100644 --- a/src/ascii/mod.rs +++ b/src/ascii/mod.rs @@ -1765,28 +1765,29 @@ where /// # } /// ``` #[inline(always)] -pub fn escaped_transform( +pub fn escaped_transform( mut normal: Normal, control_char: char, mut escape: Escape, ) -> impl Parser where Input: StreamIsPartial + Stream + Compare, - Output: crate::stream::Accumulate<::Slice>, - Normal: Parser::Slice, Error>, - Escape: Parser::Slice, Error>, + Normal: Parser, + Escape: Parser, + Output: crate::stream::Accumulate, + Output: crate::stream::Accumulate, Error: ParserError, { trace("escaped_transform", move |input: &mut Input| { if ::is_partial_supported() && input.is_partial() { - escaped_transform_internal::<_, _, _, _, _, true>( + escaped_transform_internal::<_, _, _, _, _, _, _, true>( input, &mut normal, control_char, &mut escape, ) } else { - escaped_transform_internal::<_, _, _, _, _, false>( + escaped_transform_internal::<_, _, _, _, _, _, _, false>( input, &mut normal, control_char, @@ -1796,7 +1797,16 @@ where }) } -fn escaped_transform_internal( +fn escaped_transform_internal< + I, + Error, + F, + NormalOutput, + G, + EscapeOutput, + Output, + const PARTIAL: bool, +>( input: &mut I, normal: &mut F, control_char: char, @@ -1806,12 +1816,14 @@ where I: StreamIsPartial, I: Stream, I: Compare, - Output: crate::stream::Accumulate<::Slice>, - F: Parser::Slice, Error>, - G: Parser::Slice, Error>, + Output: crate::stream::Accumulate, + Output: crate::stream::Accumulate, + F: Parser, + G: Parser, Error: ParserError, { - let mut res = Output::initial(Some(input.eof_offset())); + let mut res = + >::initial(Some(input.eof_offset())); while input.eof_offset() > 0 { let current_len = input.eof_offset(); diff --git a/src/ascii/tests.rs b/src/ascii/tests.rs index 9c917064..35f7b182 100644 --- a/src/ascii/tests.rs +++ b/src/ascii/tests.rs @@ -2499,17 +2499,9 @@ Ok( } fn esc<'i>(i: &mut &'i [u8]) -> TestResult<&'i [u8], String> { - escaped_transform( - alpha, - '\\', - alt(( - "\\".value(&b"\\"[..]), - "\"".value(&b"\""[..]), - "n".value(&b"\n"[..]), - )), - ) - .map(to_s) - .parse_next(i) + escaped_transform(alpha, '\\', alt((b'\\', b'"', "n".value(b'\n')))) + .map(to_s) + .parse_next(i) } assert_parse!( @@ -2674,7 +2666,12 @@ Ok( escaped_transform( alpha, '\\', - alt(("\\".value("\\"), "\"".value("\""), "n".value("\n"))), + alt(( + '\\', + '"', + "n".value('\n'), + ("x", hex_uint).map(|(_, hex)| char::from_u32(hex).unwrap()), + )), ) .parse_next(i) } @@ -2741,6 +2738,19 @@ Ok( ), ) +"#]] + .raw() + ); + assert_parse!( + esc.parse_peek("ab\\x20"), + str![[r#" +Ok( + ( + "", + "ab ", + ), +) + "#]] .raw() );