Skip to content

Commit

Permalink
Merge pull request #132 from epage/zero
Browse files Browse the repository at this point in the history
fix!: Consistently apply `0` suffix to parsers
  • Loading branch information
epage committed Feb 7, 2023
2 parents 8ad67de + aa49fc0 commit 5ea86e7
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 110 deletions.
8 changes: 4 additions & 4 deletions examples/ini/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str;

use winnow::prelude::*;
use winnow::{
bytes::take_while,
bytes::take_while0,
character::{alphanumeric1 as alphanumeric, multispace0 as multispace, space0 as space},
combinator::opt,
multi::many0,
Expand All @@ -22,18 +22,18 @@ pub fn categories(i: Input<'_>) -> IResult<Input<'_>, HashMap<&str, HashMap<&str
}

fn category(i: Input<'_>) -> IResult<Input<'_>, &str> {
delimited('[', take_while(|c| c != b']'), ']')
delimited('[', take_while0(|c| c != b']'), ']')
.map_res(str::from_utf8)
.parse_next(i)
}

pub fn key_value(i: Input<'_>) -> IResult<Input<'_>, (&str, &str)> {
let (i, key) = alphanumeric.map_res(str::from_utf8).parse_next(i)?;
let (i, _) = (opt(space), '=', opt(space)).parse_next(i)?;
let (i, val) = take_while(|c| c != b'\n' && c != b';')
let (i, val) = take_while0(|c| c != b'\n' && c != b';')
.map_res(str::from_utf8)
.parse_next(i)?;
let (i, _) = opt((';', take_while(|c| c != b'\n')))(i)?;
let (i, _) = opt((';', take_while0(|c| c != b'\n')))(i)?;
Ok((i, (key, val)))
}

Expand Down
8 changes: 4 additions & 4 deletions examples/ini/parser_str.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::{
bytes::{take_till, take_while, take_while1},
bytes::{take_till0, take_while0, take_while1},
character::{alphanumeric1 as alphanumeric, space0 as space},
combinator::opt,
multi::many0,
Expand All @@ -21,7 +21,7 @@ fn category_and_keys(i: Input<'_>) -> IResult<Input<'_>, (&str, HashMap<&str, &s

fn category(i: Input<'_>) -> IResult<Input<'_>, &str> {
terminated(
delimited('[', take_while(|c| c != ']'), ']'),
delimited('[', take_while0(|c| c != ']'), ']'),
opt(take_while1(" \r\n")),
)(i)
}
Expand All @@ -33,7 +33,7 @@ fn keys_and_values(input: Input<'_>) -> IResult<Input<'_>, HashMap<&str, &str>>
fn key_value(i: Input<'_>) -> IResult<Input<'_>, (&str, &str)> {
let (i, key) = alphanumeric(i)?;
let (i, _) = (opt(space), "=", opt(space)).parse_next(i)?;
let (i, val) = take_till(is_line_ending_or_comment)(i)?;
let (i, val) = take_till0(is_line_ending_or_comment)(i)?;
let (i, _) = opt(space)(i)?;
let (i, _) = opt((";", not_line_ending))(i)?;
let (i, _) = opt(space_or_line_ending)(i)?;
Expand All @@ -46,7 +46,7 @@ fn is_line_ending_or_comment(chr: char) -> bool {
}

fn not_line_ending(i: Input<'_>) -> IResult<Input<'_>, &str> {
take_while(|c| c != '\r' && c != '\n')(i)
take_while0(|c| c != '\r' && c != '\n')(i)
}

fn space_or_line_ending(i: Input<'_>) -> IResult<Input<'_>, &str> {
Expand Down
6 changes: 3 additions & 3 deletions examples/json/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str;
use winnow::prelude::*;
use winnow::{
branch::alt,
bytes::{any, none_of, tag, take, take_while},
bytes::{any, none_of, tag, take, take_while0},
character::float,
combinator::cut_err,
error::{ContextError, ParseError},
Expand Down Expand Up @@ -193,9 +193,9 @@ fn key_value<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static str
/// first we write parsers for the smallest elements (here a space character),
/// then we'll combine them in larger parsers
fn ws<'i, E: ParseError<Input<'i>>>(input: Input<'i>) -> IResult<Input<'i>, &'i str, E> {
// nom combinators like `take_while` return a function. That function is the
// nom combinators like `take_while0` return a function. That function is the
// parser,to which we can pass the input
take_while(WS)(input)
take_while0(WS)(input)
}

const WS: &str = " \t\r\n";
Expand Down
6 changes: 3 additions & 3 deletions examples/json/parser_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str;
use winnow::prelude::*;
use winnow::{
branch::{alt, dispatch},
bytes::{any, none_of, tag, take, take_while},
bytes::{any, none_of, tag, take, take_while0},
character::float,
combinator::cut_err,
combinator::fail,
Expand Down Expand Up @@ -202,9 +202,9 @@ fn key_value<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static str
/// first we write parsers for the smallest elements (here a space character),
/// then we'll combine them in larger parsers
fn ws<'i, E: ParseError<Input<'i>>>(input: Input<'i>) -> IResult<Input<'i>, &'i str, E> {
// nom combinators like `take_while` return a function. That function is the
// nom combinators like `take_while0` return a function. That function is the
// parser,to which we can pass the input
take_while(WS)(input)
take_while0(WS)(input)
}

const WS: &str = " \t\r\n";
Expand Down
6 changes: 3 additions & 3 deletions examples/json/parser_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str;
use winnow::prelude::*;
use winnow::{
branch::alt,
bytes::{any, none_of, one_of, tag, take, take_while},
bytes::{any, none_of, one_of, tag, take, take_while0},
character::float,
combinator::{cut_err, rest},
error::{ContextError, ParseError},
Expand Down Expand Up @@ -202,9 +202,9 @@ fn key_value<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static str
/// first we write parsers for the smallest elements (here a space character),
/// then we'll combine them in larger parsers
fn ws<'i, E: ParseError<Input<'i>>>(input: Input<'i>) -> IResult<Input<'i>, &'i str, E> {
// nom combinators like `take_while` return a function. That function is the
// nom combinators like `take_while0` return a function. That function is the
// parser,to which we can pass the input
take_while(WS)(input)
take_while0(WS)(input)
}

fn ws_or_eof<'i, E: ParseError<Input<'i>>>(input: Input<'i>) -> IResult<Input<'i>, &'i str, E> {
Expand Down
4 changes: 2 additions & 2 deletions examples/json_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use winnow::prelude::*;
use winnow::{
branch::alt,
bytes::one_of,
bytes::{tag, take_while},
bytes::{tag, take_while0},
character::{alphanumeric1 as alphanumeric, escaped, float},
combinator::cut_err,
error::ParseError,
Expand Down Expand Up @@ -206,7 +206,7 @@ impl<'a, 'b: 'a> JsonValue<'a, 'b> {
fn sp<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
let chars = " \t\r\n";

take_while(move |c| chars.contains(c))(i)
take_while0(move |c| chars.contains(c))(i)
}

fn parse_str<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
Expand Down
10 changes: 5 additions & 5 deletions src/_cookbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@
//! use winnow::prelude::*;
//! use winnow::{
//! error::ParseError,
//! bytes::{tag, take_until},
//! bytes::{tag, take_until0},
//! };
//!
//! pub fn pinline_comment<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, (), E> {
//! (
//! "(*",
//! take_until("*)"),
//! take_until0("*)"),
//! "*)"
//! )
//! .void() // Output is thrown away.
Expand All @@ -109,14 +109,14 @@
//! use winnow::prelude::*;
//! use winnow::{
//! input::AsChar,
//! bytes::take_while,
//! bytes::take_while0,
//! bytes::one_of,
//! };
//!
//! pub fn identifier(input: &str) -> IResult<&str, &str> {
//! (
//! one_of(|c: char| c.is_alpha() || c == '_'),
//! take_while(|c: char| c.is_alphanum() || c == '_')
//! take_while0(|c: char| c.is_alphanum() || c == '_')
//! )
//! .recognize()
//! .parse_next(input)
Expand All @@ -125,7 +125,7 @@
//!
//! Let's say we apply this to the identifier `hello_world123abc`. The first element of the tuple
//! would uses [`one_of`][crate::bytes::one_of] which would recognize `h`. The tuple ensures that
//! `ello_world123abc` will be piped to the next [`take_while`][crate::bytes::take_while] parser,
//! `ello_world123abc` will be piped to the next [`take_while0`][crate::bytes::take_while0] parser,
//! which recognizes every remaining character. However, the tuple returns a tuple of the results
//! of its sub-parsers. The [`recognize`][crate::Parser::recognize] parser produces a `&str` of the
//! input text that was parsed, which in this case is the entire `&str` `hello_world123abc`.
Expand Down
12 changes: 6 additions & 6 deletions src/bytes/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ where
/// assert_eq!(alpha(b""), Ok((&b""[..], &b""[..])));
/// ```
///
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_while`][crate::bytes::take_while]
#[deprecated(since = "0.1.0", note = "Replaced with `winnow::bytes::take_while`")]
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_while0`][crate::bytes::take_while0]
#[deprecated(since = "0.1.0", note = "Replaced with `winnow::bytes::take_while0`")]
pub fn take_while<T, I, Error: ParseError<I>>(
list: T,
) -> impl Fn(I) -> IResult<I, <I as Input>::Slice, Error>
Expand Down Expand Up @@ -431,8 +431,8 @@ where
/// assert_eq!(till_colon(""), Ok(("", "")));
/// ```
///
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_till`][crate::bytes::take_till]
#[deprecated(since = "0.1.0", note = "Replaced with `winnow::bytes::take_till`")]
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_till0`][crate::bytes::take_till0]
#[deprecated(since = "0.1.0", note = "Replaced with `winnow::bytes::take_till0`")]
#[allow(clippy::redundant_closure)]
pub fn take_till<T, I, Error: ParseError<I>>(
list: T,
Expand Down Expand Up @@ -577,8 +577,8 @@ where
/// assert_eq!(until_eof("1eof2eof"), Ok(("eof2eof", "1")));
/// ```
///
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_until`][crate::bytes::take_until]
#[deprecated(since = "0.1.0", note = "Replaced with `winnow::bytes::take_until`")]
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_until0`][crate::bytes::take_until0]
#[deprecated(since = "0.1.0", note = "Replaced with `winnow::bytes::take_until0`")]
pub fn take_until<T, I, Error: ParseError<I>>(
tag: T,
) -> impl Fn(I) -> IResult<I, <I as Input>::Slice, Error>
Expand Down
30 changes: 15 additions & 15 deletions src/bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ where
/// # Example
/// ```rust
/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed, IResult};
/// use winnow::bytes::take_while;
/// use winnow::bytes::take_while0;
/// use winnow::input::AsChar;
///
/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
/// take_while(AsChar::is_alpha)(s)
/// take_while0(AsChar::is_alpha)(s)
/// }
///
/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
Expand All @@ -291,11 +291,11 @@ where
/// ```rust
/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed, IResult};
/// # use winnow::input::Streaming;
/// use winnow::bytes::take_while;
/// use winnow::bytes::take_while0;
/// use winnow::input::AsChar;
///
/// fn alpha(s: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, &[u8]> {
/// take_while(AsChar::is_alpha)(s)
/// take_while0(AsChar::is_alpha)(s)
/// }
///
/// assert_eq!(alpha(Streaming(b"latin123")), Ok((Streaming(&b"123"[..]), &b"latin"[..])));
Expand All @@ -304,7 +304,7 @@ where
/// assert_eq!(alpha(Streaming(b"")), Err(ErrMode::Incomplete(Needed::new(1))));
/// ```
#[inline(always)]
pub fn take_while<T, I, Error: ParseError<I>, const STREAMING: bool>(
pub fn take_while0<T, I, Error: ParseError<I>, const STREAMING: bool>(
list: T,
) -> impl Fn(I) -> IResult<I, <I as Input>::Slice, Error>
where
Expand Down Expand Up @@ -462,10 +462,10 @@ where
/// # Example
/// ```rust
/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed, IResult};
/// use winnow::bytes::take_till;
/// use winnow::bytes::take_till0;
///
/// fn till_colon(s: &str) -> IResult<&str, &str> {
/// take_till(|c| c == ':')(s)
/// take_till0(|c| c == ':')(s)
/// }
///
/// assert_eq!(till_colon("latin:123"), Ok((":123", "latin")));
Expand All @@ -477,10 +477,10 @@ where
/// ```rust
/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed, IResult};
/// # use winnow::input::Streaming;
/// use winnow::bytes::take_till;
/// use winnow::bytes::take_till0;
///
/// fn till_colon(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
/// take_till(|c| c == ':')(s)
/// take_till0(|c| c == ':')(s)
/// }
///
/// assert_eq!(till_colon(Streaming("latin:123")), Ok((Streaming(":123"), "latin")));
Expand All @@ -489,7 +489,7 @@ where
/// assert_eq!(till_colon(Streaming("")), Err(ErrMode::Incomplete(Needed::new(1))));
/// ```
#[inline(always)]
pub fn take_till<T, I, Error: ParseError<I>, const STREAMING: bool>(
pub fn take_till0<T, I, Error: ParseError<I>, const STREAMING: bool>(
list: T,
) -> impl Fn(I) -> IResult<I, <I as Input>::Slice, Error>
where
Expand Down Expand Up @@ -662,10 +662,10 @@ where
/// # Example
/// ```rust
/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed, IResult};
/// use winnow::bytes::take_until;
/// use winnow::bytes::take_until0;
///
/// fn until_eof(s: &str) -> IResult<&str, &str> {
/// take_until("eof")(s)
/// take_until0("eof")(s)
/// }
///
/// assert_eq!(until_eof("hello, worldeof"), Ok(("eof", "hello, world")));
Expand All @@ -677,10 +677,10 @@ where
/// ```rust
/// # use winnow::{error::ErrMode, error::ErrorKind, error::Error, error::Needed, IResult};
/// # use winnow::input::Streaming;
/// use winnow::bytes::take_until;
/// use winnow::bytes::take_until0;
///
/// fn until_eof(s: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
/// take_until("eof")(s)
/// take_until0("eof")(s)
/// }
///
/// assert_eq!(until_eof(Streaming("hello, worldeof")), Ok((Streaming("eof"), "hello, world")));
Expand All @@ -689,7 +689,7 @@ where
/// assert_eq!(until_eof(Streaming("1eof2eof")), Ok((Streaming("eof2eof"), "1")));
/// ```
#[inline(always)]
pub fn take_until<T, I, Error: ParseError<I>, const STREAMING: bool>(
pub fn take_until0<T, I, Error: ParseError<I>, const STREAMING: bool>(
tag: T,
) -> impl Fn(I) -> IResult<I, <I as Input>::Slice, Error>
where
Expand Down
12 changes: 6 additions & 6 deletions src/bytes/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ where
/// assert_eq!(alpha(b""), Err(ErrMode::Incomplete(Needed::new(1))));
/// ```
///
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_while`][crate::bytes::take_while] with input wrapped in [`winnow::input::Streaming`][crate::input::Streaming]
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_while0`][crate::bytes::take_while0] with input wrapped in [`winnow::input::Streaming`][crate::input::Streaming]
#[deprecated(
since = "0.1.0",
note = "Replaced with `winnow::bytes::take_while` with input wrapped in `winnow::input::Streaming`"
note = "Replaced with `winnow::bytes::take_while0` with input wrapped in `winnow::input::Streaming`"
)]
pub fn take_while<T, I, Error: ParseError<I>>(
list: T,
Expand Down Expand Up @@ -478,10 +478,10 @@ where
/// assert_eq!(till_colon(""), Err(ErrMode::Incomplete(Needed::new(1))));
/// ```
///
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_till`][crate::bytes::take_till] with input wrapped in [`winnow::input::Streaming`][crate::input::Streaming]
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_till0`][crate::bytes::take_till0] with input wrapped in [`winnow::input::Streaming`][crate::input::Streaming]
#[deprecated(
since = "0.1.0",
note = "Replaced with `winnow::bytes::take_till` with input wrapped in `winnow::input::Streaming`"
note = "Replaced with `winnow::bytes::take_till0` with input wrapped in `winnow::input::Streaming`"
)]
#[allow(clippy::redundant_closure)]
pub fn take_till<T, I, Error: ParseError<I>>(
Expand Down Expand Up @@ -631,10 +631,10 @@ where
/// assert_eq!(until_eof("1eof2eof"), Ok(("eof2eof", "1")));
/// ```
///
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_until`][crate::bytes::take_until] with input wrapped in [`winnow::input::Streaming`][crate::input::Streaming]
/// **WARNING:** Deprecated, replaced with [`winnow::bytes::take_until0`][crate::bytes::take_until0] with input wrapped in [`winnow::input::Streaming`][crate::input::Streaming]
#[deprecated(
since = "0.1.0",
note = "Replaced with `winnow::bytes::take_until` with input wrapped in `winnow::input::Streaming`"
note = "Replaced with `winnow::bytes::take_until0` with input wrapped in `winnow::input::Streaming`"
)]
pub fn take_until<T, I, Error: ParseError<I>>(
tag: T,
Expand Down

0 comments on commit 5ea86e7

Please sign in to comment.