slicing.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2016 The rust-url developers.
  2. //
  3. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  4. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  5. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  6. // option. This file may not be copied, modified, or distributed
  7. // except according to those terms.
  8. use std::ops::{Range, RangeFrom, RangeTo, RangeFull, Index};
  9. use Url;
  10. impl Index<RangeFull> for Url {
  11. type Output = str;
  12. fn index(&self, _: RangeFull) -> &str {
  13. &self.serialization
  14. }
  15. }
  16. impl Index<RangeFrom<Position>> for Url {
  17. type Output = str;
  18. fn index(&self, range: RangeFrom<Position>) -> &str {
  19. &self.serialization[self.index(range.start)..]
  20. }
  21. }
  22. impl Index<RangeTo<Position>> for Url {
  23. type Output = str;
  24. fn index(&self, range: RangeTo<Position>) -> &str {
  25. &self.serialization[..self.index(range.end)]
  26. }
  27. }
  28. impl Index<Range<Position>> for Url {
  29. type Output = str;
  30. fn index(&self, range: Range<Position>) -> &str {
  31. &self.serialization[self.index(range.start)..self.index(range.end)]
  32. }
  33. }
  34. /// Indicates a position within a URL based on its components.
  35. ///
  36. /// A range of positions can be used for slicing `Url`:
  37. ///
  38. /// ```rust
  39. /// # use url::{Url, Position};
  40. /// # fn something(some_url: Url) {
  41. /// let serialization: &str = &some_url[..];
  42. /// let serialization_without_fragment: &str = &some_url[..Position::AfterQuery];
  43. /// let authority: &str = &some_url[Position::BeforeUsername..Position::AfterPort];
  44. /// let data_url_payload: &str = &some_url[Position::BeforePath..Position::AfterQuery];
  45. /// let scheme_relative: &str = &some_url[Position::BeforeUsername..];
  46. /// # }
  47. /// ```
  48. ///
  49. /// In a pseudo-grammar (where `[`…`]?` makes a sub-sequence optional),
  50. /// URL components and delimiters that separate them are:
  51. ///
  52. /// ```notrust
  53. /// url =
  54. /// scheme ":"
  55. /// [ "//" [ username [ ":" password ]? "@" ]? host [ ":" port ]? ]?
  56. /// path [ "?" query ]? [ "#" fragment ]?
  57. /// ```
  58. ///
  59. /// When a given component is not present,
  60. /// its "before" and "after" position are the same
  61. /// (so that `&some_url[BeforeFoo..AfterFoo]` is the empty string)
  62. /// and component ordering is preserved
  63. /// (so that a missing query "is between" a path and a fragment).
  64. ///
  65. /// The end of a component and the start of the next are either the same or separate
  66. /// by a delimiter.
  67. /// (Not that the initial `/` of a path is considered part of the path here, not a delimiter.)
  68. /// For example, `&url[..BeforeFragment]` would include a `#` delimiter (if present in `url`),
  69. /// so `&url[..AfterQuery]` might be desired instead.
  70. ///
  71. /// `BeforeScheme` and `AfterFragment` are always the start and end of the entire URL,
  72. /// so `&url[BeforeScheme..X]` is the same as `&url[..X]`
  73. /// and `&url[X..AfterFragment]` is the same as `&url[X..]`.
  74. #[derive(Copy, Clone, Debug)]
  75. pub enum Position {
  76. BeforeScheme,
  77. AfterScheme,
  78. BeforeUsername,
  79. AfterUsername,
  80. BeforePassword,
  81. AfterPassword,
  82. BeforeHost,
  83. AfterHost,
  84. BeforePort,
  85. AfterPort,
  86. BeforePath,
  87. AfterPath,
  88. BeforeQuery,
  89. AfterQuery,
  90. BeforeFragment,
  91. AfterFragment
  92. }
  93. impl Url {
  94. #[inline]
  95. fn index(&self, position: Position) -> usize {
  96. match position {
  97. Position::BeforeScheme => 0,
  98. Position::AfterScheme => self.scheme_end as usize,
  99. Position::BeforeUsername => if self.has_authority() {
  100. self.scheme_end as usize + "://".len()
  101. } else {
  102. debug_assert!(self.byte_at(self.scheme_end) == b':');
  103. debug_assert!(self.scheme_end + ":".len() as u32 == self.username_end);
  104. self.scheme_end as usize + ":".len()
  105. },
  106. Position::AfterUsername => self.username_end as usize,
  107. Position::BeforePassword => if self.has_authority() &&
  108. self.byte_at(self.username_end) == b':' {
  109. self.username_end as usize + ":".len()
  110. } else {
  111. debug_assert!(self.username_end == self.host_start);
  112. self.username_end as usize
  113. },
  114. Position::AfterPassword => if self.has_authority() &&
  115. self.byte_at(self.username_end) == b':' {
  116. debug_assert!(self.byte_at(self.host_start - "@".len() as u32) == b'@');
  117. self.host_start as usize - "@".len()
  118. } else {
  119. debug_assert!(self.username_end == self.host_start);
  120. self.host_start as usize
  121. },
  122. Position::BeforeHost => self.host_start as usize,
  123. Position::AfterHost => self.host_end as usize,
  124. Position::BeforePort => if self.port.is_some() {
  125. debug_assert!(self.byte_at(self.host_end) == b':');
  126. self.host_end as usize + ":".len()
  127. } else {
  128. self.host_end as usize
  129. },
  130. Position::AfterPort => self.path_start as usize,
  131. Position::BeforePath => self.path_start as usize,
  132. Position::AfterPath => match (self.query_start, self.fragment_start) {
  133. (Some(q), _) => q as usize,
  134. (None, Some(f)) => f as usize,
  135. (None, None) => self.serialization.len(),
  136. },
  137. Position::BeforeQuery => match (self.query_start, self.fragment_start) {
  138. (Some(q), _) => {
  139. debug_assert!(self.byte_at(q) == b'?');
  140. q as usize + "?".len()
  141. }
  142. (None, Some(f)) => f as usize,
  143. (None, None) => self.serialization.len(),
  144. },
  145. Position::AfterQuery => match self.fragment_start {
  146. None => self.serialization.len(),
  147. Some(f) => f as usize,
  148. },
  149. Position::BeforeFragment => match self.fragment_start {
  150. Some(f) => {
  151. debug_assert!(self.byte_at(f) == b'#');
  152. f as usize + "#".len()
  153. }
  154. None => self.serialization.len(),
  155. },
  156. Position::AfterFragment => self.serialization.len(),
  157. }
  158. }
  159. }