Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion bench/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ fn chars(c: &mut Criterion) {
}
}

fn chars_count(c: &mut Criterion) {
// benchmark our impl
for &(name, corpus) in CORPORA_HUGE {
define(c, "bstr/chars_count", name, corpus, move |b| {
b.iter(|| {
assert!(corpus.chars().count() > 0);
});
});
}
// benchmark std's impl
for &(name, corpus) in CORPORA_HUGE {
define(c, "std/chars_count", name, corpus, move |b| {
let corpus = std::str::from_utf8(corpus).unwrap();
b.iter(|| {
assert!(corpus.chars().count() > 0);
});
});
}
}

fn graphemes(c: &mut Criterion) {
// benchmark our impl
for &(name, corpus) in CORPORA_SMALL {
Expand Down Expand Up @@ -287,4 +307,7 @@ criterion_group!(g11, search::rfind_iter);
criterion_group!(g12, search::find_char);
criterion_group!(g13, search::find_byteset);
criterion_group!(g14, search::find_not_byteset);
criterion_main!(g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11, g12, g13, g14);
criterion_group!(g15, chars_count);
criterion_main!(
g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11, g12, g13, g14, g15
);
24 changes: 24 additions & 0 deletions src/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ impl<'a> Iterator for Chars<'a> {
self.bs = &self.bs[size..];
Some(ch)
}

#[inline]
fn count(mut self) -> usize {
let mut count = 0;
loop {
// ASCII fast path taken if two consecutive ASCII chars found
match self.bs {
[fst, snd, ..] if *fst <= 0x7F && *snd <= 0x7F => {
let size = ascii::first_non_ascii_byte(self.bs);
count += size;
self.bs = &self.bs[size..];
}
_ => (),
}

let (_ch, size) = decode(self.bs);
if size == 0 {
return count;
} else {
count += 1;
self.bs = &self.bs[size..];
}
}
}
}

impl<'a> DoubleEndedIterator for Chars<'a> {
Expand Down
Loading