refactor: cleanup parser

This commit is contained in:
PoiScript 2019-04-05 22:54:50 +08:00
parent 1bb5286dd3
commit 8bb7ae41d3
6 changed files with 128 additions and 99 deletions

View file

@ -13,19 +13,19 @@ pub fn parse(src: &str) -> Option<(&str, Option<&str>, usize, usize, usize)> {
let name = memchr2(b' ', b'\n', src.as_bytes())
.filter(|&i| src.as_bytes()[8..i].iter().all(u8::is_ascii_alphabetic))?;
let mut lines = Lines::new(src);
let (pre_cont_end, cont_beg, _) = lines.next()?;
let args = if pre_cont_end == name {
let (pre_limit, begin, _) = lines.next()?;
let args = if pre_limit == name {
None
} else {
Some(&src[name..pre_cont_end])
Some(&src[name..pre_limit])
};
let name = &src[8..name];
let end_line = format!(r"#+END_{}", name.to_uppercase());
let mut pre_end = cont_beg;
let mut pre_end = begin;
for (_, end, line) in lines {
if line.trim() == end_line {
return Some((name, args, cont_beg, pre_end, end));
return Some((name, args, begin, pre_end, end));
} else {
pre_end = end;
}