Skip to content

Commit

Permalink
fix(ascii): Rename escaped to take_escaped
Browse files Browse the repository at this point in the history
This better matches our "recognize" naming scheme and imo
`escaped_transform` should be just `escaped`.
  • Loading branch information
epage committed Feb 29, 2024
1 parent b44005d commit c08ee44
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 34 deletions.
4 changes: 2 additions & 2 deletions examples/json_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use winnow::prelude::*;
use winnow::{
ascii::{alphanumeric1 as alphanumeric, escaped, float},
ascii::{alphanumeric1 as alphanumeric, float, take_escaped},
combinator::alt,
combinator::cut_err,
combinator::separated,
Expand Down Expand Up @@ -216,7 +216,7 @@ fn sp<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> PResult<&'a str, E> {
}

fn parse_str<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> PResult<&'a str, E> {
escaped(alphanumeric, '\\', one_of(['"', 'n', '\\'])).parse_next(i)
take_escaped(alphanumeric, '\\', one_of(['"', 'n', '\\'])).parse_next(i)
}

fn string<'s>(i: &mut &'s str) -> PResult<&'s str> {
Expand Down
4 changes: 2 additions & 2 deletions src/_topic/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
#![doc = include_str!("../../examples/string/parser.rs")]
//! ```
//!
//! See also [`escaped`] and [`escaped_transform`].
//! See also [`take_escaped`] and [`escaped_transform`].
//!
//! ### Integers
//!
Expand Down Expand Up @@ -320,7 +320,7 @@
#![allow(unused_imports)]
use crate::ascii::dec_int;
use crate::ascii::dec_uint;
use crate::ascii::escaped;
use crate::ascii::escaped_transform;
use crate::ascii::float;
use crate::ascii::hex_uint;
use crate::ascii::take_escaped;
41 changes: 26 additions & 15 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1550,11 +1550,9 @@ where
.parse_next(input)
}

