Skip to content

Commit

Permalink
style: Use standard rustfmt rules (IGNORE THIS)
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 8, 2023
1 parent f34b2ca commit efe1b50
Show file tree
Hide file tree
Showing 64 changed files with 16,697 additions and 16,662 deletions.
68 changes: 34 additions & 34 deletions benches/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,56 @@ use winnow::prelude::*;
type Input<'i> = &'i [u8];

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

fn number(c: &mut Criterion) {
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

parser(&data[..]).expect("should parse correctly");
c.bench_function("number", move |b| {
b.iter(|| parser(&data[..]).unwrap());
});
parser(&data[..]).expect("should parse correctly");
c.bench_function("number", move |b| {
b.iter(|| parser(&data[..]).unwrap());
});
}

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

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

fn std_float(input: &[u8]) -> IResult<&[u8], f64, Error<&[u8]>> {
match input.parse_slice() {
Some(n) => Ok((&[], n)),
None => Err(ErrMode::Backtrack(Error {
input,
kind: ErrorKind::Float,
})),
}
match input.parse_slice() {
Some(n) => Ok((&[], n)),
None => Err(ErrMode::Backtrack(Error {
input,
kind: ErrorKind::Float,
})),
}
}

fn std_float_bytes(c: &mut Criterion) {
println!(
"std_float_bytes result: {:?}",
std_float(&b"-1.234E-12"[..])
);
c.bench_function("std_float bytes", |b| {
b.iter(|| std_float(&b"-1.234E-12"[..]));
});
println!(
"std_float_bytes result: {:?}",
std_float(&b"-1.234E-12"[..])
);
c.bench_function("std_float bytes", |b| {
b.iter(|| std_float(&b"-1.234E-12"[..]));
});
}

criterion_group!(benches, number, float_bytes, std_float_bytes, float_str);
Expand Down
16 changes: 8 additions & 8 deletions examples/arithmetic/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use parser::expr;

#[allow(clippy::eq_op, clippy::erasing_op)]
fn arithmetic(c: &mut criterion::Criterion) {
let data = " 2*2 / ( 5 - 1) + 3 / 4 * (2 - 7 + 567 *12 /2) + 3*(1+2*( 45 /2));";
let data = " 2*2 / ( 5 - 1) + 3 / 4 * (2 - 7 + 567 *12 /2) + 3*(1+2*( 45 /2));";

assert_eq!(
expr(data),
Ok((";", 2 * 2 / (5 - 1) + 3 * (1 + 2 * (45 / 2)),))
);
c.bench_function("arithmetic", |b| {
b.iter(|| expr(data).unwrap());
});
assert_eq!(
expr(data),
Ok((";", 2 * 2 / (5 - 1) + 3 * (1 + 2 * (45 / 2)),))
);
c.bench_function("arithmetic", |b| {
b.iter(|| expr(data).unwrap());
});
}

criterion::criterion_group!(benches, arithmetic);
Expand Down
52 changes: 26 additions & 26 deletions examples/arithmetic/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,43 @@ mod parser;
use parser::expr;

fn main() -> Result<(), lexopt::Error> {
let args = Args::parse()?;
let args = Args::parse()?;

let input = args.input.as_deref().unwrap_or("1 + 1");
let input = args.input.as_deref().unwrap_or("1 + 1");

println!("{} =", input);
match expr.parse_next(input).finish() {
Ok(result) => {
println!(" {}", result);
}
Err(err) => {
println!(" {}", err);
println!("{} =", input);
match expr.parse_next(input).finish() {
Ok(result) => {
println!(" {}", result);
}
Err(err) => {
println!(" {}", err);
}
}
}

Ok(())
Ok(())
}

#[derive(Default)]
struct Args {
input: Option<String>,
input: Option<String>,
}

impl Args {
fn parse() -> Result<Self, lexopt::Error> {
use lexopt::prelude::*;

let mut res = Args::default();

let mut args = lexopt::Parser::from_env();
while let Some(arg) = args.next()? {
match arg {
Value(input) => {
res.input = Some(input.string()?);
fn parse() -> Result<Self, lexopt::Error> {
use lexopt::prelude::*;

let mut res = Args::default();

let mut args = lexopt::Parser::from_env();
while let Some(arg) = args.next()? {
match arg {
Value(input) => {
res.input = Some(input.string()?);
}
_ => return Err(arg.unexpected()),
}
}
_ => return Err(arg.unexpected()),
}
Ok(res)
}
Ok(res)
}
}
106 changes: 53 additions & 53 deletions examples/arithmetic/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,97 @@ use std::str::FromStr;

use winnow::prelude::*;
use winnow::{
branch::alt,
bytes::one_of,
character::{digit1 as digits, space0 as spaces},
multi::fold_many0,
sequence::delimited,
IResult,
branch::alt,
bytes::one_of,
character::{digit1 as digits, space0 as spaces},
multi::fold_many0,
sequence::delimited,
IResult,
};

// Parser definition

pub fn expr(i: &str) -> IResult<&str, i64> {
let (i, init) = term(i)?;
let (i, init) = term(i)?;

fold_many0(
(one_of("+-"), term),
move || init,
|acc, (op, val): (char, i64)| {
if op == '+' {
acc + val
} else {
acc - val
}
},
)(i)
fold_many0(
(one_of("+-"), term),
move || init,
|acc, (op, val): (char, i64)| {
if op == '+' {
acc + val
} else {
acc - val
}
},
)(i)
}

// We read an initial factor and for each time we find
// a * or / operator followed by another factor, we do
// the math by folding everything
fn term(i: &str) -> IResult<&str, i64> {
let (i, init) = factor(i)?;
let (i, init) = factor(i)?;

fold_many0(
(one_of("*/"), factor),
move || init,
|acc, (op, val): (char, i64)| {
if op == '*' {
acc * val
} else {
acc / val
}
},
)(i)
fold_many0(
(one_of("*/"), factor),
move || init,
|acc, (op, val): (char, i64)| {
if op == '*' {
acc * val
} else {
acc / val
}
},
)(i)
}

// We transform an integer string into a i64, ignoring surrounding whitespaces
// We look for a digit suite, and try to convert it.
// If either str::from_utf8 or FromStr::from_str fail,
// we fallback to the parens parser defined above
fn factor(i: &str) -> IResult<&str, i64> {
delimited(
spaces,
alt((
digits.map_res(FromStr::from_str),
delimited(one_of('('), expr, one_of(')')),
parens,
)),
spaces,
)(i)
delimited(
spaces,
alt((
digits.map_res(FromStr::from_str),
delimited(one_of('('), expr, one_of(')')),
parens,
)),
spaces,
)(i)
}

// We parse any expr surrounded by parens, ignoring all whitespaces around those
fn parens(i: &str) -> IResult<&str, i64> {
delimited(one_of('('), expr, one_of(')'))(i)
delimited(one_of('('), expr, one_of(')'))(i)
}

#[test]
fn factor_test() {
assert_eq!(factor("3"), Ok(("", 3)));
assert_eq!(factor(" 12"), Ok(("", 12)));
assert_eq!(factor("537 "), Ok(("", 537)));
assert_eq!(factor(" 24 "), Ok(("", 24)));
assert_eq!(factor("3"), Ok(("", 3)));
assert_eq!(factor(" 12"), Ok(("", 12)));
assert_eq!(factor("537 "), Ok(("", 537)));
assert_eq!(factor(" 24 "), Ok(("", 24)));
}

#[test]
fn term_test() {
assert_eq!(term(" 12 *2 / 3"), Ok(("", 8)));
assert_eq!(term(" 2* 3 *2 *2 / 3"), Ok(("", 8)));
assert_eq!(term(" 48 / 3/2"), Ok(("", 8)));
assert_eq!(term(" 12 *2 / 3"), Ok(("", 8)));
assert_eq!(term(" 2* 3 *2 *2 / 3"), Ok(("", 8)));
assert_eq!(term(" 48 / 3/2"), Ok(("", 8)));
}

#[test]
fn expr_test() {
assert_eq!(expr(" 1 + 2 "), Ok(("", 3)));
assert_eq!(expr(" 12 + 6 - 4+ 3"), Ok(("", 17)));
assert_eq!(expr(" 1 + 2*3 + 4"), Ok(("", 11)));
assert_eq!(expr(" 1 + 2 "), Ok(("", 3)));
assert_eq!(expr(" 12 + 6 - 4+ 3"), Ok(("", 17)));
assert_eq!(expr(" 1 + 2*3 + 4"), Ok(("", 11)));
}

#[test]
fn parens_test() {
assert_eq!(expr(" ( 2 )"), Ok(("", 2)));
assert_eq!(expr(" 2* ( 3 + 4 ) "), Ok(("", 14)));
assert_eq!(expr(" 2*2 / ( 5 - 1) + 3"), Ok(("", 4)));
assert_eq!(expr(" ( 2 )"), Ok(("", 2)));
assert_eq!(expr(" 2* ( 3 + 4 ) "), Ok(("", 14)));
assert_eq!(expr(" 2*2 / ( 5 - 1) + 3"), Ok(("", 4)));
}
Loading

0 comments on commit efe1b50

Please sign in to comment.