From 4f38725e6323424884810530bda6a8bdb1d26a81 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 4 Dec 2023 13:39:42 -0600 Subject: [PATCH] fix: Deprecate take_till0, take_till1 --- benches/contains_token.rs | 32 ++++++++++++++++++++++++++------ examples/ini/parser_str.rs | 4 ++-- examples/string/parser.rs | 8 ++++---- src/ascii/mod.rs | 4 ++-- src/token/mod.rs | 4 +++- src/token/tests.rs | 10 +++++----- tests/testsuite/issues.rs | 8 ++++---- tests/testsuite/reborrow_fold.rs | 4 ++-- tests/testsuite/utf8.rs | 8 ++++---- 9 files changed, 52 insertions(+), 30 deletions(-) diff --git a/benches/contains_token.rs b/benches/contains_token.rs index 2980ce6c..675b08e5 100644 --- a/benches/contains_token.rs +++ b/benches/contains_token.rs @@ -3,7 +3,7 @@ use criterion::black_box; use winnow::combinator::alt; use winnow::combinator::repeat; use winnow::prelude::*; -use winnow::token::take_till1; +use winnow::token::take_till; use winnow::token::take_while; fn contains_token(c: &mut criterion::Criterion) { @@ -52,17 +52,29 @@ fn contains_token(c: &mut criterion::Criterion) { fn parser_slice(input: &mut &str) -> PResult { let contains = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'][..]; - repeat(0.., alt((take_while(1.., contains), take_till1(contains)))).parse_next(input) + repeat( + 0.., + alt((take_while(1.., contains), take_till(1.., contains))), + ) + .parse_next(input) } fn parser_array(input: &mut &str) -> PResult { let contains = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; - repeat(0.., alt((take_while(1.., contains), take_till1(contains)))).parse_next(input) + repeat( + 0.., + alt((take_while(1.., contains), take_till(1.., contains))), + ) + .parse_next(input) } fn parser_tuple(input: &mut &str) -> PResult { let contains = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); - repeat(0.., alt((take_while(1.., contains), take_till1(contains)))).parse_next(input) + repeat( + 0.., + alt((take_while(1.., contains), take_till(1.., contains))), + ) + .parse_next(input) } fn parser_closure_or(input: &mut &str) -> PResult { @@ -78,12 +90,20 @@ fn parser_closure_or(input: &mut &str) -> PResult { || c == '8' || c == '9' }; - repeat(0.., alt((take_while(1.., contains), take_till1(contains)))).parse_next(input) + repeat( + 0.., + alt((take_while(1.., contains), take_till(1.., contains))), + ) + .parse_next(input) } fn parser_closure_matches(input: &mut &str) -> PResult { let contains = |c: char| matches!(c, '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'); - repeat(0.., alt((take_while(1.., contains), take_till1(contains)))).parse_next(input) + repeat( + 0.., + alt((take_while(1.., contains), take_till(1.., contains))), + ) + .parse_next(input) } const CONTIGUOUS: &str = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"; diff --git a/examples/ini/parser_str.rs b/examples/ini/parser_str.rs index 8f7b9cef..c1858dbf 100644 --- a/examples/ini/parser_str.rs +++ b/examples/ini/parser_str.rs @@ -6,7 +6,7 @@ use winnow::{ combinator::opt, combinator::repeat, combinator::{delimited, terminated}, - token::{take_till0, take_while}, + token::{take_till, take_while}, }; pub type Stream<'i> = &'i str; @@ -36,7 +36,7 @@ fn keys_and_values<'s>(input: &mut Stream<'s>) -> PResult(i: &mut Stream<'s>) -> PResult<(&'s str, &'s str)> { let key = alphanumeric.parse_next(i)?; let _ = (opt(space), "=", opt(space)).parse_next(i)?; - let val = take_till0(is_line_ending_or_comment).parse_next(i)?; + let val = take_till(0.., is_line_ending_or_comment).parse_next(i)?; let _ = opt(space).parse_next(i)?; let _ = opt((";", not_line_ending)).parse_next(i)?; let _ = opt(space_or_line_ending).parse_next(i)?; diff --git a/examples/string/parser.rs b/examples/string/parser.rs index 6b634580..01de737b 100644 --- a/examples/string/parser.rs +++ b/examples/string/parser.rs @@ -15,7 +15,7 @@ use winnow::combinator::fold_repeat; use winnow::combinator::{delimited, preceded}; use winnow::error::{FromExternalError, ParserError}; use winnow::prelude::*; -use winnow::token::{take_till1, take_while}; +use winnow::token::{take_till, take_while}; /// Parse a string. Use a loop of `parse_fragment` and push all of the fragments /// into an output string. @@ -78,13 +78,13 @@ where /// Parse a non-empty block of text that doesn't include \ or " fn parse_literal<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<&'a str, E> { - // `take_till1` parses a string of 0 or more characters that aren't one of the + // `take_till` parses a string of 0 or more characters that aren't one of the // given characters. - let not_quote_slash = take_till1(['"', '\\']); + let not_quote_slash = take_till(1.., ['"', '\\']); // `verify` runs a parser, then runs a verification function on the output of // the parser. The verification function accepts the output only if it - // returns true. In this case, we want to ensure that the output of take_till1 + // returns true. In this case, we want to ensure that the output of take_till // is non-empty. not_quote_slash .verify(|s: &str| !s.is_empty()) diff --git a/src/ascii/mod.rs b/src/ascii/mod.rs index 35c942ac..a026b1bc 100644 --- a/src/ascii/mod.rs +++ b/src/ascii/mod.rs @@ -15,7 +15,7 @@ use crate::error::{ErrMode, ErrorKind, Needed}; use crate::stream::{AsBStr, AsChar, ParseSlice, Stream, StreamIsPartial}; use crate::stream::{Compare, CompareResult}; use crate::token::one_of; -use crate::token::take_till0; +use crate::token::take_till; use crate::token::take_while; use crate::trace::trace; use crate::PResult; @@ -144,7 +144,7 @@ where I: Compare<&'static str>, ::Token: AsChar + Clone, { - let res = take_till0(('\r', '\n')).parse_next(input)?; + let res = take_till(0.., ('\r', '\n')).parse_next(input)?; if input.compare("\r") == CompareResult::Ok { let comp = input.compare("\r\n"); match comp { diff --git a/src/token/mod.rs b/src/token/mod.rs index aa0be1fc..839821f7 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -756,6 +756,7 @@ where /// assert_eq!(till_colon(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] +#[doc(alias = "is_not")] pub fn take_till>( range: impl Into, list: T, @@ -834,6 +835,7 @@ where /// assert_eq!(till_colon(Partial::new("12345")), Err(ErrMode::Incomplete(Needed::new(1)))); /// assert_eq!(till_colon(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` +#[deprecated(since = "0.5.21", note = "Replaced with `take_till(0.., ...)`")] #[inline(always)] pub fn take_till0>( list: T, @@ -911,7 +913,7 @@ where /// assert_eq!(not_space(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -#[doc(alias = "is_not")] +#[deprecated(since = "0.5.21", note = "Replaced with `take_till(1.., ...)`")] pub fn take_till1>( list: T, ) -> impl Parser::Slice, Error> diff --git a/src/token/tests.rs b/src/token/tests.rs index c39ba605..696187d6 100644 --- a/src/token/tests.rs +++ b/src/token/tests.rs @@ -184,7 +184,7 @@ fn partial_is_a() { #[test] fn partial_is_not() { fn a_or_b(i: Partial<&[u8]>) -> IResult, &[u8]> { - take_till1(['a', 'b']).parse_peek(i) + take_till(1.., ['a', 'b']).parse_peek(i) } let a = Partial::new(&b"cdab"[..]); @@ -366,7 +366,7 @@ fn partial_take_while_m_n() { #[test] fn partial_take_till0() { fn f(i: Partial<&[u8]>) -> IResult, &[u8]> { - take_till0(AsChar::is_alpha).parse_peek(i) + take_till(0.., AsChar::is_alpha).parse_peek(i) } let a = &b""[..]; let b = &b"abcd"[..]; @@ -388,7 +388,7 @@ fn partial_take_till0() { #[test] fn partial_take_till1() { fn f(i: Partial<&[u8]>) -> IResult, &[u8]> { - take_till1(AsChar::is_alpha).parse_peek(i) + take_till(1.., AsChar::is_alpha).parse_peek(i) } let a = &b""[..]; let b = &b"abcd"[..]; @@ -448,7 +448,7 @@ fn partial_take_while_utf8() { #[test] fn partial_take_till0_utf8() { fn f(i: Partial<&str>) -> IResult, &str> { - take_till0(|c| c == '點').parse_peek(i) + take_till(0.., |c| c == '點').parse_peek(i) } assert_eq!( @@ -466,7 +466,7 @@ fn partial_take_till0_utf8() { ); fn g(i: Partial<&str>) -> IResult, &str> { - take_till0(|c| c != '點').parse_peek(i) + take_till(0.., |c| c != '點').parse_peek(i) } assert_eq!( diff --git a/tests/testsuite/issues.rs b/tests/testsuite/issues.rs index 2346b188..f99a0c3a 100644 --- a/tests/testsuite/issues.rs +++ b/tests/testsuite/issues.rs @@ -77,10 +77,10 @@ fn usize_length_bytes_issue() { #[test] fn take_till0_issue() { - use winnow::token::take_till0; + use winnow::token::take_till; fn nothing(i: Partial<&[u8]>) -> IResult, &[u8]> { - take_till0(|_| true).parse_peek(i) + take_till(0.., |_| true).parse_peek(i) } assert_eq!( @@ -126,9 +126,9 @@ fn issue_655() { #[cfg(feature = "alloc")] fn issue_717(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { use winnow::combinator::separated; - use winnow::token::{tag, take_till1}; + use winnow::token::{tag, take_till}; - separated(0.., take_till1([0x0u8]), tag([0x0])).parse_peek(i) + separated(0.., take_till(1.., [0x0u8]), tag([0x0])).parse_peek(i) } mod issue_647 { diff --git a/tests/testsuite/reborrow_fold.rs b/tests/testsuite/reborrow_fold.rs index a3e9f716..f4597216 100644 --- a/tests/testsuite/reborrow_fold.rs +++ b/tests/testsuite/reborrow_fold.rs @@ -7,11 +7,11 @@ use winnow::combinator::delimited; use winnow::combinator::fold_repeat; use winnow::error::InputError; use winnow::prelude::*; -use winnow::token::take_till1; +use winnow::token::take_till; use winnow::IResult; fn atom<'a>(_tomb: &mut ()) -> impl Parser<&'a [u8], String, InputError<&'a [u8]>> { - take_till1([' ', '\t', '\r', '\n']) + take_till(1.., [' ', '\t', '\r', '\n']) .try_map(str::from_utf8) .map(ToString::to_string) } diff --git a/tests/testsuite/utf8.rs b/tests/testsuite/utf8.rs index 0b6baf91..7419a546 100644 --- a/tests/testsuite/utf8.rs +++ b/tests/testsuite/utf8.rs @@ -8,7 +8,7 @@ mod test { use winnow::{ error::ErrMode, error::{ErrorKind, InputError}, - token::{take, take_till0, take_till1, take_until0, take_while}, + token::{take, take_till, take_until0, take_while}, IResult, }; @@ -426,7 +426,7 @@ mod test { c == 'á' } fn test(input: &str) -> IResult<&str, &str> { - take_till0(till_s).parse_peek(input) + take_till(0.., till_s).parse_peek(input) } match test(INPUT) { Ok((extra, output)) => { @@ -457,7 +457,7 @@ mod test { const CONSUMED: &str = "βèƒôřèÂßÇ"; const LEFTOVER: &str = "áƒƭèř"; fn test(input: &str) -> IResult<&str, &str> { - take_till1(AVOID).parse_peek(input) + take_till(1.., AVOID).parse_peek(input) } match test(INPUT) { Ok((extra, output)) => { @@ -486,7 +486,7 @@ mod test { const INPUT: &str = "βèƒôřèÂßÇáƒƭèř"; const AVOID: &[char] = &['β', 'ú', 'ç', 'ƙ', '¥']; fn test(input: &str) -> IResult<&str, &str> { - take_till1(AVOID).parse_peek(input) + take_till(1.., AVOID).parse_peek(input) } match test(INPUT) { Err(ErrMode::Backtrack(_)) => (),