perf: replace all str.find(..)s with memchr(..)
which is much faster, theoretically.
This commit is contained in:
parent
19f7bacf55
commit
da04d3d25d
7 changed files with 45 additions and 50 deletions
|
|
@ -12,7 +12,9 @@ impl Keyword {
|
|||
let key = until_while!(src, 2, b':', |c: u8| c.is_ascii_alphabetic() || c == b'_')?;
|
||||
|
||||
// includes the eol character
|
||||
let end = src.find('\n').map(|i| i + 1).unwrap_or_else(|| src.len());
|
||||
let end = memchr::memchr(b'\n', src.as_bytes())
|
||||
.map(|i| i + 1)
|
||||
.unwrap_or_else(|| src.len());
|
||||
|
||||
Some((&src[2..key], &src[key + 1..end].trim(), end))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,7 +51,9 @@ impl List {
|
|||
|
||||
// returns (contents_begin, contents_end)
|
||||
pub fn parse_item(src: &str, ident: usize) -> (usize, usize) {
|
||||
let beg = src[ident..].find(' ').map(|i| ident + i + 1).unwrap();
|
||||
let beg = memchr::memchr(b' ', &src.as_bytes()[ident..])
|
||||
.map(|i| i + ident + 1)
|
||||
.unwrap();
|
||||
let mut lines = lines!(src);
|
||||
// skip first line
|
||||
let mut pos = lines.next().unwrap();
|
||||
|
|
|
|||
|
|
@ -179,12 +179,12 @@ impl<'a> Element<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: multiple lines fixed width area
|
||||
if bytes[pos] == b':' && bytes.get(pos + 1).map(|&b| b == b' ').unwrap_or(false) {
|
||||
let eol = src[pos..]
|
||||
.find('\n')
|
||||
.map(|i| i + pos + 1)
|
||||
.unwrap_or_else(|| src.len());
|
||||
ret!(Element::FixedWidth(&src[pos + 1..eol]), eol);
|
||||
let eol = memchr::memchr(b'\n', &src.as_bytes()[pos..])
|
||||
.map(|i| i + 1)
|
||||
.unwrap_or_else(|| src.len() - pos);
|
||||
ret!(Element::FixedWidth(&src[pos + 1..pos + eol]), eol);
|
||||
}
|
||||
|
||||
if bytes[pos] == b'#' && bytes.get(pos + 1).filter(|&&b| b == b'+').is_some() {
|
||||
|
|
@ -246,12 +246,12 @@ impl<'a> Element<'a> {
|
|||
}
|
||||
|
||||
// Comment
|
||||
// TODO: multiple lines comment
|
||||
if bytes[pos] == b'#' && bytes.get(pos + 1).map(|&b| b == b' ').unwrap_or(false) {
|
||||
let eol = src[pos..]
|
||||
.find('\n')
|
||||
.map(|i| i + pos + 1)
|
||||
.unwrap_or_else(|| src.len());
|
||||
ret!(Element::Comment(&src[pos + 1..eol]), eol);
|
||||
let eol = memchr::memchr(b'\n', &src.as_bytes()[pos..])
|
||||
.map(|i| i + 1)
|
||||
.unwrap_or_else(|| src.len() - pos);
|
||||
ret!(Element::Comment(&src[pos + 1..pos + eol]), eol);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
use memchr::memchr;
|
||||
|
||||
#[cfg_attr(test, derive(PartialEq))]
|
||||
#[derive(Debug)]
|
||||
pub struct Rule;
|
||||
|
||||
impl Rule {
|
||||
pub fn parse(src: &str) -> usize {
|
||||
let end = memchr(b'\n', src.as_bytes())
|
||||
let end = memchr::memchr(b'\n', src.as_bytes())
|
||||
.map(|i| i + 1)
|
||||
.unwrap_or_else(|| src.len());
|
||||
let rules = &src[0..end].trim();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue