Skip to content

Commit 7ae618d

Browse files
committed
Improve performance
1 parent a9f4356 commit 7ae618d

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ cargo run --example json
183183

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

190190
## Releases

src/parser.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ pub fn list<'a, I, O, U>(parser: Parser<'a, I, O>, separator: Parser<'a, I, U>)
261261
/// Success when current input symbol is one of the set.
262262
pub fn one_of<'a, I, S>(set: &'static S) -> Parser<'a, I, I>
263263
where I: Copy + PartialEq + Display + Debug + 'static,
264-
S: Set<I> + Debug + ?Sized
264+
S: Set<I> + ?Sized
265265
{
266266
Parser::new(move |input: &mut Input<I>| {
267267
if let Some(s) = input.current() {
@@ -270,7 +270,7 @@ pub fn one_of<'a, I, S>(set: &'static S) -> Parser<'a, I, I>
270270
Ok(s)
271271
} else {
272272
Err(Error::Mismatch {
273-
message: format!("expect one of: {:?}, found: {}", set, s),
273+
message: format!("expect one of: {}, found: {}", set.to_str(), s),
274274
position: input.position(),
275275
})
276276
}
@@ -283,13 +283,13 @@ pub fn one_of<'a, I, S>(set: &'static S) -> Parser<'a, I, I>
283283
/// Success when current input symbol is none of the set.
284284
pub fn none_of<'a, I, S>(set: &'static S) -> Parser<'a, I, I>
285285
where I: Copy + PartialEq + Display + Debug + 'static,
286-
S: Set<I> + Debug + ?Sized
286+
S: Set<I> + ?Sized
287287
{
288288
Parser::new(move |input: &mut Input<I>| {
289289
if let Some(s) = input.current() {
290290
if set.contains(&s) {
291291
Err(Error::Mismatch {
292-
message: format!("expect none of: {:?}, found: {}", set, s),
292+
message: format!("expect none of: {}, found: {}", set.to_str(), s),
293293
position: input.position(),
294294
})
295295
} else {

src/set.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
use std::cmp::{PartialEq, PartialOrd};
22
use std::ops::{Range, RangeFrom, RangeTo, RangeFull};
3+
use std::str;
34

45
/// Set relationship.
56
pub trait Set<T> {
67
/// Whether a set contains an element or not.
78
fn contains(&self, elem: &T) -> bool;
9+
10+
/// Convert to text for display.
11+
fn to_str(&self) -> &str {
12+
"<set>"
13+
}
814
}
915

1016
impl<T: PartialEq> Set<T> for [T] {
@@ -17,6 +23,10 @@ impl Set<char> for str {
1723
fn contains(&self, elem: &char) -> bool {
1824
(self as &str).contains(*elem)
1925
}
26+
27+
fn to_str(&self) -> &str {
28+
self
29+
}
2030
}
2131

2232
impl<T: PartialOrd + Copy> Set<T> for Range<T> {
@@ -41,14 +51,22 @@ impl<T> Set<T> for RangeFull {
4151
fn contains(&self, _: &T) -> bool {
4252
true
4353
}
54+
55+
fn to_str(&self) -> &str {
56+
".."
57+
}
4458
}
4559

4660
macro_rules! impl_set_for_array
4761
{
4862
($n:expr) => {
4963
impl Set<u8> for [u8; $n] {
5064
fn contains(&self, elem: &u8) -> bool {
51-
(&self[..]).contains(elem)
65+
(self as &[u8]).contains(elem)
66+
}
67+
68+
fn to_str(&self) -> &str {
69+
str::from_utf8(self).unwrap_or("<byte array>")
5270
}
5371
}
5472
};

0 commit comments

Comments
 (0)