diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 978a1d0..1897053 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,7 @@ name: CI on: [push, pull_request] env: - RUST_MINVERSION: 1.63.0 + RUST_MINVERSION: 1.65.0 jobs: test: @@ -16,7 +16,7 @@ jobs: - stable - beta - nightly - - 1.63.0 + - 1.65.0 features: - '' diff --git a/CHANGELOG.md b/CHANGELOG.md index 98e964b..f4b7e55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## master + +Breaking change: + +* [Migrate to 8.0 `nom`](https://github.com/fflorent/nom_locate/pull/96) + * Bump up MSRV to 1.65 + ## v4.2.0 Improvements: diff --git a/Cargo.toml b/Cargo.toml index de9ddb1..ed70ec3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ readme = "README.md" repository = "https://github.com/fflorent/nom_locate" version = "4.2.0" edition = "2018" -rust-version = "1.63.0" +rust-version = "1.65.0" [badges.travis-ci] repository = "fflorent/nom_locate" @@ -27,5 +27,5 @@ stable-deref-trait = ["stable_deref_trait"] [dependencies] bytecount = "^0.6" memchr = { version = ">=1.0.1, <3.0.0", default-features = false } # ^1.0.0 + ^2.0 -nom = { version = "7", default-features = false } +nom = { version = "8", default-features = false } stable_deref_trait = { version = "^1", optional = true, default-features = false } diff --git a/FAQ.md b/FAQ.md index f2da8bf..4669f98 100644 --- a/FAQ.md +++ b/FAQ.md @@ -1,13 +1,11 @@ # FAQ -## How to use LocatedSpan with my own input type? +## How to use `LocatedSpan` with my own input type? -LocatedSpan has been designed to wrap any input type. By default it wraps `&str` and `&[u8]` but it should work with any other types. +`LocatedSpan` has been designed to wrap any input type. By default it wraps `&str` and `&[u8]` but it should work with any other types. To do so, all you need is to ensure that your input type implements these traits: - - `nom::InputLength` - - `nom::Slice` - - `nom::InputIter` + - `nom::Input` - `nom::Compare` - `nom::Offset` - `nom::CompareResult` diff --git a/benches/benches.rs b/benches/benches.rs index 37ed824..9a31747 100644 --- a/benches/benches.rs +++ b/benches/benches.rs @@ -1,7 +1,7 @@ #![feature(test)] extern crate test; -use nom::Slice; +use nom::Input; use nom_locate::LocatedSpan; use test::Bencher; @@ -102,7 +102,7 @@ fn bench_slice_full(b: &mut Bencher) { let input = LocatedSpan::new(TEXT); b.iter(|| { - input.slice(..); + input.take_from(0); }); } @@ -111,16 +111,7 @@ fn bench_slice_from(b: &mut Bencher) { let input = LocatedSpan::new(TEXT); b.iter(|| { - input.slice(200..); - }); -} - -#[bench] -fn bench_slice_from_zero(b: &mut Bencher) { - let input = LocatedSpan::new(TEXT); - - b.iter(|| { - input.slice(0..); + input.take_from(200); }); } @@ -129,7 +120,7 @@ fn bench_slice_to(b: &mut Bencher) { let input = LocatedSpan::new(TEXT); b.iter(|| { - input.slice(..200); + input.take(200); }); } @@ -138,7 +129,7 @@ fn bench_slice(b: &mut Bencher) { let input = LocatedSpan::new(TEXT); b.iter(|| { - input.slice(200..300); + input.take(300).take_from(200); }); } @@ -148,7 +139,7 @@ fn bench_slice_columns_only(b: &mut Bencher) { let input = LocatedSpan::new(text.as_str()); b.iter(|| { - input.slice(499..501).get_utf8_column(); + input.take(501).take_from(499).get_utf8_column(); }); } @@ -161,6 +152,6 @@ fn bench_slice_columns_only_for_ascii_text(b: &mut Bencher) { assert!(text.is_ascii()); b.iter(|| { - input.slice(500..501).get_column(); + input.take(501).take_from(500).get_column(); }); } diff --git a/src/lib.rs b/src/lib.rs index d6fc6d3..7f40a98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,13 +78,9 @@ mod lib { pub mod std { pub use std::fmt::{Display, Formatter, Result as FmtResult}; pub use std::hash::{Hash, Hasher}; - pub use std::iter::{Copied, Enumerate}; - pub use std::ops::{Range, RangeFrom, RangeFull, RangeTo}; pub use std::slice; - pub use std::slice::Iter; - pub use std::str::{CharIndices, Chars, FromStr}; - pub use std::string::{String, ToString}; - pub use std::vec::Vec; + pub use std::str::FromStr; + pub use std::string::ToString; } #[cfg(not(feature = "std"))] @@ -92,15 +88,10 @@ mod lib { #[cfg(feature = "alloc")] pub use alloc::fmt::{Display, Formatter, Result as FmtResult}; #[cfg(feature = "alloc")] - pub use alloc::string::{String, ToString}; - #[cfg(feature = "alloc")] - pub use alloc::vec::Vec; + pub use alloc::string::ToString; pub use core::hash::{Hash, Hasher}; - pub use core::iter::{Copied, Enumerate}; - pub use core::ops::{Range, RangeFrom, RangeFull, RangeTo}; pub use core::slice; - pub use core::slice::Iter; - pub use core::str::{CharIndices, Chars, FromStr}; + pub use core::str::FromStr; } } @@ -111,9 +102,8 @@ use memchr::Memchr; #[cfg(feature = "alloc")] use nom::ExtendInto; use nom::{ - error::{ErrorKind, ParseError}, - AsBytes, Compare, CompareResult, Err, FindSubstring, FindToken, IResult, InputIter, - InputLength, InputTake, InputTakeAtPosition, Offset, ParseTo, Slice, + error::ParseError, AsBytes, Compare, CompareResult, FindSubstring, FindToken, IResult, Input, + Offset, ParseTo, }; #[cfg(feature = "stable-deref-trait")] use stable_deref_trait::StableDeref; @@ -286,30 +276,31 @@ impl LocatedSpan { /// # extern crate nom; /// # use nom_locate::LocatedSpan; /// use nom::{ - /// IResult, + /// IResult, AsChar, Parser, /// combinator::{recognize, map_res}, - /// sequence::{terminated, tuple}, - /// character::{complete::{char, one_of}, is_digit}, - /// bytes::complete::{tag, take_while1} + /// sequence::terminated, + /// character::complete::{char, one_of}, + /// bytes::complete::{tag, take_while1}, /// }; /// /// fn decimal(input: LocatedSpan<&str>) -> IResult, LocatedSpan<&str>> { /// recognize( - /// take_while1(|c| is_digit(c as u8) || c == '_') - /// )(input) + /// take_while1(|c: char| c.is_dec_digit() || c == '_') + /// ).parse(input) /// } /// /// fn main() { - /// let span = LocatedSpan::new("$10"); + /// use nom::Parser; + /// let span = LocatedSpan::new("$10"); /// // matches the $ and then matches the decimal number afterwards, /// // converting it into a `u8` and putting that value in the span - /// let (_, (_, n)) = tuple(( - /// tag("$"), - /// map_res( - /// decimal, - /// |x| x.fragment().parse::().map(|n| x.map_extra(|_| n)) - /// ) - /// ))(span).unwrap(); + /// let (_, (_, n)) = ( + /// tag("$"), + /// map_res( + /// decimal, + /// |x| x.fragment().parse::().map(|n| x.map_extra(|_| n)) + /// ) + /// ).parse(span).unwrap(); /// assert_eq!(n.extra, 10); /// } /// ``` @@ -401,7 +392,7 @@ impl LocatedSpan { /// # extern crate nom_locate; /// # extern crate nom; /// # use nom_locate::LocatedSpan; - /// # use nom::{Slice, FindSubstring}; + /// # use nom::{Input, FindSubstring}; /// # /// # fn main() { /// let program = LocatedSpan::new( @@ -411,7 +402,7 @@ impl LocatedSpan { /// let multi = program.find_substring("multi").unwrap(); /// /// assert_eq!( - /// program.slice(multi..).get_line_beginning(), + /// program.take_from(multi).get_line_beginning(), /// "This is a multi-line input".as_bytes(), /// ); /// # } @@ -435,12 +426,13 @@ impl LocatedSpan { /// # extern crate nom_locate; /// # extern crate nom; /// # use nom_locate::LocatedSpan; - /// # use nom::Slice; + /// # use nom::Input; /// # /// # fn main() { + /// use nom::Input; /// let span = LocatedSpan::new("foobar"); /// - /// assert_eq!(span.slice(3..).get_column(), 4); + /// assert_eq!(span.take_from(3).get_column(), 4); /// # } /// ``` pub fn get_column(&self) -> usize { @@ -461,14 +453,15 @@ impl LocatedSpan { /// # extern crate nom_locate; /// # extern crate nom; /// # use nom_locate::LocatedSpan; - /// # use nom::{Slice, FindSubstring}; + /// # use nom::{Input, FindSubstring}; /// # /// # fn main() { + /// use nom::Input; /// let span = LocatedSpan::new("メカジキ"); /// let indexOf3dKanji = span.find_substring("ジ").unwrap(); /// - /// assert_eq!(span.slice(indexOf3dKanji..).get_column(), 7); - /// assert_eq!(span.slice(indexOf3dKanji..).get_utf8_column(), 3); + /// assert_eq!(span.take_from(indexOf3dKanji).get_column(), 7); + /// assert_eq!(span.take_from(indexOf3dKanji).get_utf8_column(), 3); /// # } /// ``` pub fn get_utf8_column(&self) -> usize { @@ -488,20 +481,53 @@ impl LocatedSpan { /// # extern crate nom_locate; /// # extern crate nom; /// # use nom_locate::LocatedSpan; - /// # use nom::{Slice, FindSubstring}; + /// # use nom::{Input, FindSubstring}; /// # /// # fn main() { /// let span = LocatedSpan::new("メカジキ"); /// let indexOf3dKanji = span.find_substring("ジ").unwrap(); /// - /// assert_eq!(span.slice(indexOf3dKanji..).get_column(), 7); - /// assert_eq!(span.slice(indexOf3dKanji..).naive_get_utf8_column(), 3); + /// assert_eq!(span.take_from(indexOf3dKanji).get_column(), 7); + /// assert_eq!(span.take_from(indexOf3dKanji).naive_get_utf8_column(), 3); /// # } /// ``` pub fn naive_get_utf8_column(&self) -> usize { let before_self = self.get_columns_and_bytes_before().1; naive_num_chars(before_self) + 1 } + + // Helper for `Input::take()` and `Input::take_from()` implementations. + fn slice_by(&self, next_fragment: T) -> Self + where + T: AsBytes + Input + Offset, + X: Clone, + { + let consumed_len = self.fragment.offset(&next_fragment); + if consumed_len == 0 { + return Self { + line: self.line, + offset: self.offset, + fragment: next_fragment, + extra: self.extra.clone(), + }; + } + + let consumed = self.fragment.take(consumed_len); + + let next_offset = self.offset + consumed_len; + + let consumed_as_bytes = consumed.as_bytes(); + let iter = Memchr::new(b'\n', consumed_as_bytes); + let number_of_lines = iter.count() as u32; + let next_line = self.line + number_of_lines; + + Self { + line: next_line, + offset: next_offset, + fragment: next_fragment, + extra: self.extra.clone(), + } + } } impl Hash for LocatedSpan { @@ -532,89 +558,56 @@ impl AsBytes for LocatedSpan { } } -impl InputLength for LocatedSpan { +impl Input for LocatedSpan +where + T: AsBytes + Input + Offset, + X: Clone, +{ + type Item = ::Item; + type Iter = ::Iter; + type IterIndices = ::IterIndices; + + #[inline] fn input_len(&self) -> usize { self.fragment.input_len() } -} -impl InputTake for LocatedSpan -where - Self: Slice> + Slice>, -{ - fn take(&self, count: usize) -> Self { - self.slice(..count) + #[inline] + fn take(&self, index: usize) -> Self { + self.slice_by(self.fragment.take(index)) } - fn take_split(&self, count: usize) -> (Self, Self) { - (self.slice(count..), self.slice(..count)) + #[inline] + fn take_from(&self, index: usize) -> Self { + self.slice_by(self.fragment.take_from(index)) } -} -impl InputTakeAtPosition for LocatedSpan -where - T: InputTakeAtPosition + InputLength + InputIter, - Self: Slice> + Slice> + Clone, -{ - type Item = ::Item; + #[inline] + fn take_split(&self, index: usize) -> (Self, Self) { + (self.take_from(index), self.take(index)) + } - fn split_at_position_complete>( - &self, - predicate: P, - ) -> IResult + #[inline] + fn position

