Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Jun 10, 2023
1 parent d9f643a commit a55d6c6
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 76 deletions.
28 changes: 2 additions & 26 deletions benchmarks/benches/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,6 @@ fn is_version(c: u8) -> bool {
fn line_ending<'a>()-> impl Parser<&'a[u8], Output=&'a[u8], Error=Error<&'a[u8]>> {
tag("\n").or(tag("\r\n"))
}
/*
fn request_line<OM: OutputMode>(input: &[u8]) -> PResult<OM, &[u8], Request<'_>, Error<&[u8]>> {
let (input, method) = take_while1(is_token).process::<OM>(input)?;
let (input, uri) = preceded(take_while1(is_space), take_while1(is_not_space)).process::<OM>(input)?;
let (input, version) = delimited(take_while1(is_space), http_version(), line_ending).process::<OM>(input)?;
Ok((input, OM::Output::bind(|| Request {method, uri, version})))
(take_while1(is_token), preceded(take_while1(is_space), take_while1(is_not_space)), delimited(take_while1(is_space), http_version(), line_ending))
}*/

fn request_line<'a>()-> impl Parser<&'a[u8], Output=Request<'a>, Error=Error<&'a[u8]>> {
(take_while1(is_token), preceded(take_while1(is_space), take_while1(is_not_space)), delimited(take_while1(is_space), http_version(), line_ending()))
Expand All @@ -99,23 +87,11 @@ fn message_header_value<'a>() -> impl Parser<&'a[u8], Output=&'a[u8], Error=Erro
}

fn message_header<'a>() -> impl Parser<&'a[u8], Output=Header<'a>, Error=Error<&'a[u8]> >{
/*let (input, name) = take_while1(is_token)(input)?;
let (input, _) = char(':')(input)?;
let (input, value) = many(1.., message_header_value)(input)?;
Ok((input, Header{ name, value }))*/

separated_pair(take_while1(is_token), char(':'), many(1.., message_header_value())).map(|(name, value)|Header{ name, value })

separated_pair(take_while1(is_token), char(':'), many(1.., message_header_value()))
.map(|(name, value)|Header{ name, value })
}

fn request<'a>() -> impl Parser<&'a[u8], Output=(Request<'a>, Vec<Header<'a>>), Error=Error<&'a[u8]> > {
/*let (input, req) = request_line(input)?;
let (input, h) = many(1.., message_header)(input)?;
let (input, _) = line_ending(input)?;
Ok((input, (req, h)))*/

pair(request_line(), terminated(many(1.., message_header()), line_ending()))
}

Expand Down
8 changes: 4 additions & 4 deletions examples/json2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
//static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

use nom::{
branch::{alt, Choice},
branch::alt,
bytes::{tag, take},
character::{anychar, char, multispace0, none_of},
combinator::{map, map_opt, map_res, value, verify},
error::{Error, ErrorKind, ParseError},
error::{Error, ParseError},
multi::{fold, separated_list0},
number::double,
sequence::{delimited, preceded, separated_pair},
Complete, Emit, IResult, Mode, OutputM, Parser,
Complete, Emit, Mode, OutputM, Parser,
};

