refactor: cleanup utils macros
This commit is contained in:
parent
346ebc83d7
commit
0b355b498c
18 changed files with 285 additions and 297 deletions
|
|
@ -1,3 +1,5 @@
|
|||
use memchr::{memchr, memchr2};
|
||||
|
||||
#[cfg_attr(test, derive(PartialEq))]
|
||||
#[derive(Debug)]
|
||||
pub struct Cookie<'a> {
|
||||
|
|
@ -6,22 +8,25 @@ pub struct Cookie<'a> {
|
|||
|
||||
impl<'a> Cookie<'a> {
|
||||
pub fn parse(src: &'a str) -> Option<(Cookie<'a>, usize)> {
|
||||
if cfg!(test) {
|
||||
starts_with!(src, '[');
|
||||
}
|
||||
debug_assert!(src.starts_with("["));
|
||||
|
||||
let num1 = until_while!(src, 1, |c| c == b'%' || c == b'/', |c: u8| c
|
||||
.is_ascii_digit())?;
|
||||
let num1 = memchr2(b'%', b'/', src.as_bytes())
|
||||
.filter(|&i| src.as_bytes()[1..i].iter().all(|c| c.is_ascii_digit()))?;
|
||||
|
||||
if src.as_bytes()[num1] == b'%' && *src.as_bytes().get(num1 + 1)? == b']' {
|
||||
Some((
|
||||
Cookie {
|
||||
value: &src[0..num1 + 2],
|
||||
value: &src[0..=num1 + 1],
|
||||
},
|
||||
num1 + 2,
|
||||
))
|
||||
} else {
|
||||
let num2 = until_while!(src, num1 + 1, b']', |c: u8| c.is_ascii_digit())?;
|
||||
let num2 = memchr(b']', src.as_bytes()).filter(|&i| {
|
||||
src.as_bytes()[num1 + 1..i]
|
||||
.iter()
|
||||
.all(|c| c.is_ascii_digit())
|
||||
})?;
|
||||
|
||||
Some((
|
||||
Cookie {
|
||||
value: &src[0..=num2],
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use memchr::memchr;
|
||||
|
||||
pub struct Emphasis;
|
||||
|
||||
impl Emphasis {
|
||||
|
|
@ -5,13 +7,10 @@ impl Emphasis {
|
|||
pub fn parse(src: &str, marker: u8) -> Option<usize> {
|
||||
expect!(src, 1, |c: u8| !c.is_ascii_whitespace())?;
|
||||
|
||||
let mut lines = 0;
|
||||
let end = until_while!(src, 1, marker, |c| {
|
||||
if c == b'\n' {
|
||||
lines += 1;
|
||||
}
|
||||
lines < 2
|
||||
})?;
|
||||
let bytes = src.as_bytes();
|
||||
let end = memchr(marker, &bytes[1..])
|
||||
.map(|i| i + 1)
|
||||
.filter(|&i| bytes[1..i].iter().filter(|&&c| c == b'\n').count() < 2)?;
|
||||
|
||||
expect!(src, end - 1, |c: u8| !c.is_ascii_whitespace())?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use memchr::{memchr2, memchr2_iter};
|
||||
|
||||
#[cfg_attr(test, derive(PartialEq))]
|
||||
#[derive(Debug)]
|
||||
pub struct FnRef<'a> {
|
||||
|
|
@ -5,26 +7,32 @@ pub struct FnRef<'a> {
|
|||
definition: Option<&'a str>,
|
||||
}
|
||||
|
||||
fn valid_label(ch: u8) -> bool {
|
||||
ch.is_ascii_alphanumeric() || ch == b'-' || ch == b'_'
|
||||
fn valid_label(ch: &u8) -> bool {
|
||||
ch.is_ascii_alphanumeric() || *ch == b'-' || *ch == b'_'
|
||||
}
|
||||
|
||||
impl<'a> FnRef<'a> {
|
||||
pub fn parse(src: &'a str) -> Option<(FnRef<'a>, usize)> {
|
||||
starts_with!(src, "[fn:");
|
||||
debug_assert!(src.starts_with("[fn:"));
|
||||
|
||||
let label = until_while!(src, 4, |c| c == b']' || c == b':', valid_label)?;
|
||||
let bytes = src.as_bytes();
|
||||
let label = memchr2(b']', b':', &bytes[4..])
|
||||
.map(|i| i + 4)
|
||||
.filter(|&i| bytes[4..i].iter().all(valid_label))?;
|
||||
|
||||
if src.as_bytes()[label] == b':' {
|
||||
if bytes[label] == b':' {
|
||||
let mut pairs = 1;
|
||||
let def = until!(src[label..], |c| {
|
||||
if c == b'[' {
|
||||
pairs += 1;
|
||||
} else if c == b']' {
|
||||
pairs -= 1;
|
||||
}
|
||||
c == b']' && pairs == 0
|
||||
})? + label;
|
||||
let def = memchr2_iter(b'[', b']', &bytes[label..])
|
||||
.map(|i| i + label)
|
||||
.filter(|&i| {
|
||||
if bytes[i] == b'[' {
|
||||
pairs += 1;
|
||||
} else {
|
||||
pairs -= 1;
|
||||
}
|
||||
pairs == 0
|
||||
})
|
||||
.next()?;
|
||||
|
||||
Some((
|
||||
FnRef {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use memchr::{memchr, memchr2};
|
||||
|
||||
#[cfg_attr(test, derive(PartialEq))]
|
||||
#[derive(Debug)]
|
||||
pub struct InlineSrc<'a> {
|
||||
|
|
@ -8,18 +10,18 @@ pub struct InlineSrc<'a> {
|
|||
|
||||
impl<'a> InlineSrc<'a> {
|
||||
pub fn parse(src: &'a str) -> Option<(InlineSrc, usize)> {
|
||||
starts_with!(src, "src_");
|
||||
debug_assert!(src.starts_with("src_"));
|
||||
|
||||
let lang = until_while!(src, 4, |c| c == b'[' || c == b'{', |c: u8| !c
|
||||
.is_ascii_whitespace())?;
|
||||
let bytes = src.as_bytes();
|
||||
let lang = memchr2(b'[', b'{', bytes)
|
||||
.filter(|&i| i != 4 && bytes[4..i].iter().all(|c| !c.is_ascii_whitespace()))?;
|
||||
|
||||
if lang == 4 {
|
||||
return None;
|
||||
}
|
||||
|
||||
if src.as_bytes()[lang] == b'[' {
|
||||
let option = until_while!(src, lang, b']', |c| c != b'\n')?;
|
||||
let body = until_while!(src, option, b'}', |c| c != b'\n')?;
|
||||
if bytes[lang] == b'[' {
|
||||
let option =
|
||||
memchr(b']', bytes).filter(|&i| bytes[lang..i].iter().all(|c| *c != b'\n'))?;
|
||||
let body = memchr(b'}', &bytes[option..])
|
||||
.map(|i| i + option)
|
||||
.filter(|&i| bytes[option..i].iter().all(|c| *c != b'\n'))?;
|
||||
|
||||
Some((
|
||||
InlineSrc {
|
||||
|
|
@ -30,7 +32,8 @@ impl<'a> InlineSrc<'a> {
|
|||
body + 1,
|
||||
))
|
||||
} else {
|
||||
let body = until_while!(src, lang, b'}', |c| c != b'\n')?;
|
||||
let body =
|
||||
memchr(b'}', bytes).filter(|&i| bytes[lang..i].iter().all(|c| *c != b'\n'))?;
|
||||
|
||||
Some((
|
||||
InlineSrc {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use memchr::memchr;
|
||||
|
||||
#[cfg_attr(test, derive(PartialEq))]
|
||||
#[derive(Debug)]
|
||||
pub struct Link<'a> {
|
||||
|
|
@ -7,13 +9,16 @@ pub struct Link<'a> {
|
|||
|
||||
impl<'a> Link<'a> {
|
||||
pub fn parse(src: &'a str) -> Option<(Link<'a>, usize)> {
|
||||
if cfg!(test) {
|
||||
starts_with!(src, "[[");
|
||||
}
|
||||
debug_assert!(src.starts_with("[["));
|
||||
|
||||
let path = until_while!(src, 2, b']', |c| c != b'<' && c != b'>' && c != b'\n')?;
|
||||
let bytes = src.as_bytes();
|
||||
let path = memchr(b']', bytes).filter(|&i| {
|
||||
bytes[2..i]
|
||||
.iter()
|
||||
.all(|&c| c != b'<' && c != b'>' && c != b'\n')
|
||||
})?;
|
||||
|
||||
if cond_eq!(src, path + 1, b']') {
|
||||
if *bytes.get(path + 1)? == b']' {
|
||||
Some((
|
||||
Link {
|
||||
path: &src[2..path],
|
||||
|
|
@ -21,8 +26,10 @@ impl<'a> Link<'a> {
|
|||
},
|
||||
path + 2,
|
||||
))
|
||||
} else if src.as_bytes()[path + 1] == b'[' {
|
||||
let desc = until_while!(src, path + 2, b']', |c| c != b'[')?;
|
||||
} else if bytes[path + 1] == b'[' {
|
||||
let desc = memchr(b']', &bytes[path + 2..])
|
||||
.map(|i| i + path + 2)
|
||||
.filter(|&i| bytes[path + 2..i].iter().all(|&c| c != b'['))?;
|
||||
expect!(src, desc + 1, b']')?;
|
||||
|
||||
Some((
|
||||
|
|
@ -61,5 +68,4 @@ fn parse() {
|
|||
)
|
||||
);
|
||||
assert!(Link::parse("[[#id][desc]").is_none());
|
||||
assert!(Link::parse("[#id][desc]]").is_none());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use jetscii::Substring;
|
||||
use memchr::memchr2;
|
||||
|
||||
#[cfg_attr(test, derive(PartialEq))]
|
||||
#[derive(Debug)]
|
||||
|
|
@ -7,54 +8,83 @@ pub struct Macros<'a> {
|
|||
pub args: Option<&'a str>,
|
||||
}
|
||||
|
||||
fn valid_name(ch: u8) -> bool {
|
||||
ch.is_ascii_alphanumeric() || ch == b'-' || ch == b'_'
|
||||
}
|
||||
|
||||
impl<'a> Macros<'a> {
|
||||
pub fn parse(src: &'a str) -> Option<(Macros<'a>, usize)> {
|
||||
starts_with!(src, "{{{");
|
||||
debug_assert!(src.starts_with("{{{"));
|
||||
|
||||
expect!(src, 3, |c: u8| c.is_ascii_alphabetic())?;
|
||||
|
||||
let name = until_while!(src, 3, |c| c == b'}' || c == b'(', valid_name)?;
|
||||
let bytes = src.as_bytes();
|
||||
let name = memchr2(b'}', b'(', bytes).filter(|&i| {
|
||||
bytes[3..i]
|
||||
.iter()
|
||||
.all(|&c| c.is_ascii_alphanumeric() || c == b'-' || c == b'_')
|
||||
})?;
|
||||
|
||||
if src.as_bytes()[name] == b'}' {
|
||||
Some(if bytes[name] == b'}' {
|
||||
expect!(src, name + 1, b'}')?;
|
||||
expect!(src, name + 2, b'}')?;
|
||||
Some((
|
||||
(
|
||||
Macros {
|
||||
name: &src[3..name],
|
||||
args: None,
|
||||
},
|
||||
name + 3,
|
||||
))
|
||||
)
|
||||
} else {
|
||||
let end = Substring::new("}}}").find(&src[name..]).map(|i| i + name)?;
|
||||
expect!(src, end - 1, b')')?;
|
||||
Some((
|
||||
let end = Substring::new(")}}}")
|
||||
.find(&src[name..])
|
||||
.map(|i| i + name)?;
|
||||
(
|
||||
Macros {
|
||||
name: &src[3..name],
|
||||
args: if name == end {
|
||||
None
|
||||
} else {
|
||||
Some(&src[name + 1..end - 1])
|
||||
Some(&src[name + 1..end])
|
||||
},
|
||||
},
|
||||
end + 3,
|
||||
))
|
||||
}
|
||||
end + 4,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse() {
|
||||
parse_succ!(Macros, "{{{poem(red,blue)}}}", name: "poem", args: Some("red,blue"));
|
||||
parse_succ!(Macros, "{{{poem())}}}", name: "poem", args: Some(")"));
|
||||
parse_succ!(Macros, "{{{author}}}", name: "author", args: None);
|
||||
parse_fail!(Macros, "{{author}}}");
|
||||
parse_fail!(Macros, "{{{0uthor}}}");
|
||||
parse_fail!(Macros, "{{{author}}");
|
||||
parse_fail!(Macros, "{{{poem(}}}");
|
||||
parse_fail!(Macros, "{{{poem)}}}");
|
||||
assert_eq!(
|
||||
Macros::parse("{{{poem(red,blue)}}}"),
|
||||
Some((
|
||||
Macros {
|
||||
name: "poem",
|
||||
args: Some("red,blue")
|
||||
},
|
||||
"{{{poem(red,blue)}}}".len()
|
||||
))
|
||||
);
|
||||
assert_eq!(
|
||||
Macros::parse("{{{poem())}}}"),
|
||||
Some((
|
||||
Macros {
|
||||
name: "poem",
|
||||
args: Some(")")
|
||||
},
|
||||
"{{{poem())}}}".len()
|
||||
))
|
||||
);
|
||||
assert_eq!(
|
||||
Macros::parse("{{{author}}}"),
|
||||
Some((
|
||||
Macros {
|
||||
name: "author",
|
||||
args: None
|
||||
},
|
||||
"{{{author}}}".len()
|
||||
))
|
||||
);
|
||||
|
||||
assert_eq!(Macros::parse("{{{0uthor}}}"), None);
|
||||
assert_eq!(Macros::parse("{{{author}}"), None);
|
||||
assert_eq!(Macros::parse("{{{poem(}}}"), None);
|
||||
assert_eq!(Macros::parse("{{{poem)}}}"), None);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
mod cookie;
|
||||
mod emphasis;
|
||||
mod entity;
|
||||
mod fn_ref;
|
||||
mod fragment;
|
||||
mod inline_call;
|
||||
mod inline_src;
|
||||
mod link;
|
||||
|
|
@ -67,51 +65,47 @@ impl<'a> Object<'a> {
|
|||
|
||||
let mut pre = pos;
|
||||
|
||||
match (bytes[pos], bytes[pos + 1], bytes[pos + 2]) {
|
||||
(b'@', b'@', _) => {
|
||||
match bytes[pos] {
|
||||
b'@' if bytes[pos + 1] == b'@' => {
|
||||
if let Some((snippet, off)) = Snippet::parse(&src[pos..]) {
|
||||
brk!(Object::Snippet(snippet), off, pos);
|
||||
}
|
||||
}
|
||||
(b'{', b'{', b'{') => {
|
||||
b'{' if bytes[pos + 1] == b'{' && bytes[pos + 2] == b'{' => {
|
||||
if let Some((macros, off)) = Macros::parse(&src[pos..]) {
|
||||
brk!(Object::Macros(macros), off, pos);
|
||||
}
|
||||
}
|
||||
(b'<', b'<', b'<') => {
|
||||
if let Some((target, off)) = RadioTarget::parse(&src[pos..]) {
|
||||
brk!(Object::RadioTarget(target), off, pos);
|
||||
}
|
||||
}
|
||||
(b'<', b'<', third) => {
|
||||
if third != b'\n' {
|
||||
b'<' if bytes[pos + 1] == b'<' => {
|
||||
if bytes[pos + 2] == b'<' {
|
||||
if let Some((target, off)) = RadioTarget::parse(&src[pos..]) {
|
||||
brk!(Object::RadioTarget(target), off, pos);
|
||||
}
|
||||
} else if bytes[pos + 2] != b'\n' {
|
||||
if let Some((target, off)) = Target::parse(&src[pos..]) {
|
||||
brk!(Object::Target(target), off, pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
(b'[', b'f', b'n') => {
|
||||
if let Some((fn_ref, off)) = FnRef::parse(&src[pos..]) {
|
||||
brk!(Object::FnRef(fn_ref), off, pos);
|
||||
b'[' => {
|
||||
if bytes[pos + 1..].starts_with(b"fn:") {
|
||||
if let Some((fn_ref, off)) = FnRef::parse(&src[pos..]) {
|
||||
brk!(Object::FnRef(fn_ref), off, pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
(b'[', b'[', _) => {
|
||||
if let Some((link, off)) = Link::parse(&src[pos..]) {
|
||||
brk!(Object::Link(link), off, pos);
|
||||
|
||||
if bytes[pos + 1] == b'[' {
|
||||
if let Some((link, off)) = Link::parse(&src[pos..]) {
|
||||
brk!(Object::Link(link), off, pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
(b'[', _, _) => {
|
||||
|
||||
if let Some((cookie, off)) = Cookie::parse(&src[pos..]) {
|
||||
brk!(Object::Cookie(cookie), off, pos);
|
||||
}
|
||||
// TODO: Timestamp
|
||||
}
|
||||
(b'{', _, _)
|
||||
| (b' ', _, _)
|
||||
| (b'"', _, _)
|
||||
| (b',', _, _)
|
||||
| (b'(', _, _)
|
||||
| (b'\n', _, _) => pre += 1,
|
||||
b'{' | b' ' | b'"' | b',' | b'(' | b'\n' => pre += 1,
|
||||
_ => (),
|
||||
}
|
||||
|
||||
|
|
@ -146,12 +140,12 @@ impl<'a> Object<'a> {
|
|||
brk!(Object::Code(&src[pre + 1..pre + end]), end + 1, pre);
|
||||
}
|
||||
}
|
||||
b'c' => {
|
||||
b'c' if src[pre..].starts_with("call_") => {
|
||||
if let Some((call, off)) = InlineCall::parse(&src[pre..]) {
|
||||
brk!(Object::InlineCall(call), off, pre);
|
||||
}
|
||||
}
|
||||
b's' => {
|
||||
b's' if src[pre..].starts_with("src_") => {
|
||||
if let Some((src, off)) = InlineSrc::parse(&src[pre..]) {
|
||||
brk!(Object::InlineSrc(src), off, pre);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use jetscii::Substring;
|
||||
use memchr::memchr;
|
||||
|
||||
#[cfg_attr(test, derive(PartialEq))]
|
||||
#[derive(Debug)]
|
||||
|
|
@ -9,15 +10,14 @@ pub struct Snippet<'a> {
|
|||
|
||||
impl<'a> Snippet<'a> {
|
||||
pub fn parse(src: &'a str) -> Option<(Snippet<'a>, usize)> {
|
||||
if cfg!(test) {
|
||||
starts_with!(src, "@@");
|
||||
}
|
||||
debug_assert!(src.starts_with("@@"));
|
||||
|
||||
let name = until_while!(src, 2, b':', |c: u8| c.is_ascii_alphanumeric() || c == b'-')?;
|
||||
|
||||
if name == 2 {
|
||||
return None;
|
||||
}
|
||||
let name = memchr(b':', src.as_bytes()).filter(|&i| {
|
||||
i != 2
|
||||
&& src.as_bytes()[2..i]
|
||||
.iter()
|
||||
.all(|&c| c.is_ascii_alphanumeric() || c == b'-')
|
||||
})?;
|
||||
|
||||
let end = Substring::new("@@")
|
||||
.find(&src[name + 1..])
|
||||
|
|
@ -66,7 +66,6 @@ fn parse() {
|
|||
)
|
||||
);
|
||||
assert!(Snippet::parse("@@html:<b>@").is_none());
|
||||
assert!(Snippet::parse("@html:<b>@@").is_none());
|
||||
assert!(Snippet::parse("@@html<b>@@").is_none());
|
||||
assert!(Snippet::parse("@@:<b>@@").is_none());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use jetscii::Substring;
|
||||
|
||||
#[cfg_attr(test, derive(PartialEq))]
|
||||
#[derive(Debug)]
|
||||
// TODO: text-markup, entities, latex-fragments, subscript and superscript
|
||||
|
|
@ -5,17 +7,17 @@ pub struct RadioTarget<'a>(&'a str);
|
|||
|
||||
impl<'a> RadioTarget<'a> {
|
||||
pub fn parse(src: &'a str) -> Option<(RadioTarget<'a>, usize)> {
|
||||
if cfg!(test) {
|
||||
starts_with!(src, "<<<");
|
||||
}
|
||||
debug_assert!(src.starts_with("<<<"));
|
||||
|
||||
expect!(src, 3, |c| c != b' ')?;
|
||||
|
||||
let end = until_while!(src, 3, b'>', |c| c != b'<' && c != b'\n')?;
|
||||
let end = Substring::new(">>>").find(src).filter(|&i| {
|
||||
src.as_bytes()[3..i]
|
||||
.iter()
|
||||
.all(|&c| c != b'<' && c != b'\n' && c != b'>')
|
||||
})?;
|
||||
|
||||
expect!(src, end - 1, |c| c != b' ')?;
|
||||
expect!(src, end + 1, b'>')?;
|
||||
expect!(src, end + 2, b'>')?;
|
||||
|
||||
Some((RadioTarget(&src[3..end]), end + 3))
|
||||
}
|
||||
|
|
@ -27,16 +29,17 @@ pub struct Target<'a>(&'a str);
|
|||
|
||||
impl<'a> Target<'a> {
|
||||
pub fn parse(src: &'a str) -> Option<(Target<'a>, usize)> {
|
||||
if cfg!(test) {
|
||||
starts_with!(src, "<<");
|
||||
}
|
||||
debug_assert!(src.starts_with("<<"));
|
||||
|
||||
expect!(src, 2, |c| c != b' ')?;
|
||||
|
||||
let end = until_while!(src, 2, b'>', |c| c != b'<' && c != b'\n')?;
|
||||
let end = Substring::new(">>").find(src).filter(|&i| {
|
||||
src.as_bytes()[2..i]
|
||||
.iter()
|
||||
.all(|&c| c != b'<' && c != b'\n' && c != b'>')
|
||||
})?;
|
||||
|
||||
expect!(src, end - 1, |c| c != b' ')?;
|
||||
expect!(src, end + 1, b'>')?;
|
||||
|
||||
Some((Target(&src[2..end]), end + 2))
|
||||
}
|
||||
|
|
@ -52,13 +55,12 @@ fn parse() {
|
|||
RadioTarget::parse("<<<tar get>>>").unwrap(),
|
||||
(RadioTarget("tar get"), "<<<tar get>>>".len())
|
||||
);
|
||||
parse_fail!(RadioTarget, "<<<target >>>");
|
||||
parse_fail!(RadioTarget, "<<< target>>>");
|
||||
parse_fail!(RadioTarget, "<<<ta<get>>>");
|
||||
parse_fail!(RadioTarget, "<<<ta>get>>>");
|
||||
parse_fail!(RadioTarget, "<<<ta\nget>>>");
|
||||
parse_fail!(RadioTarget, "<<target>>>");
|
||||
parse_fail!(RadioTarget, "<<<target>>");
|
||||
assert_eq!(RadioTarget::parse("<<<target >>>"), None);
|
||||
assert_eq!(RadioTarget::parse("<<< target>>>"), None);
|
||||
assert_eq!(RadioTarget::parse("<<<ta<get>>>"), None);
|
||||
assert_eq!(RadioTarget::parse("<<<ta>get>>>"), None);
|
||||
assert_eq!(RadioTarget::parse("<<<ta\nget>>>"), None);
|
||||
assert_eq!(RadioTarget::parse("<<<target>>"), None);
|
||||
|
||||
assert_eq!(
|
||||
Target::parse("<<target>>").unwrap(),
|
||||
|
|
@ -68,11 +70,10 @@ fn parse() {
|
|||
Target::parse("<<tar get>>").unwrap(),
|
||||
(Target("tar get"), "<<tar get>>".len())
|
||||
);
|
||||
parse_fail!(Target, "<<target >>");
|
||||
parse_fail!(Target, "<< target>>");
|
||||
parse_fail!(Target, "<<ta<get>>");
|
||||
parse_fail!(Target, "<<ta>get>>");
|
||||
parse_fail!(Target, "<<ta\nget>>");
|
||||
parse_fail!(Target, "<target>>");
|
||||
parse_fail!(Target, "<<target>");
|
||||
assert_eq!(Target::parse("<<target >>"), None);
|
||||
assert_eq!(Target::parse("<< target>>"), None);
|
||||
assert_eq!(Target::parse("<<ta<get>>"), None);
|
||||
assert_eq!(Target::parse("<<ta>get>>"), None);
|
||||
assert_eq!(Target::parse("<<ta\nget>>"), None);
|
||||
assert_eq!(Target::parse("<<target>"), None);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue