mirror of https://github.com/fafhrd91/actix-net
remove unnecessary len
This is used to strore the number of static chrs after the last capture group. However, we don't need this to calculate the totalmatched length, we can inseted use the 0th capture group.
This commit is contained in:
parent
908583982f
commit
515e1089b1
|
@ -38,8 +38,8 @@ enum PatternElement {
|
||||||
enum PatternType {
|
enum PatternType {
|
||||||
Static(String),
|
Static(String),
|
||||||
Prefix(String),
|
Prefix(String),
|
||||||
Dynamic(Regex, Vec<&'static str>, usize),
|
Dynamic(Regex, Vec<&'static str>),
|
||||||
DynamicSet(RegexSet, Vec<(Regex, Vec<&'static str>, usize)>),
|
DynamicSet(RegexSet, Vec<(Regex, Vec<&'static str>)>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ResourceDef {
|
impl ResourceDef {
|
||||||
|
@ -56,7 +56,7 @@ impl ResourceDef {
|
||||||
let mut re_set = Vec::new();
|
let mut re_set = Vec::new();
|
||||||
|
|
||||||
for path in set {
|
for path in set {
|
||||||
let (pattern, _, _, len) = ResourceDef::parse(&path, false);
|
let (pattern, _, _) = ResourceDef::parse(&path, false);
|
||||||
|
|
||||||
let re = match Regex::new(&pattern) {
|
let re = match Regex::new(&pattern) {
|
||||||
Ok(re) => re,
|
Ok(re) => re,
|
||||||
|
@ -69,7 +69,7 @@ impl ResourceDef {
|
||||||
name.map(|name| Box::leak(Box::new(name.to_owned())).as_str())
|
name.map(|name| Box::leak(Box::new(name.to_owned())).as_str())
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
data.push((re, names, len));
|
data.push((re, names));
|
||||||
re_set.push(pattern);
|
re_set.push(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +116,7 @@ impl ResourceDef {
|
||||||
/// Parse path pattern and create new `Pattern` instance with custom prefix
|
/// Parse path pattern and create new `Pattern` instance with custom prefix
|
||||||
fn with_prefix(path: &str, for_prefix: bool) -> Self {
|
fn with_prefix(path: &str, for_prefix: bool) -> Self {
|
||||||
let path = path.to_owned();
|
let path = path.to_owned();
|
||||||
let (pattern, elements, is_dynamic, len) = ResourceDef::parse(&path, for_prefix);
|
let (pattern, elements, is_dynamic) = ResourceDef::parse(&path, for_prefix);
|
||||||
|
|
||||||
let tp = if is_dynamic {
|
let tp = if is_dynamic {
|
||||||
let re = match Regex::new(&pattern) {
|
let re = match Regex::new(&pattern) {
|
||||||
|
@ -130,7 +130,7 @@ impl ResourceDef {
|
||||||
name.map(|name| Box::leak(Box::new(name.to_owned())).as_str())
|
name.map(|name| Box::leak(Box::new(name.to_owned())).as_str())
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
PatternType::Dynamic(re, names, len)
|
PatternType::Dynamic(re, names)
|
||||||
} else if for_prefix {
|
} else if for_prefix {
|
||||||
PatternType::Prefix(pattern)
|
PatternType::Prefix(pattern)
|
||||||
} else {
|
} else {
|
||||||
|
@ -167,7 +167,7 @@ impl ResourceDef {
|
||||||
match self.tp {
|
match self.tp {
|
||||||
PatternType::Static(ref s) => s == path,
|
PatternType::Static(ref s) => s == path,
|
||||||
PatternType::Prefix(ref s) => path.starts_with(s),
|
PatternType::Prefix(ref s) => path.starts_with(s),
|
||||||
PatternType::Dynamic(ref re, _, _) => re.is_match(path),
|
PatternType::Dynamic(ref re, _) => re.is_match(path),
|
||||||
PatternType::DynamicSet(ref re, _) => re.is_match(path),
|
PatternType::DynamicSet(ref re, _) => re.is_match(path),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -185,25 +185,7 @@ impl ResourceDef {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PatternType::Dynamic(ref re, _, len) => {
|
PatternType::Dynamic(ref re, _) => re.find(path).map(|m| m.end()),
|
||||||
if let Some(captures) = re.captures(path) {
|
|
||||||
let mut pos = 0;
|
|
||||||
let mut passed = false;
|
|
||||||
for capture in captures.iter() {
|
|
||||||
if let Some(ref m) = capture {
|
|
||||||
if !passed {
|
|
||||||
passed = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
pos = m.end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(pos + len)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
PatternType::Prefix(ref s) => {
|
PatternType::Prefix(ref s) => {
|
||||||
let len = if path == s {
|
let len = if path == s {
|
||||||
s.len()
|
s.len()
|
||||||
|
@ -222,24 +204,8 @@ impl ResourceDef {
|
||||||
}
|
}
|
||||||
PatternType::DynamicSet(ref re, ref params) => {
|
PatternType::DynamicSet(ref re, ref params) => {
|
||||||
if let Some(idx) = re.matches(path).into_iter().next() {
|
if let Some(idx) = re.matches(path).into_iter().next() {
|
||||||
let (ref pattern, _, len) = params[idx];
|
let (ref pattern, _) = params[idx];
|
||||||
if let Some(captures) = pattern.captures(path) {
|
pattern.find(path).map(|m| m.end())
|
||||||
let mut pos = 0;
|
|
||||||
let mut passed = false;
|
|
||||||
for capture in captures.iter() {
|
|
||||||
if let Some(ref m) = capture {
|
|
||||||
if !passed {
|
|
||||||
passed = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
pos = m.end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(pos + len)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -293,13 +259,10 @@ impl ResourceDef {
|
||||||
};
|
};
|
||||||
(min(path.len(), len), None)
|
(min(path.len(), len), None)
|
||||||
}
|
}
|
||||||
PatternType::Dynamic(ref re, ref names, len) => {
|
PatternType::Dynamic(ref re, ref names) => {
|
||||||
let mut pos = 0;
|
|
||||||
|
|
||||||
if let Some(captures) = re.captures(path.path()) {
|
if let Some(captures) = re.captures(path.path()) {
|
||||||
for (no, name) in names.iter().enumerate() {
|
for (no, name) in names.iter().enumerate() {
|
||||||
if let Some(m) = captures.name(&name) {
|
if let Some(m) = captures.name(&name) {
|
||||||
pos = m.end();
|
|
||||||
segments[no] = PathItem::Segment(m.start() as u16, m.end() as u16);
|
segments[no] = PathItem::Segment(m.start() as u16, m.end() as u16);
|
||||||
} else {
|
} else {
|
||||||
log::error!(
|
log::error!(
|
||||||
|
@ -309,21 +272,19 @@ impl ResourceDef {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
(captures[0].len(), Some(names))
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
(pos + len, Some(names))
|
|
||||||
}
|
}
|
||||||
PatternType::DynamicSet(ref re, ref params) => {
|
PatternType::DynamicSet(ref re, ref params) => {
|
||||||
let path = path.path();
|
let path = path.path();
|
||||||
if let Some(idx) = re.matches(path).into_iter().next() {
|
if let Some(idx) = re.matches(path).into_iter().next() {
|
||||||
let (ref pattern, ref names, len) = params[idx];
|
let (ref pattern, ref names) = params[idx];
|
||||||
let mut pos = 0;
|
|
||||||
|
|
||||||
if let Some(captures) = pattern.captures(path) {
|
if let Some(captures) = pattern.captures(path) {
|
||||||
for (no, name) in names.iter().enumerate() {
|
for (no, name) in names.iter().enumerate() {
|
||||||
if let Some(m) = captures.name(&name) {
|
if let Some(m) = captures.name(&name) {
|
||||||
pos = m.end();
|
|
||||||
segments[no] =
|
segments[no] =
|
||||||
PathItem::Segment(m.start() as u16, m.end() as u16);
|
PathItem::Segment(m.start() as u16, m.end() as u16);
|
||||||
} else {
|
} else {
|
||||||
|
@ -334,10 +295,10 @@ impl ResourceDef {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
(captures[0].len(), Some(names))
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
(pos + len, Some(names))
|
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -451,20 +412,16 @@ impl ResourceDef {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(
|
fn parse(mut pattern: &str, mut for_prefix: bool) -> (String, Vec<PatternElement>, bool) {
|
||||||
mut pattern: &str,
|
|
||||||
mut for_prefix: bool,
|
|
||||||
) -> (String, Vec<PatternElement>, bool, usize) {
|
|
||||||
if pattern.find('{').is_none() {
|
if pattern.find('{').is_none() {
|
||||||
return if let Some(path) = pattern.strip_suffix('*') {
|
return if let Some(path) = pattern.strip_suffix('*') {
|
||||||
let re = format!("{}^{}(.*)", REGEX_FLAGS, path);
|
let re = format!("{}^{}(.*)", REGEX_FLAGS, path);
|
||||||
(re, vec![PatternElement::Const(String::from(path))], true, 0)
|
(re, vec![PatternElement::Const(String::from(path))], true)
|
||||||
} else {
|
} else {
|
||||||
(
|
(
|
||||||
String::from(pattern),
|
String::from(pattern),
|
||||||
vec![PatternElement::Const(String::from(pattern))],
|
vec![PatternElement::Const(String::from(pattern))],
|
||||||
false,
|
false,
|
||||||
pattern.chars().count(),
|
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -501,7 +458,7 @@ impl ResourceDef {
|
||||||
if !for_prefix {
|
if !for_prefix {
|
||||||
re.push('$');
|
re.push('$');
|
||||||
}
|
}
|
||||||
(re, elements, true, pattern.chars().count())
|
(re, elements, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue