Skip to content

Commit

Permalink
Merge pull request #129 from epage/int
Browse files Browse the repository at this point in the history
fix(char)!: Generalize textual number parsing
  • Loading branch information
epage committed Feb 6, 2023
2 parents 77623fd + 152e683 commit b21c772
Show file tree
Hide file tree
Showing 14 changed files with 614 additions and 523 deletions.
55 changes: 12 additions & 43 deletions benches/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate criterion;

use criterion::Criterion;

use winnow::character::{f64, recognize_float};
use winnow::character::float;
use winnow::error::ErrMode;
use winnow::error::Error;
use winnow::error::ErrorKind;
Expand All @@ -26,56 +26,33 @@ fn number(c: &mut Criterion) {
});
}

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

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

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

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

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

Expand All @@ -89,13 +66,5 @@ fn std_float_bytes(c: &mut Criterion) {
});
}

criterion_group!(
benches,
number,
recognize_float_bytes,
recognize_float_str,
float_bytes,
std_float_bytes,
float_str
);
criterion_group!(benches, number, float_bytes, std_float_bytes, float_str);
criterion_main!(benches);
4 changes: 2 additions & 2 deletions examples/json/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use winnow::prelude::*;
use winnow::{
branch::alt,
bytes::{any, none_of, tag, take, take_while},
character::f64,
character::float,
combinator::cut_err,
error::{ContextError, ParseError},
multi::{fold_many0, separated_list0},
Expand Down Expand Up @@ -45,7 +45,7 @@ fn json_value<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static st
null.value(JsonValue::Null),
boolean.map(JsonValue::Boolean),
string.map(JsonValue::Str),
f64.map(JsonValue::Num),
float.map(JsonValue::Num),
array.map(JsonValue::Array),
object.map(JsonValue::Object),
))(input)
Expand Down
8 changes: 4 additions & 4 deletions examples/json/parser_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use winnow::prelude::*;
use winnow::{
branch::{alt, dispatch},
bytes::{any, none_of, tag, take, take_while},
character::f64,
character::float,
combinator::cut_err,
combinator::fail,
combinator::peek,
Expand Down Expand Up @@ -49,9 +49,9 @@ fn json_value<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static st
't' => true_.map(JsonValue::Boolean),
'f' => false_.map(JsonValue::Boolean),
'"' => string.map(JsonValue::Str),
'+' => f64.map(JsonValue::Num),
'-' => f64.map(JsonValue::Num),
'0'..='9' => f64.map(JsonValue::Num),
'+' => float.map(JsonValue::Num),
'-' => float.map(JsonValue::Num),
'0'..='9' => float.map(JsonValue::Num),
'[' => array.map(JsonValue::Array),
'{' => object.map(JsonValue::Object),
_ => fail,
Expand Down
4 changes: 2 additions & 2 deletions examples/json/parser_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use winnow::prelude::*;
use winnow::{
branch::alt,
bytes::{any, none_of, one_of, tag, take, take_while},
character::f64,
character::float,
combinator::{cut_err, rest},
error::{ContextError, ParseError},
input::Streaming,
Expand Down Expand Up @@ -54,7 +54,7 @@ fn json_value<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static st
null.value(JsonValue::Null),
boolean.map(JsonValue::Boolean),
string.map(JsonValue::Str),
f64.map(JsonValue::Num),
float.map(JsonValue::Num),
array.map(JsonValue::Array),
object.map(JsonValue::Object),
))(input)
Expand Down
6 changes: 3 additions & 3 deletions examples/json_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use winnow::{
branch::alt,
bytes::one_of,
bytes::{tag, take_while},
character::{alphanumeric1 as alphanumeric, escaped, f64},
character::{alphanumeric1 as alphanumeric, escaped, float},
combinator::cut_err,
error::ParseError,
multi::separated_list0,
Expand Down Expand Up @@ -62,7 +62,7 @@ impl<'a, 'b: 'a> JsonValue<'a, 'b> {

pub fn number(&self) -> Option<f64> {
println!("number()");
match f64::<_, (), false>(self.data()) {
match float::<_, _, (), false>(self.data()) {
Ok((i, o)) => {
self.offset(i);
println!("-> {}", o);
Expand Down Expand Up @@ -258,7 +258,7 @@ fn value(i: &str) -> IResult<&str, ()> {
hash,
array,
string.map(|_| ()),
f64.map(|_| ()),
float::<_, f64, _, false>.map(|_| ()),
boolean.map(|_| ()),
)),
)(i)
Expand Down
14 changes: 14 additions & 0 deletions src/character/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,13 @@ macro_rules! ints {
/// will parse a number in text form to a number
///
/// *Complete version*: can parse until the end of input.
///
/// **WARNING:** Deprecated, replaced with
/// [`winnow::character::dec_uint`][crate::character::dec_int]
#[deprecated(
since = "0.1.0",
note = "Replaced with `winnow::character::dec_int`"
)]
pub fn $t<T, E: ParseError<T>>(input: T) -> IResult<T, $t, E>
where
T: Input,
Expand Down Expand Up @@ -880,6 +887,13 @@ macro_rules! uints {
/// will parse a number in text form to a number
///
/// *Complete version*: can parse until the end of input.
///
/// **WARNING:** Deprecated, replaced with
/// [`winnow::character::dec_uint`][crate::character::dec_uint]
#[deprecated(
since = "0.1.0",
note = "Replaced with `winnow::character::dec_uint`"
)]
pub fn $t<T, E: ParseError<T>>(input: T) -> IResult<T, $t, E>
where
T: Input,
Expand Down

0 comments on commit b21c772

Please sign in to comment.