Skip to content

Commit

Permalink
Merge pull request #715 from epage/escaped
Browse files Browse the repository at this point in the history
fix(ascii): Allow separate normal/transform types for escaped_transform
  • Loading branch information
epage authored Jan 24, 2025
2 parents 9c82a02 + 74569b5 commit 1be0ea8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
34 changes: 23 additions & 11 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1765,28 +1765,29 @@ where
/// # }
/// ```
#[inline(always)]
pub fn escaped_transform<Input, Error, Normal, Escape, Output>(
pub fn escaped_transform<Input, Error, Normal, NormalOutput, Escape, EscapeOutput, Output>(
mut normal: Normal,
control_char: char,
mut escape: Escape,
) -> impl Parser<Input, Output, Error>
where
Input: StreamIsPartial + Stream + Compare<char>,
Output: crate::stream::Accumulate<<Input as Stream>::Slice>,
Normal: Parser<Input, <Input as Stream>::Slice, Error>,
Escape: Parser<Input, <Input as Stream>::Slice, Error>,
Normal: Parser<Input, NormalOutput, Error>,
Escape: Parser<Input, EscapeOutput, Error>,
Output: crate::stream::Accumulate<NormalOutput>,
Output: crate::stream::Accumulate<EscapeOutput>,
Error: ParserError<Input>,
{
trace("escaped_transform", move |input: &mut Input| {
if <Input as StreamIsPartial>::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,
Expand All @@ -1796,7 +1797,16 @@ where
})
}

fn escaped_transform_internal<I, Error, F, G, Output, const PARTIAL: bool>(
fn escaped_transform_internal<
I,
Error,
F,
NormalOutput,
G,
EscapeOutput,
Output,
const PARTIAL: bool,
>(
input: &mut I,
normal: &mut F,
control_char: char,
Expand All @@ -1806,12 +1816,14 @@ where
I: StreamIsPartial,
I: Stream,
I: Compare<char>,
Output: crate::stream::Accumulate<<I as Stream>::Slice>,
F: Parser<I, <I as Stream>::Slice, Error>,
G: Parser<I, <I as Stream>::Slice, Error>,
Output: crate::stream::Accumulate<NormalOutput>,
Output: crate::stream::Accumulate<EscapeOutput>,
F: Parser<I, NormalOutput, Error>,
G: Parser<I, EscapeOutput, Error>,
Error: ParserError<I>,
{
let mut res = Output::initial(Some(input.eof_offset()));
let mut res =
<Output as crate::stream::Accumulate<NormalOutput>>::initial(Some(input.eof_offset()));

while input.eof_offset() > 0 {
let current_len = input.eof_offset();
Expand Down
34 changes: 22 additions & 12 deletions src/ascii/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -2741,6 +2738,19 @@ Ok(
),
)
"#]]
.raw()
);
assert_parse!(
esc.parse_peek("ab\\x20"),
str![[r#"
Ok(
(
"",
"ab ",
),
)
"#]]
.raw()
);
Expand Down

0 comments on commit 1be0ea8

Please sign in to comment.