refactor: cleanup parse function

This commit is contained in:
PoiScript 2019-02-08 21:34:58 +08:00
parent c1154a1853
commit c5b14256f0
25 changed files with 1299 additions and 1234 deletions

View file

@ -2,74 +2,50 @@ use memchr::{memchr, memchr2};
#[cfg_attr(test, derive(PartialEq))]
#[derive(Debug)]
pub struct Cookie<'a> {
value: &'a str,
pub enum Cookie<'a> {
Percent(&'a str),
Slash(&'a str, &'a str),
}
impl<'a> Cookie<'a> {
pub fn parse(src: &'a str) -> Option<(Cookie<'a>, usize)> {
debug_assert!(src.starts_with('['));
#[inline]
pub fn parse(src: &str) -> Option<(Cookie<'_>, usize)> {
debug_assert!(src.starts_with('['));
let num1 = memchr2(b'%', b'/', src.as_bytes())
.filter(|&i| src.as_bytes()[1..i].iter().all(|c| c.is_ascii_digit()))?;
let bytes = src.as_bytes();
let num1 =
memchr2(b'%', b'/', bytes).filter(|&i| 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 + 1],
},
num1 + 2,
))
} else {
let num2 = memchr(b']', src.as_bytes()).filter(|&i| {
src.as_bytes()[num1 + 1..i]
.iter()
.all(|c| c.is_ascii_digit())
})?;
if bytes[num1] == b'%' && *bytes.get(num1 + 1)? == b']' {
Some((Cookie::Percent(&src[1..num1]), num1 + 2))
} else {
let num2 = memchr(b']', bytes)
.filter(|&i| bytes[num1 + 1..i].iter().all(|c| c.is_ascii_digit()))?;
Some((
Cookie {
value: &src[0..=num2],
},
num2 + 1,
))
}
Some((Cookie::Slash(&src[1..num1], &src[num1 + 1..num2]), num2 + 1))
}
}
#[test]
fn parse() {
assert_eq!(
Cookie::parse("[1/10]").unwrap(),
(Cookie { value: "[1/10]" }, "[1/10]".len())
);
assert_eq!(
Cookie::parse("[1/1000]").unwrap(),
(Cookie { value: "[1/1000]" }, "[1/1000]".len())
);
assert_eq!(
Cookie::parse("[10%]").unwrap(),
(Cookie { value: "[10%]" }, "[10%]".len())
);
assert_eq!(
Cookie::parse("[%]").unwrap(),
(Cookie { value: "[%]" }, "[%]".len())
);
assert_eq!(
Cookie::parse("[/]").unwrap(),
(Cookie { value: "[/]" }, "[/]".len())
);
assert_eq!(
Cookie::parse("[100/]").unwrap(),
(Cookie { value: "[100/]" }, "[100/]".len())
);
assert_eq!(
Cookie::parse("[/100]").unwrap(),
(Cookie { value: "[/100]" }, "[/100]".len())
);
#[cfg(test)]
mod tests {
#[test]
fn parse() {
use super::parse;
use super::Cookie::*;
assert!(Cookie::parse("[10% ]").is_none(),);
assert!(Cookie::parse("[1//100]").is_none(),);
assert!(Cookie::parse("[1\\100]").is_none(),);
assert!(Cookie::parse("[10%%]").is_none(),);
assert_eq!(parse("[1/10]").unwrap(), (Slash("1", "10"), "[1/10]".len()));
assert_eq!(
parse("[1/1000]").unwrap(),
(Slash("1", "1000"), "[1/1000]".len())
);
assert_eq!(parse("[10%]").unwrap(), (Percent("10"), "[10%]".len()));
assert_eq!(parse("[%]").unwrap(), (Percent(""), "[%]".len()));
assert_eq!(parse("[/]").unwrap(), (Slash("", ""), "[/]".len()));
assert_eq!(parse("[100/]").unwrap(), (Slash("100", ""), "[100/]".len()));
assert_eq!(parse("[/100]").unwrap(), (Slash("", "100"), "[/100]".len()));
assert!(parse("[10% ]").is_none(),);
assert!(parse("[1//100]").is_none(),);
assert!(parse("[1\\100]").is_none(),);
assert!(parse("[10%%]").is_none(),);
}
}