Skip to content

Commit

Permalink
fix: Deprecate take_till0, take_till1
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 4, 2023
1 parent 56b9869 commit 4f38725
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 30 deletions.
32 changes: 26 additions & 6 deletions benches/contains_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -52,17 +52,29 @@ fn contains_token(c: &mut criterion::Criterion) {

fn parser_slice(input: &mut &str) -> PResult<usize> {
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<usize> {
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<usize> {
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<usize> {
Expand All @@ -78,12 +90,20 @@ fn parser_closure_or(input: &mut &str) -> PResult<usize> {
|| 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<usize> {
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";
Expand Down
4 changes: 2 additions & 2 deletions examples/ini/parser_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,7 +36,7 @@ fn keys_and_values<'s>(input: &mut Stream<'s>) -> PResult<HashMap<&'s str, &'s s
fn key_value<'s>(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)?;
Expand Down
8 changes: 4 additions & 4 deletions examples/string/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -144,7 +144,7 @@ where
I: Compare<&'static str>,
<I as Stream>::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 {
Expand Down
4 changes: 3 additions & 1 deletion src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, I, Error: ParserError<I>>(
range: impl Into<Range>,
list: T,
Expand Down Expand Up @@ -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<T, I, Error: ParserError<I>>(
list: T,
Expand Down Expand Up @@ -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<T, I, Error: ParserError<I>>(
list: T,
) -> impl Parser<I, <I as Stream>::Slice, Error>
Expand Down
10 changes: 5 additions & 5 deletions src/token/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn partial_is_a() {
#[test]
fn partial_is_not() {
fn a_or_b(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> {
take_till1(['a', 'b']).parse_peek(i)
take_till(1.., ['a', 'b']).parse_peek(i)
}

let a = Partial::new(&b"cdab"[..]);
Expand Down Expand Up @@ -366,7 +366,7 @@ fn partial_take_while_m_n() {
#[test]
fn partial_take_till0() {
fn f(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[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"[..];
Expand All @@ -388,7 +388,7 @@ fn partial_take_till0() {
#[test]
fn partial_take_till1() {
fn f(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[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"[..];
Expand Down Expand Up @@ -448,7 +448,7 @@ fn partial_take_while_utf8() {
#[test]
fn partial_take_till0_utf8() {
fn f(i: Partial<&str>) -> IResult<Partial<&str>, &str> {
take_till0(|c| c == '點').parse_peek(i)
take_till(0.., |c| c == '點').parse_peek(i)
}

assert_eq!(
Expand All @@ -466,7 +466,7 @@ fn partial_take_till0_utf8() {
);

fn g(i: Partial<&str>) -> IResult<Partial<&str>, &str> {
take_till0(|c| c != '點').parse_peek(i)
take_till(0.., |c| c != '點').parse_peek(i)
}

assert_eq!(
Expand Down
8 changes: 4 additions & 4 deletions tests/testsuite/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Partial<&[u8]>, &[u8]> {
take_till0(|_| true).parse_peek(i)
take_till(0.., |_| true).parse_peek(i)
}

assert_eq!(
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/reborrow_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions tests/testsuite/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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)) => {
Expand Down Expand Up @@ -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)) => {
Expand Down Expand Up @@ -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(_)) => (),
Expand Down

0 comments on commit 4f38725

Please sign in to comment.