Просмотр исходного кода

Remove Url::has_host

Use .host().is_some() instead.
Simon Sapin 10 лет назад
Родитель
Сommit
f59870f5d6
2 измененных файлов с 7 добавлено и 15 удалено
  1. 4 12
      src/lib.rs
  2. 3 3
      src/slicing.rs

+ 4 - 12
src/lib.rs

@@ -232,7 +232,7 @@ impl Url {
     /// Return the password for this URL, if any, as a percent-encoded ASCII string.
     pub fn password(&self) -> Option<&str> {
         if self.byte_at(self.username_end) == b':' {
-            debug_assert!(self.has_host());
+            debug_assert!(self.host().is_some());
             debug_assert!(self.byte_at(self.host_start - 1) == b'@');
             Some(self.slice(self.username_end + 1..self.host_start - 1))
         } else {
@@ -240,14 +240,6 @@ impl Url {
         }
     }
 
-    /// Return whether this URL has a host.
-    ///
-    /// Non-relative URLs (typical of `data:` and `mailto:`) and some `file:` URLs don’
-    #[inline]
-    pub fn has_host(&self) -> bool {
-        !matches!(self.host, HostInternal::None)
-    }
-
     /// Return the string representation of the host (domain or IP address) for this URL, if any.
     /// Non-ASCII domains are punycode-encoded per IDNA.
     ///
@@ -256,10 +248,10 @@ impl Url {
     ///
     /// See also the `host` method.
     pub fn host_str(&self) -> Option<&str> {
-        if self.has_host() {
-            Some(self.slice(self.host_start..self.host_end))
-        } else {
+        if matches!(self.host, HostInternal::None) {
             None
+        } else {
+            Some(self.slice(self.host_start..self.host_end))
         }
     }
 

+ 3 - 3
src/slicing.rs

@@ -58,7 +58,7 @@ impl Index<Range<Position>> for Url {
 /// ```notrust
 /// url =
 ///     scheme ":"
-///     [ "//" [ username [ ":" password ]? "@" ]? host [ ":" port ]? ]
+///     [ "//" [ username [ ":" password ]? "@" ]? host [ ":" port ]? ]?
 ///     path [ "?" query ]? [ "#" fragment ]?
 /// ```
 ///
@@ -116,7 +116,7 @@ impl Url {
             Position::AfterUsername => self.username_end as usize,
 
             Position::BeforePassword => if self.port.is_some() {
-                debug_assert!(self.has_host());
+                debug_assert!(self.host().is_some());
                 debug_assert!(self.byte_at(self.username_end) == b':');
                 self.username_end as usize + ":".len()
             } else {
@@ -125,7 +125,7 @@ impl Url {
             },
 
             Position::AfterPassword => if self.port.is_some() {
-                debug_assert!(self.has_host());
+                debug_assert!(self.host().is_some());
                 debug_assert!(self.byte_at(self.username_end) == b':');
                 debug_assert!(self.byte_at(self.host_start - "@".len() as u32) == b'@');
                 self.host_start as usize - "@".len()