refactor: cleanup utils macros

This commit is contained in:
PoiScript 2019-02-07 15:54:16 +08:00
parent 346ebc83d7
commit 0b355b498c
18 changed files with 285 additions and 297 deletions

View file

@ -1,3 +1,5 @@
use memchr::{memchr, memchr2};
#[cfg_attr(test, derive(PartialEq))]
#[derive(Debug)]
pub struct InlineCall<'a> {
@ -11,17 +13,21 @@ pub struct InlineCall<'a> {
impl<'a> InlineCall<'a> {
pub fn parse(src: &'a str) -> Option<(InlineCall, usize)> {
starts_with!(src, "call_");
debug_assert!(src.starts_with("call_"));
let mut pos = until_while!(src, 5, |c| c == b'[' || c == b'(', |c: u8| c
.is_ascii_graphic())?;
let bytes = src.as_bytes();
let mut pos = memchr2(b'[', b'(', bytes)
.filter(|&i| bytes[5..i].iter().all(|c| c.is_ascii_graphic()))?;
let mut pos_;
let name = &src[5..pos];
let inside_header = if src.as_bytes()[pos] == b'[' {
let inside_header = if bytes[pos] == b'[' {
pos_ = pos;
pos = until_while!(src, pos, b']', |c: u8| c != b'\n')? + 1;
pos = memchr(b']', &bytes[pos..])
.map(|i| i + pos)
.filter(|&i| bytes[pos..i].iter().all(|&c| c != b'\n'))?
+ 1;
expect!(src, pos, b'(')?;
Some(&src[pos_ + 1..pos - 1])
} else {
@ -29,13 +35,16 @@ impl<'a> InlineCall<'a> {
};
pos_ = pos;
pos = until_while!(src, pos, b')', |c| c != b'\n')?;
pos = memchr(b')', &bytes[pos..])
.map(|i| i + pos)
.filter(|&i| bytes[pos..i].iter().all(|&c| c != b'\n'))?;
let args = &src[pos_ + 1..pos];
let end_header = if src.len() > pos + 1 && src.as_bytes()[pos + 1] == b'[' {
pos_ = pos;
pos = until_while!(src, pos_ + 1, |c| c == b']', |c: u8| c != b'\n'
&& c != b')')?;
pos = memchr(b']', &bytes[pos_ + 1..])
.map(|i| i + pos_ + 1)
.filter(|&i| bytes[pos_ + 1..i].iter().all(|&c| c != b'\n' && c != b')'))?;
Some(&src[pos_ + 2..pos])
} else {
None