Merge pull request #1030 from M3rs/remove_vec

refactor(uri): Remove vec in uri parsing

Closes #1029
This commit is contained in:
Sean McArthur
2017-01-23 16:54:03 -08:00
committed by GitHub

View File

@@ -145,16 +145,10 @@ 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
}
} }
/// Get the query string of this `Uri`, starting after the `?`. /// Get the query string of this `Uri`, starting after the `?`.
@@ -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]