(&self, predicate: P) -> Option where P: Fn(Self::Item) -> bool, { - match self.split_at_position(predicate) { - Err(Err::Incomplete(_)) => Ok(self.take_split(self.input_len())), - res => res, - } + self.fragment.position(predicate) } - fn split_at_position>(&self, predicate: P) -> IResult - where - P: Fn(Self::Item) -> bool, - { - match self.fragment.position(predicate) { - Some(n) => Ok(self.take_split(n)), - None => Err(Err::Incomplete(nom::Needed::new(1))), - } + #[inline] + fn iter_elements(&self) -> Self::Iter { + self.fragment.iter_elements() } - fn split_at_position1>( - &self, - predicate: P, - e: ErrorKind, - ) -> IResult - where - P: Fn(Self::Item) -> bool, - { - match self.fragment.position(predicate) { - Some(0) => Err(Err::Error(E::from_error_kind(self.clone(), e))), - Some(n) => Ok(self.take_split(n)), - None => Err(Err::Incomplete(nom::Needed::new(1))), - } + #[inline] + fn iter_indices(&self) -> Self::IterIndices { + self.fragment.iter_indices() } - fn split_at_position1_complete>( - &self, - predicate: P, - e: ErrorKind, - ) -> IResult - where - P: Fn(Self::Item) -> bool, - { - match self.fragment.position(predicate) { - Some(0) => Err(Err::Error(E::from_error_kind(self.clone(), e))), - Some(n) => Ok(self.take_split(n)), - None => { - if self.fragment.input_len() == 0 { - Err(Err::Error(E::from_error_kind(self.clone(), e))) - } else { - Ok(self.take_split(self.input_len())) - } - } - } + #[inline] + fn slice_index(&self, count: usize) -> Result { + self.fragment.slice_index(count) } } @@ -627,34 +620,6 @@ macro_rules! impl_input_iter { () => {}; } -impl<'a, T, X> InputIter for LocatedSpan -where - T: InputIter, -{ - type Item = T::Item; - type Iter = T::Iter; - type IterElem = T::IterElem; - #[inline] - fn iter_indices(&self) -> Self::Iter { - self.fragment.iter_indices() - } - #[inline] - fn iter_elements(&self) -> Self::IterElem { - self.fragment.iter_elements() - } - #[inline] - fn position

(&self, predicate: P) -> Option - where - P: Fn(Self::Item) -> bool, - { - self.fragment.position(predicate) - } - #[inline] - fn slice_index(&self, count: usize) -> Result { - self.fragment.slice_index(count) - } -} - impl, B: Into>, X> Compare for LocatedSpan { #[inline(always)] fn compare(&self, t: B) -> CompareResult { @@ -694,40 +659,6 @@ macro_rules! impl_slice_ranges { ( $fragment_type:ty ) => {}; } -impl<'a, T, R, X: Clone> Slice for LocatedSpan -where - T: Slice + Offset + AsBytes + Slice>, -{ - fn slice(&self, range: R) -> Self { - let next_fragment = self.fragment.slice(range); - let consumed_len = self.fragment.offset(&next_fragment); - if consumed_len == 0 { - return LocatedSpan { - line: self.line, - offset: self.offset, - fragment: next_fragment, - extra: self.extra.clone(), - }; - } - - let consumed = self.fragment.slice(..consumed_len); - - let next_offset = self.offset + consumed_len; - - let consumed_as_bytes = consumed.as_bytes(); - let iter = Memchr::new(b'\n', consumed_as_bytes); - let number_of_lines = iter.count() as u32; - let next_line = self.line + number_of_lines; - - LocatedSpan { - line: next_line, - offset: next_offset, - fragment: next_fragment, - extra: self.extra.clone(), - } - } -} - impl, Token, X> FindToken for LocatedSpan { fn find_token(&self, token: Token) -> bool { self.fragment.find_token(token) @@ -835,7 +766,7 @@ macro_rules! position { pub fn position(s: T) -> IResult where E: ParseError, - T: InputIter + InputTake, + T: Input, { nom::bytes::complete::take(0usize)(s) } diff --git a/src/tests.rs b/src/tests.rs index 3718c6c..2c0031d 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -17,10 +17,7 @@ use lib::std::*; use super::LocatedSpan; #[cfg(feature = "alloc")] use nom::ParseTo; -use nom::{ - error::ErrorKind, Compare, CompareResult, FindSubstring, FindToken, InputIter, InputTake, - InputTakeAtPosition, Offset, Slice, -}; +use nom::{error::ErrorKind, Compare, CompareResult, FindSubstring, FindToken, Input, Offset}; type StrSpan<'a> = LocatedSpan<&'a str>; type BytesSpan<'a> = LocatedSpan<&'a [u8]>; @@ -112,7 +109,7 @@ fn it_should_ignore_extra_for_hash() { fn it_should_slice_for_str() { let str_slice = StrSpanEx::new_extra("foobar", "extra"); assert_eq!( - str_slice.slice(1..), + str_slice.take_from(1), StrSpanEx { offset: 1, line: 1, @@ -121,7 +118,7 @@ fn it_should_slice_for_str() { } ); assert_eq!( - str_slice.slice(1..3), + str_slice.take(3).take_from(1), StrSpanEx { offset: 1, line: 1, @@ -130,7 +127,7 @@ fn it_should_slice_for_str() { } ); assert_eq!( - str_slice.slice(..3), + str_slice.take(3), StrSpanEx { offset: 0, line: 1, @@ -138,14 +135,14 @@ fn it_should_slice_for_str() { extra: "extra", } ); - assert_eq!(str_slice.slice(..), str_slice); + assert_eq!(str_slice.take_from(0), str_slice); } #[test] fn it_should_slice_for_u8() { let bytes_slice = BytesSpanEx::new_extra(b"foobar", "extra"); assert_eq!( - bytes_slice.slice(1..), + bytes_slice.take_from(1), BytesSpanEx { offset: 1, line: 1, @@ -154,7 +151,7 @@ fn it_should_slice_for_u8() { } ); assert_eq!( - bytes_slice.slice(1..3), + bytes_slice.take(3).take_from(1), BytesSpanEx { offset: 1, line: 1, @@ -163,7 +160,7 @@ fn it_should_slice_for_u8() { } ); assert_eq!( - bytes_slice.slice(..3), + bytes_slice.take(3), BytesSpanEx { offset: 0, line: 1, @@ -171,7 +168,7 @@ fn it_should_slice_for_u8() { extra: "extra", } ); - assert_eq!(bytes_slice.slice(..), bytes_slice); + assert_eq!(bytes_slice.take_from(0), bytes_slice); } #[test] @@ -182,13 +179,13 @@ fn it_should_calculate_columns() { ); let bar_idx = input.find_substring("bar").unwrap(); - assert_eq!(input.slice(bar_idx..).get_column(), 9); + assert_eq!(input.take_from(bar_idx).get_column(), 9); } #[test] fn it_should_calculate_columns_accurately_with_non_ascii_chars() { let s = StrSpan::new("メカジキ"); - assert_eq!(s.slice(6..).get_utf8_column(), 3); + assert_eq!(s.take_from(6).get_utf8_column(), 3); } #[test] @@ -321,10 +318,10 @@ fn it_should_calculate_offset_for_u8() { #[test] fn it_should_calculate_offset_for_str() { let s = StrSpan::new("abcřèÂßÇd123"); - let a = s.slice(..); - let b = a.slice(7..); - let c = a.slice(..5); - let d = a.slice(5..9); + let a = s.take_from(0); + let b = a.take_from(7); + let c = a.take(5); + let d = a.take(9).take_from(5); assert_eq!(a.offset(&b), 7); assert_eq!(a.offset(&c), 0); assert_eq!(a.offset(&d), 5); @@ -459,7 +456,9 @@ fn line_of_single_line_start_is_whole() { fn line_of_single_line_end_is_whole() { let data = "A single line"; assert_eq!( - StrSpan::new(data).slice(data.len()..).get_line_beginning(), + StrSpan::new(data) + .take_from(data.len()) + .get_line_beginning(), "A single line".as_bytes(), ); } @@ -484,7 +483,7 @@ fn line_of_nl_is_before() { \nand a third\n"; assert_eq!( StrSpan::new(data) - .slice(data.find('\n').unwrap()..) + .take_from(data.find('\n').unwrap()) .get_line_beginning(), "One line of text".as_bytes(), ); @@ -496,7 +495,9 @@ fn line_of_end_after_nl_is_empty() { \nFollowed by a second\ \nand a third\n"; assert_eq!( - StrSpan::new(data).slice(data.len()..).get_line_beginning(), + StrSpan::new(data) + .take_from(data.len()) + .get_line_beginning(), "".as_bytes(), ); } @@ -507,7 +508,9 @@ fn line_of_end_no_nl_is_last() { \nFollowed by a second\ \nand a third"; assert_eq!( - StrSpan::new(data).slice(data.len()..).get_line_beginning(), + StrSpan::new(data) + .take_from(data.len()) + .get_line_beginning(), "and a third".as_bytes(), ); } @@ -523,7 +526,8 @@ fn line_begining_may_ot_be_entire_len() { let pos = data.find_substring(by).unwrap(); assert_eq!( StrSpan::new(data) - .slice(pos..pos + by.len()) + .take(pos + by.len()) + .take_from(pos) .get_line_beginning(), "Followed by".as_bytes(), ); @@ -537,7 +541,7 @@ fn line_for_non_ascii_chars() { \nFörra raden var först, den här är i mitten\ \noch här är sista raden.\n", ); - let s = data.slice(data.find_substring("först").unwrap()..); + let s = data.take_from(data.find_substring("först").unwrap()); assert_eq!( format!( "{line_no:3}: {line_text}\n {0:>lpos$}^- The match\n", diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 74dd0db..8d8e473 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1,8 +1,7 @@ -use nom::{error::ErrorKind, error_position, AsBytes, FindSubstring, IResult, InputLength, Slice}; +use nom::{error::ErrorKind, error_position, AsBytes, FindSubstring, IResult, Input, Parser}; use nom_locate::LocatedSpan; use std::cmp; use std::fmt::Debug; -use std::ops::{Range, RangeFull}; #[cfg(feature = "alloc")] use nom::bytes::complete::escaped_transform; @@ -21,9 +20,9 @@ type BytesSpan<'a> = LocatedSpan<&'a [u8]>; #[cfg(any(feature = "std", feature = "alloc"))] fn simple_parser_str(i: StrSpan) -> IResult> { - let (i, foo) = delimited(multispace0, tag("foo"), multispace0)(i)?; - let (i, bar) = delimited(multispace0, tag("bar"), multispace0)(i)?; - let (i, baz) = many0(delimited(multispace0, tag("baz"), multispace0))(i)?; + let (i, foo) = delimited(multispace0, tag("foo"), multispace0).parse(i)?; + let (i, bar) = delimited(multispace0, tag("bar"), multispace0).parse(i)?; + let (i, baz) = many0(delimited(multispace0, tag("baz"), multispace0)).parse(i)?; let (i, eof) = eof(i)?; Ok({ @@ -36,9 +35,9 @@ fn simple_parser_str(i: StrSpan) -> IResult> { #[cfg(any(feature = "std", feature = "alloc"))] fn simple_parser_u8(i: BytesSpan) -> IResult> { - let (i, foo) = delimited(multispace0, tag("foo"), multispace0)(i)?; - let (i, bar) = delimited(multispace0, tag("bar"), multispace0)(i)?; - let (i, baz) = many0(delimited(multispace0, tag("baz"), multispace0))(i)?; + let (i, foo) = delimited(multispace0, tag("foo"), multispace0).parse(i)?; + let (i, bar) = delimited(multispace0, tag("bar"), multispace0).parse(i)?; + let (i, baz) = many0(delimited(multispace0, tag("baz"), multispace0)).parse(i)?; let (i, eof) = eof(i)?; Ok({ @@ -59,9 +58,9 @@ struct Position { fn test_str_fragments<'a, F, T>(parser: F, input: T, positions: Vec) where F: Fn(LocatedSpan) -> IResult, Vec>>, - T: InputLength + Slice> + Slice + Debug + PartialEq + AsBytes, + T: Input + Debug + PartialEq + AsBytes, { - let res = parser(LocatedSpan::new(input.slice(..))) + let res = parser(LocatedSpan::new(input.take_from(0))) .map_err(|err| { eprintln!( "for={:?} -- The parser should run successfully\n{:?}", @@ -83,7 +82,9 @@ where assert_eq!(output_item.location_line(), pos.line); assert_eq!( output_item.fragment(), - &input.slice(pos.offset..cmp::min(pos.offset + pos.fragment_len, input.input_len())) + &input + .take(cmp::min(pos.offset + pos.fragment_len, input.input_len())) + .take_from(pos.offset) ); assert_eq!( output_item.get_utf8_column(), @@ -235,8 +236,8 @@ fn find_substring<'a>( match input.find_substring(substr) { None => Err(nom::Err::Error(error_position!(input, ErrorKind::Tag))), Some(pos) => Ok(( - input.slice(pos + substr_len..), - input.slice(pos..pos + substr_len), + input.take_from(pos + substr_len), + input.take(pos + substr_len).take_from(pos), )), } } @@ -255,7 +256,8 @@ fn test_escaped_string() { nom::character::complete::anychar, ), char('"'), - )(i) + ) + .parse(i) } let res = string(LocatedSpan::new("\"foo\\\"bar\"")); @@ -270,9 +272,9 @@ fn test_escaped_string() { #[cfg(any(feature = "std", feature = "alloc"))] fn plague(i: StrSpan) -> IResult> { let (i, ojczyzno) = find_substring(i, "Ojczyzno")?; - let (i, jak) = many0(|i| find_substring(i, "jak "))(i)?; + let (i, jak) = many0(|i| find_substring(i, "jak ")).parse(i)?; let (i, zielona) = find_substring(i, "Zielona")?; - let (i, _) = preceded(take_until("."), tag("."))(i)?; + let (i, _) = preceded(take_until("."), tag(".")).parse(i)?; Ok({ let mut res = vec![ojczyzno]; @@ -345,8 +347,8 @@ Zielona, na niej zrzadka ciche grusze siedza."; #[test] fn test_take_until_str() { fn parser(i: StrSpan) -> IResult { - let (i, _) = delimited(take_until("foo"), tag("foo"), multispace0)(i)?; - let (i, _) = delimited(take_until("bar"), tag("bar"), multispace0)(i)?; + let (i, _) = delimited(take_until("foo"), tag("foo"), multispace0).parse(i)?; + let (i, _) = delimited(take_until("bar"), tag("bar"), multispace0).parse(i)?; let (i, _) = eof(i)?; Ok((i, ())) } @@ -363,8 +365,8 @@ fn test_take_until_str() { fn test_take_until_u8() { fn parser(i: BytesSpan) -> IResult { // Mix string and byte conditions. - let (i, _) = delimited(take_until("foo"), tag("foo"), multispace0)(i)?; - let (i, _) = delimited(take_until(&b"bar"[..]), tag(&b"bar"[..]), multispace0)(i)?; + let (i, _) = delimited(take_until("foo"), tag("foo"), multispace0).parse(i)?; + let (i, _) = delimited(take_until(&b"bar"[..]), tag(&b"bar"[..]), multispace0).parse(i)?; let (i, _) = eof(i)?; Ok((i, ())) }