diff --git a/src/form_urlencoded.rs b/src/form_urlencoded.rs index cd031224c..c75ab8b86 100644 --- a/src/form_urlencoded.rs +++ b/src/form_urlencoded.rs @@ -55,7 +55,7 @@ fn parse_internal(input: &[u8], mut encoding_override: EncodingOverride, mut use for piece in input.split(|&b| b == b'&') { if !piece.is_empty() { let (name, value) = match piece.position_elem(&b'=') { - Some(position) => (piece.slice_to(position), piece.slice_from(position + 1)), + Some(position) => (&piece[..position], &piece[position + 1..]), None => (piece, [].as_slice()) }; diff --git a/src/format.rs b/src/format.rs index 09645c08d..861a9d360 100644 --- a/src/format.rs +++ b/src/format.rs @@ -10,7 +10,7 @@ //! //! These formatters can be used to coerce various URL parts into strings. //! -//! You can use `.to_string()`, as the formatters implement `fmt::String`. +//! You can use `.to_string()`, as the formatters implement `fmt::Display`. use std::fmt::{self, Formatter}; use super::Url; @@ -21,7 +21,7 @@ pub struct PathFormatter<'a, T:'a> { pub path: &'a [T] } -impl<'a, T: Str + fmt::String> fmt::String for PathFormatter<'a, T> { +impl<'a, T: Str + fmt::Display> fmt::Display for PathFormatter<'a, T> { fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { if self.path.is_empty() { formatter.write_str("/") @@ -47,7 +47,7 @@ pub struct UserInfoFormatter<'a> { pub password: Option<&'a str> } -impl<'a> fmt::String for UserInfoFormatter<'a> { +impl<'a> fmt::Display for UserInfoFormatter<'a> { fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { if !self.username.is_empty() || self.password.is_some() { try!(formatter.write_str(self.username)); @@ -67,7 +67,7 @@ pub struct UrlNoFragmentFormatter<'a> { pub url: &'a Url } -impl<'a> fmt::String for UrlNoFragmentFormatter<'a> { +impl<'a> fmt::Display for UrlNoFragmentFormatter<'a> { fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { try!(formatter.write_str(self.url.scheme.as_slice())); try!(formatter.write_str(":")); diff --git a/src/host.rs b/src/host.rs index f2b0c7db2..f5e41cce2 100644 --- a/src/host.rs +++ b/src/host.rs @@ -14,7 +14,7 @@ use percent_encoding::{from_hex, percent_decode}; /// The host name of an URL. -#[derive(PartialEq, Eq, Clone, Show)] +#[derive(PartialEq, Eq, Clone, Debug)] pub enum Host { /// A (DNS) domain name or an IPv4 address. /// @@ -30,7 +30,7 @@ pub enum Host { /// A 128 bit IPv6 address -#[derive(Clone, Eq, PartialEq, Copy, Show)] +#[derive(Clone, Eq, PartialEq, Copy, Debug)] pub struct Ipv6Address { pub pieces: [u16; 8] } @@ -48,7 +48,7 @@ impl Host { Err(ParseError::EmptyHost) } else if input.starts_with("[") { if input.ends_with("]") { - Ipv6Address::parse(input.slice(1, input.len() - 1)).map(Host::Ipv6) + Ipv6Address::parse(&input[1..input.len() - 1]).map(Host::Ipv6) } else { Err(ParseError::InvalidIpv6Address) } @@ -77,7 +77,7 @@ impl Host { } -impl fmt::String for Host { +impl fmt::Display for Host { fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { match *self { Host::Domain(ref domain) => domain.fmt(formatter), @@ -218,7 +218,7 @@ impl Ipv6Address { } -impl fmt::String for Ipv6Address { +impl fmt::Display for Ipv6Address { fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { let (compress_start, compress_end) = longest_zero_sequence(&self.pieces); let mut i = 0; diff --git a/src/lib.rs b/src/lib.rs index 700276bae..ae326f0ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,7 +157,7 @@ mod tests; /// The parsed representation of an absolute URL. -#[derive(PartialEq, Eq, Clone, Show)] +#[derive(PartialEq, Eq, Clone, Debug)] pub struct Url { /// The scheme (a.k.a. protocol) of the URL, in ASCII lower case. pub scheme: String, @@ -188,7 +188,7 @@ pub struct Url { } /// The components of the URL whose representation depends on where the scheme is *relative*. -#[derive(PartialEq, Eq, Clone, Show)] +#[derive(PartialEq, Eq, Clone, Debug)] pub enum SchemeData { /// Components for URLs in a *relative* scheme such as HTTP. Relative(RelativeSchemeData), @@ -202,7 +202,7 @@ pub enum SchemeData { } /// Components for URLs in a *relative* scheme such as HTTP. -#[derive(PartialEq, Eq, Clone, Show)] +#[derive(PartialEq, Eq, Clone, Debug)] pub struct RelativeSchemeData { /// The username of the URL, as a possibly empty, pecent-encoded string. /// @@ -405,7 +405,7 @@ impl<'a> UrlParser<'a> { /// Determines the behavior of the URL parser for a given scheme. -#[derive(PartialEq, Eq, Copy, Show)] +#[derive(PartialEq, Eq, Copy, Debug)] pub enum SchemeType { /// Indicate that the scheme is *non-relative*. /// @@ -768,7 +768,7 @@ impl rustc_serialize::Decodable for Url { } -impl fmt::String for Url { +impl fmt::Display for Url { fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { try!(UrlNoFragmentFormatter{ url: self }.fmt(formatter)); if let Some(ref fragment) = self.fragment { @@ -780,7 +780,7 @@ impl fmt::String for Url { } -impl fmt::String for SchemeData { +impl fmt::Display for SchemeData { fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { match *self { SchemeData::Relative(ref scheme_data) => scheme_data.fmt(formatter), @@ -885,7 +885,7 @@ impl RelativeSchemeData { } -impl fmt::String for RelativeSchemeData { +impl fmt::Display for RelativeSchemeData { fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { // Write the scheme-trailing double slashes. try!(formatter.write_str("//")); @@ -940,7 +940,7 @@ impl ToUrlPath for path::windows::Path { return Err(()) } // Start with the prefix, e.g. "C:" - let mut path = vec![self.as_str().unwrap().slice_to(2).to_string()]; + let mut path = vec![self.as_str().unwrap()[..2].to_string()]; // self.components() does not include the prefix for component in self.components() { path.push(percent_encode(component, DEFAULT_ENCODE_SET)); diff --git a/src/parser.rs b/src/parser.rs index 375e8b342..2ca8f1cfb 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -24,7 +24,7 @@ pub type ParseResult = Result; macro_rules! simple_enum_error { ($($name: ident => $description: expr,)+) => { /// Errors that can occur during parsing. - #[derive(PartialEq, Eq, Clone, Copy, Show)] + #[derive(PartialEq, Eq, Clone, Copy, Debug)] pub enum ParseError { $( $name, @@ -70,7 +70,7 @@ simple_enum_error! { CannotSetPathWithNonRelativeScheme => "cannot set path with non-relative scheme", } -impl fmt::String for ParseError { +impl fmt::Display for ParseError { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { self.description().fmt(fmt) } @@ -154,8 +154,8 @@ pub fn parse_scheme<'a>(input: &'a str, context: Context) -> Option<(String, &'a match c { 'a'...'z' | 'A'...'Z' | '0'...'9' | '+' | '-' | '.' => (), ':' => return Some(( - input.slice_to(i).to_ascii_lowercase(), - input.slice_from(i + 1), + input[..i].to_ascii_lowercase(), + &input[i + 1..], )), _ => return None, } @@ -204,7 +204,7 @@ fn parse_relative_url<'a>(input: &'a str, scheme: String, scheme_type: SchemeTyp } if scheme_type == SchemeType::FileLike { // File host state - let remaining = input.slice_from(2); + let remaining = &input[2..]; let (host, remaining) = if remaining.len() >= 2 && starts_with_ascii_alpha(remaining) && matches!(remaining.char_at(1), ':' | '|') @@ -232,7 +232,7 @@ fn parse_relative_url<'a>(input: &'a str, scheme: String, scheme_type: SchemeTyp } else { // Relative path state let (path, remaining) = try!(parse_path( - &[], input.slice_from(1), Context::UrlParser, scheme_type, parser)); + &[], &input[1..], Context::UrlParser, scheme_type, parser)); let scheme_data = SchemeData::Relative(if scheme_type == SchemeType::FileLike { RelativeSchemeData { username: String::new(), password: None, host: @@ -260,7 +260,7 @@ fn parse_relative_url<'a>(input: &'a str, scheme: String, scheme_type: SchemeTyp query: query, fragment: fragment }) }, '#' => { - let fragment = Some(try!(parse_fragment(input.slice_from(1), parser))); + let fragment = Some(try!(parse_fragment(&input[1..], parser))); Ok(Url { scheme: scheme, scheme_data: SchemeData::Relative(base.clone()), query: base_query.clone(), fragment: fragment }) } @@ -283,7 +283,7 @@ fn parse_relative_url<'a>(input: &'a str, scheme: String, scheme_type: SchemeTyp path: path }), remaining) } else { - let base_path = base.path.slice_to(base.path.len() - 1); + let base_path = &base.path[..base.path.len() - 1]; // Relative path state let (path, remaining) = try!(parse_path( base_path, input, Context::UrlParser, scheme_type, parser)); @@ -306,10 +306,10 @@ fn parse_relative_url<'a>(input: &'a str, scheme: String, scheme_type: SchemeTyp fn skip_slashes<'a>(input: &'a str, parser: &UrlParser) -> ParseResult<&'a str> { let first_non_slash = input.find(|&:c| !matches!(c, '/' | '\\')).unwrap_or(input.len()); - if input.slice_to(first_non_slash) != "//" { + if &input[..first_non_slash] != "//" { try!(parser.parse_error(ParseError::ExpectedTwoSlashes)); } - Ok(input.slice_from(first_non_slash)) + Ok(&input[first_non_slash..]) } @@ -329,7 +329,7 @@ fn parse_userinfo<'a>(input: &'a str, parser: &UrlParser) } } let (input, remaining) = match last_at { - Some(at) => (input.slice_to(at), input.slice_from(at + 1)), + Some(at) => (&input[..at], &input[at + 1..]), None => return Ok((String::new(), None, input)), }; @@ -338,7 +338,7 @@ fn parse_userinfo<'a>(input: &'a str, parser: &UrlParser) for (i, c, next_i) in input.char_ranges() { match c { ':' => { - password = Some(try!(parse_password(input.slice_from(i + 1), parser))); + password = Some(try!(parse_password(&input[i + 1..], parser))); break }, '\t' | '\n' | '\r' => try!(parser.parse_error(ParseError::InvalidCharacter)), @@ -346,7 +346,7 @@ fn parse_userinfo<'a>(input: &'a str, parser: &UrlParser) try!(check_url_code_point(input, i, c, parser)); // The spec says to use the default encode set, // but also replaces '@' by '%40' in an earlier step. - utf8_percent_encode_to(input.slice(i, next_i), + utf8_percent_encode_to(&input[i..next_i], USERINFO_ENCODE_SET, &mut username); } } @@ -364,7 +364,7 @@ fn parse_password(input: &str, parser: &UrlParser) -> ParseResult { try!(check_url_code_point(input, i, c, parser)); // The spec says to use the default encode set, // but also replaces '@' by '%40' in an earlier step. - utf8_percent_encode_to(input.slice(i, next_i), + utf8_percent_encode_to(&input[i..next_i], USERINFO_ENCODE_SET, &mut password); } } @@ -377,7 +377,7 @@ pub fn parse_host<'a>(input: &'a str, scheme_type: SchemeType, parser: &UrlParse -> ParseResult<(Host, Option, Option, &'a str)> { let (host, remaining) = try!(parse_hostname(input, parser)); let (port, default_port, remaining) = if remaining.starts_with(":") { - try!(parse_port(remaining.slice_from(1), scheme_type, parser)) + try!(parse_port(&remaining[1..], scheme_type, parser)) } else { (None, scheme_type.default_port(), remaining) }; @@ -412,7 +412,7 @@ pub fn parse_hostname<'a>(input: &'a str, parser: &UrlParser) } } let host = try!(Host::parse(host_input.as_slice())); - Ok((host, input.slice_from(end))) + Ok((host, &input[end..])) } @@ -443,7 +443,7 @@ pub fn parse_port<'a>(input: &'a str, scheme_type: SchemeType, parser: &UrlParse if !has_any_digit || port == default_port { port = None; } - return Ok((port, default_port, input.slice_from(end))) + return Ok((port, default_port, &input[end..])) } @@ -465,7 +465,7 @@ fn parse_file_host<'a>(input: &'a str, parser: &UrlParser) -> ParseResult<(Host, } else { try!(Host::parse(host_input.as_slice())) }; - Ok((host, input.slice_from(end))) + Ok((host, &input[end..])) } @@ -479,7 +479,7 @@ pub fn parse_standalone_path(input: &str, parser: &UrlParser) } } let (path, remaining) = try!(parse_path( - &[], input.slice_from(1), Context::UrlParser, SchemeType::Relative(0), parser)); + &[], &input[1..], Context::UrlParser, SchemeType::Relative(0), parser)); let (query, fragment) = try!(parse_query_and_fragment(remaining, parser)); Ok((path, query, fragment)) } @@ -500,7 +500,7 @@ pub fn parse_path_start<'a>(input: &'a str, context: Context, scheme_type: Schem _ => () } } - parse_path(&[], input.slice_from(i), context, scheme_type, parser) + parse_path(&[], &input[i..], context, scheme_type, parser) } @@ -535,7 +535,7 @@ fn parse_path<'a>(base_path: &[String], input: &'a str, context: Context, '\t' | '\n' | '\r' => try!(parser.parse_error(ParseError::InvalidCharacter)), _ => { try!(check_url_code_point(input, i, c, parser)); - utf8_percent_encode_to(input.slice(i, next_i), + utf8_percent_encode_to(&input[i..next_i], DEFAULT_ENCODE_SET, &mut path_part); } } @@ -571,7 +571,7 @@ fn parse_path<'a>(base_path: &[String], input: &'a str, context: Context, break } } - Ok((path, input.slice_from(end))) + Ok((path, &input[end..])) } @@ -588,12 +588,12 @@ fn parse_scheme_data<'a>(input: &'a str, parser: &UrlParser) '\t' | '\n' | '\r' => try!(parser.parse_error(ParseError::InvalidCharacter)), _ => { try!(check_url_code_point(input, i, c, parser)); - utf8_percent_encode_to(input.slice(i, next_i), + utf8_percent_encode_to(&input[i..next_i], SIMPLE_ENCODE_SET, &mut scheme_data); } } } - Ok((scheme_data, input.slice_from(end))) + Ok((scheme_data, &input[end..])) } @@ -603,10 +603,10 @@ fn parse_query_and_fragment(input: &str, parser: &UrlParser) return Ok((None, None)) } match input.char_at(0) { - '#' => Ok((None, Some(try!(parse_fragment(input.slice_from(1), parser))))), + '#' => Ok((None, Some(try!(parse_fragment(&input[1..], parser))))), '?' => { let (query, remaining) = try!(parse_query( - input.slice_from(1), Context::UrlParser, parser)); + &input[1..], Context::UrlParser, parser)); let fragment = match remaining { Some(remaining) => Some(try!(parse_fragment(remaining, parser))), None => None @@ -626,7 +626,7 @@ pub fn parse_query<'a>(input: &'a str, context: Context, parser: &UrlParser) for (i, c) in input.char_indices() { match c { '#' if context == Context::UrlParser => { - remaining = Some(input.slice_from(i + 1)); + remaining = Some(&input[i + 1..]); break }, '\t' | '\n' | '\r' => try!(parser.parse_error(ParseError::InvalidCharacter)), @@ -649,7 +649,7 @@ pub fn parse_fragment<'a>(input: &'a str, parser: &UrlParser) -> ParseResult try!(parser.parse_error(ParseError::InvalidCharacter)), _ => { try!(check_url_code_point(input, i, c, parser)); - utf8_percent_encode_to(input.slice(i, next_i), + utf8_percent_encode_to(&input[i..next_i], SIMPLE_ENCODE_SET, &mut fragment); } } @@ -740,7 +740,7 @@ impl<'a> Iterator for CharRanges<'a> { fn check_url_code_point(input: &str, i: usize, c: char, parser: &UrlParser) -> ParseResult<()> { if c == '%' { - if !starts_with_2_hex(input.slice_from(i + 1)) { + if !starts_with_2_hex(&input[i + 1..]) { try!(parser.parse_error(ParseError::InvalidPercentEncoded)); } } else if !is_url_code_point(c) { diff --git a/src/punycode.rs b/src/punycode.rs index 8106aaa33..6d1486dde 100644 --- a/src/punycode.rs +++ b/src/punycode.rs @@ -61,8 +61,8 @@ pub fn decode(input: &str) -> Option> { let (mut output, input) = match input.rfind(DELIMITER) { None => (Vec::new(), input), Some(position) => ( - input.slice_to(position).chars().collect(), - if position > 0 { input.slice_from(position + 1) } else { input } + input[..position].chars().collect(), + if position > 0 { &input[position + 1..] } else { input } ) }; let mut code_point = INITIAL_N; diff --git a/src/tests.rs b/src/tests.rs index 82e1dff5b..5a61ab571 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -145,8 +145,8 @@ fn parse_test_data(input: &str) -> Vec { continue } let colon = piece.find(':').unwrap(); - let value = unescape(piece.slice_from(colon + 1)); - match piece.slice_to(colon) { + let value = unescape(&piece[colon + 1..]); + match &piece[..colon] { "s" => test.scheme = Some(value), "u" => test.username = value, "pass" => test.password = Some(value), @@ -207,7 +207,8 @@ fn file_paths() { assert_eq!(Url::from_file_path(&path::windows::Path::new(r"\drive-relative")), Err(())); assert_eq!(Url::from_file_path(&path::windows::Path::new(r"\\ucn\")), Err(())); - let mut url = Url::from_file_path(&path::posix::Path::new("/foo/bar")).unwrap(); + let mut url = Url::from_file_path(&path::posix::Path::new("/foo/bar")) + .unwrap_or_else(|_| panic!("failed to create URL")); assert_eq!(url.host(), Some(&Host::Domain("".to_string()))); assert_eq!(url.path(), Some(["foo".to_string(), "bar".to_string()].as_slice())); assert!(url.to_file_path() == Ok(path::posix::Path::new("/foo/bar"))); @@ -223,7 +224,8 @@ fn file_paths() { assert!(url.to_file_path() == Ok(path::posix::Path::new( /* note: byte string, invalid UTF-8 */ b"/foo/ba\x80r"))); - let mut url = Url::from_file_path(&path::windows::Path::new(r"C:\foo\bar")).unwrap(); + let mut url = Url::from_file_path(&path::windows::Path::new(r"C:\foo\bar")) + .unwrap_or_else(|_| panic!("failed to create URL")); assert_eq!(url.host(), Some(&Host::Domain("".to_string()))); assert_eq!(url.path(), Some(["C:".to_string(), "foo".to_string(), "bar".to_string()].as_slice())); assert!(url.to_file_path::() @@ -250,11 +252,13 @@ fn directory_paths() { assert_eq!(Url::from_directory_path(&path::windows::Path::new(r"\drive-relative")), Err(())); assert_eq!(Url::from_directory_path(&path::windows::Path::new(r"\\ucn\")), Err(())); - let url = Url::from_directory_path(&path::posix::Path::new("/foo/bar")).unwrap(); + let url = Url::from_directory_path(&path::posix::Path::new("/foo/bar")) + .unwrap_or_else(|_| panic!("failed to create URL")); assert_eq!(url.host(), Some(&Host::Domain("".to_string()))); assert_eq!(url.path(), Some(["foo".to_string(), "bar".to_string(), "".to_string()].as_slice())); - let url = Url::from_directory_path(&path::windows::Path::new(r"C:\foo\bar")).unwrap(); + let url = Url::from_directory_path(&path::windows::Path::new(r"C:\foo\bar")) + .unwrap_or_else(|_| panic!("failed to create URL")); assert_eq!(url.host(), Some(&Host::Domain("".to_string()))); assert_eq!(url.path(), Some([ "C:".to_string(), "foo".to_string(), "bar".to_string(), "".to_string()].as_slice())); diff --git a/src/urlutils.rs b/src/urlutils.rs index a70ebeb6f..43a890f4d 100644 --- a/src/urlutils.rs +++ b/src/urlutils.rs @@ -139,7 +139,7 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> { self.url.query = if input.is_empty() { None } else { - let input = if input.starts_with("?") { input.slice_from(1) } else { input }; + let input = if input.starts_with("?") { &input[1..] } else { input }; let (new_query, _) = try!(::parser::parse_query( input, Context::Setter, self.parser)); Some(new_query) @@ -155,7 +155,7 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> { self.url.fragment = if input.is_empty() { None } else { - let input = if input.starts_with("#") { input.slice_from(1) } else { input }; + let input = if input.starts_with("#") { &input[1..] } else { input }; Some(try!(::parser::parse_fragment(input, self.parser))) }; Ok(())