refactor(uri): Remove vec in uri parsing
Remove vec allocations in uri parsing. Additionally, change tests to check the port. https://github.com/hyperium/hyper/issues/1029
This commit is contained in:
27
src/uri.rs
27
src/uri.rs
@@ -145,15 +145,9 @@ impl Uri {
|
|||||||
|
|
||||||
/// Get the port of this `Uri.
|
/// Get the port of this `Uri.
|
||||||
pub fn port(&self) -> Option<u16> {
|
pub fn port(&self) -> Option<u16> {
|
||||||
if let Some(auth) = self.authority() {
|
match self.authority() {
|
||||||
let v: Vec<&str> = auth.split(":").collect();
|
Some(auth) => auth.find(":").and_then(|i| u16::from_str(&auth[i+1..]).ok()),
|
||||||
if v.len() == 2 {
|
None => None,
|
||||||
u16::from_str(v[1]).ok()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,14 +177,12 @@ fn parse_scheme(s: &str) -> Option<usize> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_authority(s: &str) -> Option<usize> {
|
fn parse_authority(s: &str) -> Option<usize> {
|
||||||
let v: Vec<&str> = s.split("://").collect();
|
let i = s.find("://").and_then(|p| Some(p + 3)).unwrap_or(0);
|
||||||
match v.last() {
|
|
||||||
Some(auth) => Some(auth.split("/")
|
Some(&s[i..].split("/")
|
||||||
.next()
|
.next()
|
||||||
.unwrap_or(s)
|
.unwrap_or(s)
|
||||||
.len() + if v.len() == 2 { v[0].len() + 3 } else { 0 }),
|
.len() + i)
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_query(s: &str) -> Option<usize> {
|
fn parse_query(s: &str) -> Option<usize> {
|
||||||
@@ -297,6 +289,7 @@ test_parse! {
|
|||||||
path = "/chunks",
|
path = "/chunks",
|
||||||
query = None,
|
query = None,
|
||||||
fragment = None,
|
fragment = None,
|
||||||
|
port = Some(61761),
|
||||||
}
|
}
|
||||||
|
|
||||||
test_parse! {
|
test_parse! {
|
||||||
@@ -308,6 +301,7 @@ test_parse! {
|
|||||||
path = "/",
|
path = "/",
|
||||||
query = None,
|
query = None,
|
||||||
fragment = None,
|
fragment = None,
|
||||||
|
port = Some(61761),
|
||||||
}
|
}
|
||||||
|
|
||||||
test_parse! {
|
test_parse! {
|
||||||
@@ -330,6 +324,7 @@ test_parse! {
|
|||||||
path = "",
|
path = "",
|
||||||
query = None,
|
query = None,
|
||||||
fragment = None,
|
fragment = None,
|
||||||
|
port = Some(3000),
|
||||||
}
|
}
|
||||||
|
|
||||||
test_parse! {
|
test_parse! {
|
||||||
@@ -341,6 +336,7 @@ test_parse! {
|
|||||||
path = "/",
|
path = "/",
|
||||||
query = None,
|
query = None,
|
||||||
fragment = None,
|
fragment = None,
|
||||||
|
port = Some(80),
|
||||||
}
|
}
|
||||||
|
|
||||||
test_parse! {
|
test_parse! {
|
||||||
@@ -352,6 +348,7 @@ test_parse! {
|
|||||||
path = "/",
|
path = "/",
|
||||||
query = None,
|
query = None,
|
||||||
fragment = None,
|
fragment = None,
|
||||||
|
port = Some(443),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user