use std::collections::HashMap;
Expand Down Expand Up @@ -198,7 +198,7 @@ fn main() {
let data = include_str!("../benchmarks/canada.json");

loop {
let a = json()
let _a = json()
.process::<OutputM<Emit, Emit, Complete>>(data)
.unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion src/bits/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ where
let val: O = if offset == 0 {
byte.into()
} else {
((byte << offset) as u8 >> offset).into()
((byte << offset) >> offset).into()
};

if remaining < 8 - offset {
Expand Down
4 changes: 2 additions & 2 deletions src/bits/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
} else {
let cnt = (count + bit_offset).div(8);
if input.input_len() * 8 < count + bit_offset {
Err(Err::Incomplete(Needed::new(count as usize)))
Err(Err::Incomplete(Needed::new(count)))
} else {
let mut acc: O = 0_u8.into();
let mut offset: usize = bit_offset;
Expand All @@ -36,7 +36,7 @@ where
let val: O = if offset == 0 {
byte.into()
} else {
((byte << offset) as u8 >> offset).into()
((byte << offset) >> offset).into()
};

if remaining < 8 - offset {
Expand Down
18 changes: 8 additions & 10 deletions src/bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,17 +430,15 @@ where
1
};
Err(Err::Incomplete(Needed::new(needed)))
} else if count >= self.m {
Ok((
input.take_from(input_len),
OM::Output::bind(|| input.take(input_len)),
))
} else {
if count >= self.m {
Ok((
input.take_from(input_len),
OM::Output::bind(|| input.take(input_len)),
))
} else {
Err(Err::Error(OM::Error::bind(|| {
Error::from_error_kind(input, ErrorKind::TakeWhileMN)
})))
}
Err(Err::Error(OM::Error::bind(|| {
Error::from_error_kind(input, ErrorKind::TakeWhileMN)
})))
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,10 @@ impl Mode for Check {
type Output<T> = ();

#[inline(always)]
fn bind<T, F: FnOnce() -> T>(_: F) -> Self::Output<T> {
()
}
fn bind<T, F: FnOnce() -> T>(_: F) -> Self::Output<T> {}

#[inline(always)]
fn map<T, U, F: FnOnce(T) -> U>(_: Self::Output<T>, _: F) -> Self::Output<U> {
()
}
fn map<T, U, F: FnOnce(T) -> U>(_: Self::Output<T>, _: F) -> Self::Output<U> {}

#[inline(always)]
fn combine<T, U, V, F: FnOnce(T, U) -> V>(
Expand Down
10 changes: 5 additions & 5 deletions src/multi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ where
&mut self,
mut i: I,
) -> crate::PResult<OM, I, Self::Output, Self::Error> {
let mut res = OM::Output::bind(|| crate::lib::std::vec::Vec::new());
let mut res = OM::Output::bind(crate::lib::std::vec::Vec::new);
loop {
let len = i.input_len();
match self
Expand Down Expand Up @@ -394,7 +394,7 @@ where
&mut self,
mut i: I,
) -> crate::PResult<OM, I, Self::Output, Self::Error> {
let mut res = OM::Output::bind(|| crate::lib::std::vec::Vec::new());
let mut res = OM::Output::bind(crate::lib::std::vec::Vec::new);

match self
.parser
Expand Down Expand Up @@ -511,7 +511,7 @@ where
&mut self,
mut i: I,
) -> crate::PResult<OM, I, Self::Output, Self::Error> {
let mut res = OM::Output::bind(|| crate::lib::std::vec::Vec::new());
let mut res = OM::Output::bind(crate::lib::std::vec::Vec::new);

match self.parser.process::<OM>(i.clone()) {
Err(e) => return Err(e),
Expand Down Expand Up @@ -818,8 +818,8 @@ where
<F as Parser<I>>::Error::from_error_kind(input, ErrorKind::Many1Count)
})))
}
Err(Err::Failure(e)) => return Err(Err::Failure(e)),
Err(Err::Incomplete(i)) => return Err(Err::Incomplete(i)),
Err(Err::Failure(e)) => Err(Err::Failure(e)),
Err(Err::Incomplete(i)) => Err(Err::Incomplete(i)),
Ok((mut input, _)) => {
count += 1;

Expand Down
40 changes: 18 additions & 22 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,15 @@ impl<'a> Input for &'a [u8] {
None => {
if OM::Incomplete::is_streaming() {
Err(Err::Incomplete(Needed::new(1)))
} else if self.is_empty() {
Err(Err::Error(OM::Error::bind(|| {
E::from_error_kind(self.clone(), e)

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Bench

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Bench

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Bench

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Bench

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type

Check warning on line 344 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

using `.clone()` on a double reference, which returns `&[u8]` instead of cloning the inner type
})))
} else {
if self.len() == 0 {
Err(Err::Error(OM::Error::bind(|| {
E::from_error_kind(self.clone(), e)
})))
} else {
Ok((
self.take_from(self.len()),
OM::Output::bind(|| self.take(self.len())),
))
}
Ok((
self.take_from(self.len()),
OM::Output::bind(|| self.take(self.len())),
))
}
}
}
Expand Down Expand Up @@ -544,19 +542,17 @@ impl<'a> Input for &'a str {
None => {
if OM::Incomplete::is_streaming() {
Err(Err::Incomplete(Needed::new(1)))
} else if self.len() == 0 {
Err(Err::Error(OM::Error::bind(|| {
E::from_error_kind(self.clone(), e)

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Bench

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Bench

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Bench

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Bench

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type

Check warning on line 547 in src/traits.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type
})))
} else {
if self.len() == 0 {
Err(Err::Error(OM::Error::bind(|| {
E::from_error_kind(self.clone(), e)
})))
} else {
// the end of slice is a char boundary
unsafe {
Ok((
self.get_unchecked(self.len()..),
OM::Output::bind(|| self.get_unchecked(..self.len())),
))
}
// the end of slice is a char boundary
unsafe {
Ok((
self.get_unchecked(self.len()..),
OM::Output::bind(|| self.get_unchecked(..self.len())),
))
}
}
}
Expand Down

0 comments on commit a55d6c6

Please sign in to comment.