Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
J-F-Liu committed Feb 15, 2017
1 parent a9f4356 commit 7ae618d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ cargo run --example json

| Parser | Time to parse the same JSON file |
|------------------|----------------------------------|
| pom: json_byte | 761,259 ns/iter (+/- 26,461) |
| pom: json_char | 676,080 ns/iter (+/- 36,376) |
| pom: json_byte | 621,319 ns/iter (+/- 20,318) |
| pom: json_char | 627,110 ns/iter (+/- 11,463) |
| [pest](https://github.com/dragostis/pest): json_char | 13,359 ns/iter (+/- 811) |

## Releases
Expand Down
8 changes: 4 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ pub fn list<'a, I, O, U>(parser: Parser<'a, I, O>, separator: Parser<'a, I, U>)
/// Success when current input symbol is one of the set.
pub fn one_of<'a, I, S>(set: &'static S) -> Parser<'a, I, I>
where I: Copy + PartialEq + Display + Debug + 'static,
S: Set<I> + Debug + ?Sized
S: Set<I> + ?Sized
{
Parser::new(move |input: &mut Input<I>| {
if let Some(s) = input.current() {
Expand All @@ -270,7 +270,7 @@ pub fn one_of<'a, I, S>(set: &'static S) -> Parser<'a, I, I>
Ok(s)
} else {
Err(Error::Mismatch {
message: format!("expect one of: {:?}, found: {}", set, s),
message: format!("expect one of: {}, found: {}", set.to_str(), s),
position: input.position(),
})
}
Expand All @@ -283,13 +283,13 @@ pub fn one_of<'a, I, S>(set: &'static S) -> Parser<'a, I, I>
/// Success when current input symbol is none of the set.
pub fn none_of<'a, I, S>(set: &'static S) -> Parser<'a, I, I>
where I: Copy + PartialEq + Display + Debug + 'static,
S: Set<I> + Debug + ?Sized
S: Set<I> + ?Sized
{
Parser::new(move |input: &mut Input<I>| {
if let Some(s) = input.current() {
if set.contains(&s) {
Err(Error::Mismatch {
message: format!("expect none of: {:?}, found: {}", set, s),
message: format!("expect none of: {}, found: {}", set.to_str(), s),
position: input.position(),
})
} else {
Expand Down
20 changes: 19 additions & 1 deletion src/set.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::cmp::{PartialEq, PartialOrd};
use std::ops::{Range, RangeFrom, RangeTo, RangeFull};
use std::str;

/// Set relationship.
pub trait Set<T> {
/// Whether a set contains an element or not.
fn contains(&self, elem: &T) -> bool;

/// Convert to text for display.
fn to_str(&self) -> &str {
"<set>"
}
}

impl<T: PartialEq> Set<T> for [T] {
Expand All @@ -17,6 +23,10 @@ impl Set<char> for str {
fn contains(&self, elem: &char) -> bool {
(self as &str).contains(*elem)
}

fn to_str(&self) -> &str {
self
}
}

impl<T: PartialOrd + Copy> Set<T> for Range<T> {
Expand All @@ -41,14 +51,22 @@ impl<T> Set<T> for RangeFull {
fn contains(&self, _: &T) -> bool {
true
}

fn to_str(&self) -> &str {
".."
}
}

macro_rules! impl_set_for_array
{
($n:expr) => {
impl Set<u8> for [u8; $n] {
fn contains(&self, elem: &u8) -> bool {
(&self[..]).contains(elem)
(self as &[u8]).contains(elem)
}

fn to_str(&self) -> &str {
str::from_utf8(self).unwrap_or("<byte array>")
}
}
};
Expand Down

0 comments on commit 7ae618d

Please sign in to comment.