aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bytes/complete.rs12
-rw-r--r--src/character/complete.rs18
-rw-r--r--src/lib.rs22
-rw-r--r--src/multi/tests.rs10
-rw-r--r--src/number/complete.rs13
-rw-r--r--src/number/streaming.rs13
-rw-r--r--src/sequence/mod.rs2
-rw-r--r--src/traits.rs12
8 files changed, 60 insertions, 42 deletions
diff --git a/src/bytes/complete.rs b/src/bytes/complete.rs
index 9375b1f..a5442b5 100644
--- a/src/bytes/complete.rs
+++ b/src/bytes/complete.rs
@@ -388,6 +388,18 @@ where
/// assert_eq!(take6("short"), Err(Err::Error(Error::new("short", ErrorKind::Eof))));
/// assert_eq!(take6(""), Err(Err::Error(Error::new("", ErrorKind::Eof))));
/// ```
+///
+/// The units that are taken will depend on the input type. For example, for a
+/// `&str` it will take a number of `char`'s, whereas for a `&[u8]` it will
+/// take that many `u8`'s:
+///
+/// ```rust
+/// use nom::error::Error;
+/// use nom::bytes::complete::take;
+///
+/// assert_eq!(take::<_, _, Error<_>>(1usize)("💙"), Ok(("", "💙")));
+/// assert_eq!(take::<_, _, Error<_>>(1usize)("💙".as_bytes()), Ok((b"\x9F\x92\x99".as_ref(), b"\xF0".as_ref())));
+/// ```
pub fn take<C, Input, Error: ParseError<Input>>(
count: C,
) -> impl Fn(Input) -> IResult<Input, Input, Error>
diff --git a/src/character/complete.rs b/src/character/complete.rs
index 5b73c14..a98bafb 100644
--- a/src/character/complete.rs
+++ b/src/character/complete.rs
@@ -414,6 +414,24 @@ where
/// assert_eq!(parser("c1"), Err(Err::Error(Error::new("c1", ErrorKind::Digit))));
/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Digit))));
/// ```
+///
+/// ## Parsing an integer
+/// You can use `digit1` in combination with [`map_res`] to parse an integer:
+///
+/// ```
+/// # use nom::{Err, error::{Error, ErrorKind}, IResult, Needed};
+/// # use nom::combinator::map_res;
+/// # use nom::character::complete::digit1;
+/// fn parser(input: &str) -> IResult<&str, u32> {
+/// map_res(digit1, str::parse)(input)
+/// }
+///
+/// assert_eq!(parser("416"), Ok(("", 416)));
+/// assert_eq!(parser("12b"), Ok(("b", 12)));
+/// assert!(parser("b").is_err());
+/// ```
+///
+/// [`map_res`]: crate::combinator::map_res
pub fn digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
where
T: InputTakeAtPosition,
diff --git a/src/lib.rs b/src/lib.rs
index 1ce115a..9429467 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -52,16 +52,16 @@
//!
//! The code is available on [Github](https://github.com/Geal/nom)
//!
-//! There are a few [guides](https://github.com/Geal/nom/tree/master/doc) with more details
-//! about [how to write parsers](https://github.com/Geal/nom/blob/master/doc/making_a_new_parser_from_scratch.md),
-//! or the [error management system](https://github.com/Geal/nom/blob/master/doc/error_management.md).
+//! There are a few [guides](https://github.com/Geal/nom/tree/main/doc) with more details
+//! about [how to write parsers](https://github.com/Geal/nom/blob/main/doc/making_a_new_parser_from_scratch.md),
+//! or the [error management system](https://github.com/Geal/nom/blob/main/doc/error_management.md).
//! You can also check out the [recipes] module that contains examples of common patterns.
//!
//! **Looking for a specific combinator? Read the
-//! ["choose a combinator" guide](https://github.com/Geal/nom/blob/master/doc/choosing_a_combinator.md)**
+//! ["choose a combinator" guide](https://github.com/Geal/nom/blob/main/doc/choosing_a_combinator.md)**
//!
//! If you are upgrading to nom 5.0, please read the
-//! [migration document](https://github.com/Geal/nom/blob/master/doc/upgrading_to_nom_5.md).
+//! [migration document](https://github.com/Geal/nom/blob/main/doc/upgrading_to_nom_5.md).
//!
//! ## Parser combinators
//!
@@ -167,8 +167,8 @@
//! - An error `Err(Err::Incomplete(Needed))` indicating that more input is necessary. `Needed` can indicate how much data is needed
//! - An error `Err(Err::Failure(c))`. It works like the `Error` case, except it indicates an unrecoverable error: We cannot backtrack and test another parser
//!
-//! Please refer to the ["choose a combinator" guide](https://github.com/Geal/nom/blob/master/doc/choosing_a_combinator.md) for an exhaustive list of parsers.
-//! See also the rest of the documentation [here](https://github.com/Geal/nom/blob/master/doc).
+//! Please refer to the ["choose a combinator" guide](https://github.com/Geal/nom/blob/main/doc/choosing_a_combinator.md) for an exhaustive list of parsers.
+//! See also the rest of the documentation [here](https://github.com/Geal/nom/blob/main/doc).
//!
//! ## Making new parsers with function combinators
//!
@@ -253,7 +253,7 @@
//! - **`many0`**: Will apply the parser 0 or more times (if it returns the `O` type, the new parser returns `Vec<O>`)
//! - **`many1`**: Will apply the parser 1 or more times
//!
-//! There are more complex (and more useful) parsers like `tuple!`, which is
+//! There are more complex (and more useful) parsers like `tuple`, which is
//! used to apply a series of parsers then assemble their results.
//!
//! Example with `tuple`:
@@ -369,11 +369,10 @@
//! // while the complete version knows that all of the data is there
//! assert_eq!(alpha0_complete("abcd"), Ok(("", "abcd")));
//! ```
-//! **Going further:** Read the [guides](https://github.com/Geal/nom/tree/master/doc),
+//! **Going further:** Read the [guides](https://github.com/Geal/nom/tree/main/doc),
//! check out the [recipes]!
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::doc_markdown))]
-#![cfg_attr(nightly, feature(test))]
#![cfg_attr(feature = "docsrs", feature(doc_cfg))]
#![cfg_attr(feature = "docsrs", feature(extended_key_value_attributes))]
#![deny(missing_docs)]
@@ -384,9 +383,6 @@ extern crate alloc;
#[cfg(doctest)]
extern crate doc_comment;
-#[cfg(nightly)]
-extern crate test;
-
#[cfg(doctest)]
doc_comment::doctest!("../README.md");
diff --git a/src/multi/tests.rs b/src/multi/tests.rs
index 2a96f84..96a6518 100644
--- a/src/multi/tests.rs
+++ b/src/multi/tests.rs
@@ -129,16 +129,6 @@ fn many0_test() {
);
}
-#[cfg(nightly)]
-use test::Bencher;
-
-#[cfg(nightly)]
-#[bench]
-fn many0_bench(b: &mut Bencher) {
- named!(multi<&[u8],Vec<&[u8]> >, many0!(tag!("abcd")));
- b.iter(|| multi(&b"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"[..]));
-}
-
#[test]
#[cfg(feature = "alloc")]
fn many1_test() {
diff --git a/src/number/complete.rs b/src/number/complete.rs
index d23079e..715da32 100644
--- a/src/number/complete.rs
+++ b/src/number/complete.rs
@@ -211,7 +211,6 @@ where
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -365,7 +364,6 @@ where
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -551,7 +549,6 @@ where
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -705,7 +702,6 @@ where
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -926,7 +922,6 @@ where
/// assert_eq!(le_u128(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn u128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -1146,7 +1141,6 @@ where
/// assert_eq!(le_i128(&b"\x01"[..]), Err(Err::Error((&[0x01][..], ErrorKind::Eof))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn i128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -1460,7 +1454,10 @@ where
))(input)
}
-/// Recognizes a floating point number in text format and returns the integer, fraction and exponent parts of the input data
+/// Recognizes a floating point number in text format
+///
+/// It returns a tuple of (`sign`, `integer part`, `fraction part` and `exponent`) of the input
+/// data.
///
/// *Complete version*: Can parse until the end of input.
///
@@ -1752,7 +1749,6 @@ mod tests {
}
#[test]
- #[cfg(stable_i128)]
fn be_i128_tests() {
assert_parse!(
be_i128(
@@ -1869,7 +1865,6 @@ mod tests {
}
#[test]
- #[cfg(stable_i128)]
fn le_i128_tests() {
assert_parse!(
le_i128(
diff --git a/src/number/streaming.rs b/src/number/streaming.rs
index 7c53ccc..9ee9f48 100644
--- a/src/number/streaming.rs
+++ b/src/number/streaming.rs
@@ -208,7 +208,6 @@ where
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn be_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -345,7 +344,6 @@ where
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn be_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -528,7 +526,6 @@ where
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn le_u128<I, E: ParseError<I>>(input: I) -> IResult<I, u128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -679,7 +676,6 @@ where
/// assert_eq!(parser(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn le_i128<I, E: ParseError<I>>(input: I) -> IResult<I, i128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -900,7 +896,6 @@ where
/// assert_eq!(le_u128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn u128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, u128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -1120,7 +1115,6 @@ where
/// assert_eq!(le_i128(&b"\x01"[..]), Err(Err::Incomplete(Needed::new(15))));
/// ```
#[inline]
-#[cfg(stable_i128)]
pub fn i128<I, E: ParseError<I>>(endian: crate::number::Endianness) -> fn(I) -> IResult<I, i128, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + InputLength,
@@ -1429,7 +1423,10 @@ where
))(input)
}
-/// Recognizes a floating point number in text format and returns the integer, fraction and exponent parts of the input data
+/// Recognizes a floating point number in text format
+///
+/// It returns a tuple of (`sign`, `integer part`, `fraction part` and `exponent`) of the input
+/// data.
///
/// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data.
///
@@ -1768,7 +1765,6 @@ mod tests {
}
#[test]
- #[cfg(stable_i128)]
fn i128_tests() {
assert_parse!(
be_i128(
@@ -1948,7 +1944,6 @@ mod tests {
}
#[test]
- #[cfg(stable_i128)]
fn le_i128_tests() {
assert_parse!(
le_i128(
diff --git a/src/sequence/mod.rs b/src/sequence/mod.rs
index 100c63f..e09d413 100644
--- a/src/sequence/mod.rs
+++ b/src/sequence/mod.rs
@@ -253,7 +253,7 @@ tuple_trait!(FnA A, FnB B, FnC C, FnD D, FnE E, FnF F, FnG G, FnH H, FnI I, FnJ
FnM M, FnN N, FnO O, FnP P, FnQ Q, FnR R, FnS S, FnT T, FnU U);
///Applies a tuple of parsers one by one and returns their results as a tuple.
-///
+///There is a maximum of 21 parsers
/// ```rust
/// # use nom::{Err, error::ErrorKind};
/// use nom::sequence::tuple;
diff --git a/src/traits.rs b/src/traits.rs
index 3c3053e..f6a683c 100644
--- a/src/traits.rs
+++ b/src/traits.rs
@@ -914,6 +914,18 @@ impl<'a> FindToken<char> for &'a str {
}
}
+impl<'a> FindToken<char> for &'a [char] {
+ fn find_token(&self, token: char) -> bool {
+ self.iter().any(|i| *i == token)
+ }
+}
+
+impl<'a, 'b> FindToken<&'a char> for &'b [char] {
+ fn find_token(&self, token: &char) -> bool {
+ self.find_token(*token)
+ }
+}
+
/// Look for a substring in self
pub trait FindSubstring<T> {
/// Returns the byte position of the substring if it is found