Skip to content

Commit

Permalink
Implement range index with the Position enum on ServoUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
stshine committed Nov 18, 2016
1 parent 22aebdf commit 0818b44
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion components/net/data_loader.rs
Expand Up @@ -35,7 +35,7 @@ pub type DecodeData = (Mime, Vec<u8>);
pub fn decode(url: &ServoUrl) -> Result<DecodeData, DecodeError> {
assert_eq!(url.scheme(), "data");
// Split out content type and data.
let parts: Vec<&str> = url.as_url().unwrap()[Position::BeforePath..Position::AfterQuery].splitn(2, ',').collect();
let parts: Vec<&str> = url[Position::BeforePath..Position::AfterQuery].splitn(2, ',').collect();
if parts.len() != 2 {
return Err(DecodeError::InvalidDataUri);
}
Expand Down
2 changes: 1 addition & 1 deletion components/net/http_loader.rs
Expand Up @@ -931,7 +931,7 @@ pub fn load<A, B>(load_data: &LoadData,
// the source rather than rendering the contents of the URL.
let viewing_source = doc_url.scheme() == "view-source";
if viewing_source {
doc_url = ServoUrl::parse(&load_data.url.as_url().unwrap()[Position::BeforeUsername..]).unwrap();
doc_url = ServoUrl::parse(&load_data.url[Position::BeforeUsername..]).unwrap();
}

// Loop to handle redirects.
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/response.rs
Expand Up @@ -358,7 +358,7 @@ impl ResponseMethods for Response {
}

fn serialize_without_fragment(url: &ServoUrl) -> &str {
&url.as_url().unwrap()[..Position::AfterQuery]
&url[..Position::AfterQuery]
}

impl Response {
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/xmlhttprequest.rs
Expand Up @@ -878,7 +878,7 @@ impl XMLHttpRequest {
},
};

*self.response_url.borrow_mut() = metadata.final_url.as_url().unwrap()[..Position::AfterQuery].to_owned();
*self.response_url.borrow_mut() = metadata.final_url[..Position::AfterQuery].to_owned();

// XXXManishearth Clear cache entries in case of a network error
self.process_partial_response(XHRProgress::HeadersReceived(
Expand Down
2 changes: 1 addition & 1 deletion components/script/script_thread.rs
Expand Up @@ -1753,7 +1753,7 @@ impl ScriptThread {
// Start with the scheme data of the parsed URL;
// append question mark and query component, if any;
// append number sign and fragment component if any.
let encoded = &incomplete.url.as_url().unwrap()[Position::BeforePath..];
let encoded = &incomplete.url[Position::BeforePath..];

// Percent-decode (8.) and UTF-8 decode (9.)
let script_source = percent_decode(encoded.as_bytes()).decode_utf8_lossy();
Expand Down
31 changes: 30 additions & 1 deletion components/url/lib.rs
Expand Up @@ -19,9 +19,10 @@ extern crate url;

use std::fmt;
use std::net::IpAddr;
use std::ops::{Range, RangeFrom, RangeTo, RangeFull, Index};
use std::path::Path;
use std::sync::Arc;
use url::{Url, Origin};
use url::{Url, Origin, Position};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Serialize, Deserialize))]
Expand Down Expand Up @@ -148,3 +149,31 @@ impl fmt::Display for ServoUrl {
self.0.fmt(formatter)
}
}

impl Index<RangeFull> for ServoUrl {
type Output = str;
fn index(&self, _: RangeFull) -> &str {
&self.0[..]
}
}

impl Index<RangeFrom<Position>> for ServoUrl {
type Output = str;
fn index(&self, range: RangeFrom<Position>) -> &str {
&self.0[range]
}
}

impl Index<RangeTo<Position>> for ServoUrl {
type Output = str;
fn index(&self, range: RangeTo<Position>) -> &str {
&self.0[range]
}
}

impl Index<Range<Position>> for ServoUrl {
type Output = str;
fn index(&self, range: Range<Position>) -> &str {
&self.0[range]
}
}

0 comments on commit 0818b44

Please sign in to comment.