/// Matches a byte string with escaped characters.
/// Recognize the input slice 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
/// See also [`escaped_transform`]
///
/// # Example
///
Expand All @@ -1563,11 +1561,11 @@ where
/// # use winnow::{error::ErrMode, error::ErrorKind, error::InputError, error::Needed, IResult};
/// # use winnow::ascii::digit1;
/// # use winnow::prelude::*;
/// use winnow::ascii::escaped;
/// use winnow::ascii::take_escaped;
/// use winnow::token::one_of;
///
/// fn esc(s: &str) -> IResult<&str, &str> {
/// escaped(digit1, '\\', one_of(['"', 'n', '\\'])).parse_peek(s)
/// take_escaped(digit1, '\\', one_of(['"', 'n', '\\'])).parse_peek(s)
/// }
///
/// assert_eq!(esc("123;"), Ok((";", "123")));
Expand All @@ -1580,18 +1578,18 @@ where
/// # use winnow::ascii::digit1;
/// # use winnow::prelude::*;
/// # use winnow::Partial;
/// use winnow::ascii::escaped;
/// use winnow::ascii::take_escaped;
/// use winnow::token::one_of;
///
/// fn esc(s: Partial<&str>) -> IResult<Partial<&str>, &str> {
/// escaped(digit1, '\\', one_of(['"', 'n', '\\'])).parse_peek(s)
/// 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")));
/// ```
#[inline(always)]
pub fn escaped<'i, Input: 'i, Error, Normal, Escapable, NormalOutput, EscapableOutput>(
pub fn take_escaped<'i, Input: 'i, Error, Normal, Escapable, NormalOutput, EscapableOutput>(
mut normal: Normal,
control_char: char,
mut escapable: Escapable,
Expand All @@ -1602,7 +1600,7 @@ where
Escapable: Parser<Input, EscapableOutput, Error>,
Error: ParserError<Input>,
{
trace("escaped", move |input: &mut Input| {
trace("take_escaped", move |input: &mut Input| {
if <Input as StreamIsPartial>::is_partial_supported() && input.is_partial() {
streaming_escaped_internal(input, &mut normal, control_char, &mut escapable)
} else {
Expand All @@ -1611,6 +1609,23 @@ where
})
}

/// Deprecated, replaced with [`take_escaped`]
#[deprecated(since = "0.6.4", note = "Replaced with `take_escaped`")]
#[inline(always)]
pub fn escaped<'i, Input: 'i, 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>,
Normal: Parser<Input, NormalOutput, Error>,
Escapable: Parser<Input, EscapableOutput, Error>,
Error: ParserError<Input>,
{
take_escaped(normal, control_char, escapable)

Check warning on line 1626 in src/ascii/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/ascii/mod.rs#L1626

Added line #L1626 was not covered by tests
}

fn streaming_escaped_internal<I, Error, F, G, O1, O2>(
input: &mut I,
normal: &mut F,
Expand Down Expand Up @@ -1696,11 +1711,7 @@ where
Ok(input.finish())
}

/// 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
/// Parse escaped characters, unescaping them
///
/// As an example, the chain `abc\tdef` could be `abc def` (it also consumes the control character)
///
Expand Down
26 changes: 13 additions & 13 deletions src/ascii/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,31 +646,31 @@ mod complete {
}
}

// issue #1336 "escaped hangs if normal parser accepts empty"
// issue #1336 "take_escaped hangs if normal parser accepts empty"
#[test]
fn complete_escaped_hang() {
// issue #1336 "escaped hangs if normal parser accepts empty"
fn complete_take_escaped_hang() {
// issue #1336 "take_escaped hangs if normal parser accepts empty"
fn escaped_string(input: &str) -> IResult<&str, &str> {
use crate::ascii::alpha0;
use crate::token::one_of;
escaped(alpha0, '\\', one_of(['n'])).parse_peek(input)
take_escaped(alpha0, '\\', one_of(['n'])).parse_peek(input)
}

escaped_string("7").unwrap();
escaped_string("a7").unwrap();
}

#[test]
fn complete_escaped_hang_1118() {
// issue ##1118 escaped does not work with empty string
fn complete_take_escaped_hang_1118() {
// issue ##1118 take_escaped does not work with empty string
fn unquote(input: &str) -> IResult<&str, &str> {
use crate::combinator::delimited;
use crate::combinator::opt;
use crate::token::one_of;

delimited(
'"',
escaped(
take_escaped(
opt(none_of(['\\', '"'])),
'\\',
one_of(['\\', '"', 'r', 'n', 't']),
Expand All @@ -691,7 +691,7 @@ mod complete {
use crate::token::one_of;

fn esc(i: &[u8]) -> IResult<&[u8], &[u8]> {
escaped(alpha, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i)
take_escaped(alpha, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i)
}
assert_eq!(esc(&b"abcd;"[..]), Ok((&b";"[..], &b"abcd"[..])));
assert_eq!(esc(&b"ab\\\"cd;"[..]), Ok((&b";"[..], &b"ab\\\"cd"[..])));
Expand All @@ -715,7 +715,7 @@ mod complete {
);

fn esc2(i: &[u8]) -> IResult<&[u8], &[u8]> {
escaped(digit, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i)
take_escaped(digit, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i)
}
assert_eq!(esc2(&b"12\\nnn34"[..]), Ok((&b"nn34"[..], &b"12\\n"[..])));
}
Expand All @@ -727,7 +727,7 @@ mod complete {
use crate::token::one_of;

fn esc(i: &str) -> IResult<&str, &str> {
escaped(alpha, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i)
take_escaped(alpha, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i)
}
assert_eq!(esc("abcd;"), Ok((";", "abcd")));
assert_eq!(esc("ab\\\"cd;"), Ok((";", "ab\\\"cd")));
Expand All @@ -748,12 +748,12 @@ mod complete {
);

fn esc2(i: &str) -> IResult<&str, &str> {
escaped(digit, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i)
take_escaped(digit, '\\', one_of(['\"', 'n', '\\'])).parse_peek(i)
}
assert_eq!(esc2("12\\nnn34"), Ok(("nn34", "12\\n")));

fn esc3(i: &str) -> IResult<&str, &str> {
escaped(alpha, '\u{241b}', one_of(['\"', 'n'])).parse_peek(i)
take_escaped(alpha, '\u{241b}', one_of(['\"', 'n'])).parse_peek(i)
}
assert_eq!(esc3("ab␛ncd;"), Ok((";", "ab␛ncd")));
}
Expand All @@ -762,7 +762,7 @@ mod complete {
fn test_escaped_error() {
fn esc(s: &str) -> IResult<&str, &str> {
use crate::ascii::digit1;
escaped(digit1, '\\', one_of(['\"', 'n', '\\'])).parse_peek(s)
take_escaped(digit1, '\\', one_of(['\"', 'n', '\\'])).parse_peek(s)
}

assert_eq!(esc("abcd"), Ok(("abcd", "")));
Expand Down
4 changes: 2 additions & 2 deletions src/combinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
//! - [`dec_uint`][crate::ascii::dec_uint]: Decode a variable-width, decimal unsigned integer
//! - [`hex_uint`][crate::ascii::hex_uint]: Decode a variable-width, hexadecimal integer
//!
//! - [`escaped`][crate::ascii::escaped]: Matches a byte string with escaped characters
//! - [`escaped_transform`][crate::ascii::escaped_transform]: Matches a byte string with escaped characters, and returns a new string with the escaped characters replaced
//! - [`take_escaped`][crate::ascii::take_escaped]: Recognize the input slice with escaped characters
//! - [`escaped_transform`][crate::ascii::escaped_transform]: Parse escaped characters, unescaping them
//!
//! ### Character test functions
//!
Expand Down

0 comments on commit c08ee44

Please sign in to comment.