Skip to content

Commit

Permalink
Merge pull request #277 from epage/peek
Browse files Browse the repository at this point in the history
refactor(test): Rely on trait, rather than direct calls
  • Loading branch information
epage authored Jul 6, 2023
2 parents 5e146d7 + 636b095 commit d109f82
Show file tree
Hide file tree
Showing 14 changed files with 468 additions and 380 deletions.
10 changes: 5 additions & 5 deletions benches/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use winnow::stream::ParseSlice;
type Stream<'i> = &'i [u8];

fn parser(i: Stream<'_>) -> IResult<Stream<'_>, u64> {
be_u64(i)
be_u64.parse_peek(i)
}

fn number(c: &mut Criterion) {
Expand All @@ -29,20 +29,20 @@ fn number(c: &mut Criterion) {
fn float_bytes(c: &mut Criterion) {
println!(
"float_bytes result: {:?}",
float::<_, f64, Error<_>>(&b"-1.234E-12"[..])
float::<_, f64, Error<_>>.parse_peek(&b"-1.234E-12"[..])
);
c.bench_function("float bytes", |b| {
b.iter(|| float::<_, f64, Error<_>>(&b"-1.234E-12"[..]));
b.iter(|| float::<_, f64, Error<_>>.parse_peek(&b"-1.234E-12"[..]));
});
}

fn float_str(c: &mut Criterion) {
println!(
"float_str result: {:?}",
float::<_, f64, Error<_>>("-1.234E-12")
float::<_, f64, Error<_>>.parse_peek("-1.234E-12")
);
c.bench_function("float str", |b| {
b.iter(|| float::<_, f64, Error<_>>("-1.234E-12"));
b.iter(|| float::<_, f64, Error<_>>.parse_peek("-1.234E-12"));
});
}

Expand Down
6 changes: 3 additions & 3 deletions examples/http/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn parse(data: &[u8]) -> Option<Vec<(Request<'_>, Vec<Header<'_>>)>> {
fn request(input: Stream<'_>) -> IResult<Stream<'_>, (Request<'_>, Vec<Header<'_>>)> {
let (input, req) = request_line(input)?;
let (input, h) = repeat(1.., message_header).parse_peek(input)?;
let (input, _) = line_ending(input)?;
let (input, _) = line_ending.parse_peek(input)?;

Ok((input, (req, h)))
}
Expand All @@ -56,7 +56,7 @@ fn request_line(input: Stream<'_>) -> IResult<Stream<'_>, Request<'_>> {
let (input, uri) = take_while(1.., is_not_space).parse_peek(input)?;
let (input, _) = take_while(1.., is_space).parse_peek(input)?;
let (input, version) = http_version(input)?;
let (input, _) = line_ending(input)?;
let (input, _) = line_ending.parse_peek(input)?;

Ok((
input,
Expand All @@ -78,7 +78,7 @@ fn http_version(input: Stream<'_>) -> IResult<Stream<'_>, &[u8]> {
fn message_header_value(input: Stream<'_>) -> IResult<Stream<'_>, &[u8]> {
let (input, _) = take_while(1.., is_horizontal_space).parse_peek(input)?;
let (input, data) = take_while(1.., not_line_ending).parse_peek(input)?;
let (input, _) = line_ending(input)?;
let (input, _) = line_ending.parse_peek(input)?;

Ok((input, data))
}
Expand Down
6 changes: 3 additions & 3 deletions examples/http/parser_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn parse(data: &[u8]) -> Option<Vec<(Request<'_>, Vec<Header<'_>>)>> {
fn request(input: Stream<'_>) -> IResult<Stream<'_>, (Request<'_>, Vec<Header<'_>>)> {
let (input, req) = request_line(input)?;
let (input, h) = repeat(1.., message_header).parse_peek(input)?;
let (input, _) = line_ending(input)?;
let (input, _) = line_ending.parse_peek(input)?;

Ok((input, (req, h)))
}
Expand All @@ -58,7 +58,7 @@ fn request_line(input: Stream<'_>) -> IResult<Stream<'_>, Request<'_>> {
let (input, uri) = take_while(1.., is_not_space).parse_peek(input)?;
let (input, _) = take_while(1.., is_space).parse_peek(input)?;
let (input, version) = http_version(input)?;
let (input, _) = line_ending(input)?;
let (input, _) = line_ending.parse_peek(input)?;

Ok((
input,
Expand All @@ -80,7 +80,7 @@ fn http_version(input: Stream<'_>) -> IResult<Stream<'_>, &[u8]> {
fn message_header_value(input: Stream<'_>) -> IResult<Stream<'_>, &[u8]> {
let (input, _) = take_while(1.., is_horizontal_space).parse_peek(input)?;
let (input, data) = take_while(1.., not_line_ending).parse_peek(input)?;
let (input, _) = line_ending(input)?;
let (input, _) = line_ending.parse_peek(input)?;

Ok((input, data))
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ini/parser_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn keys_and_values(input: Stream<'_>) -> IResult<Stream<'_>, HashMap<&str, &str>
}

fn key_value(i: Stream<'_>) -> IResult<Stream<'_>, (&str, &str)> {
let (i, key) = alphanumeric(i)?;
let (i, key) = alphanumeric.parse_peek(i)?;
let (i, _) = (opt(space), "=", opt(space)).parse_peek(i)?;
let (i, val) = take_till0(is_line_ending_or_comment).parse_peek(i)?;
let (i, _) = opt(space).parse_peek(i)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/json_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'a, 'b: 'a> JsonValue<'a, 'b> {

pub fn number(&self) -> Option<f64> {
println!("number()");
match float::<_, _, ()>(self.data()) {
match float::<_, _, ()>.parse_peek(self.data()) {
Ok((i, o)) => {
self.offset(i);
println!("-> {}", o);
Expand Down
Loading

0 comments on commit d109f82

Please sign in to comment.