slicing.rs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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::{Index, Range, RangeFrom, RangeFull, RangeTo};
  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 => {
  100. if self.has_authority() {
  101. self.scheme_end as usize + "://".len()
  102. } else {
  103. debug_assert!(self.byte_at(self.scheme_end) == b':');
  104. debug_assert!(self.scheme_end + ":".len() as u32 == self.username_end);
  105. self.scheme_end as usize + ":".len()
  106. }
  107. }
  108. Position::AfterUsername => self.username_end as usize,
  109. Position::BeforePassword => {
  110. if self.has_authority() && self.byte_at(self.username_end) == b':' {
  111. self.username_end as usize + ":".len()
  112. } else {
  113. debug_assert!(self.username_end == self.host_start);
  114. self.username_end as usize
  115. }
  116. }
  117. Position::AfterPassword => {
  118. if self.has_authority() && self.byte_at(self.username_end) == b':' {
  119. debug_assert!(self.byte_at(self.host_start - "@".len() as u32) == b'@');
  120. self.host_start as usize - "@".len()
  121. } else {
  122. debug_assert!(self.username_end == self.host_start);
  123. self.host_start as usize
  124. }
  125. }
  126. Position::BeforeHost => self.host_start as usize,
  127. Position::AfterHost => self.host_end as usize,
  128. Position::BeforePort => {
  129. if self.port.is_some() {
  130. debug_assert!(self.byte_at(self.host_end) == b':');
  131. self.host_end as usize + ":".len()
  132. } else {
  133. self.host_end as usize
  134. }
  135. }
  136. Position::AfterPort => self.path_start as usize,
  137. Position::BeforePath => self.path_start as usize,
  138. Position::AfterPath => match (self.query_start, self.fragment_start) {
  139. (Some(q), _) => q as usize,
  140. (None, Some(f)) => f as usize,
  141. (None, None) => self.serialization.len(),
  142. },
  143. Position::BeforeQuery => match (self.query_start, self.fragment_start) {
  144. (Some(q), _) => {
  145. debug_assert!(self.byte_at(q) == b'?');
  146. q as usize + "?".len()
  147. }
  148. (None, Some(f)) => f as usize,
  149. (None, None) => self.serialization.len(),
  150. },
  151. Position::AfterQuery => match self.fragment_start {
  152. None => self.serialization.len(),
  153. Some(f) => f as usize,
  154. },
  155. Position::BeforeFragment => match self.fragment_start {
  156. Some(f) => {
  157. debug_assert!(self.byte_at(f) == b'#');
  158. f as usize + "#".len()
  159. }
  160. None => self.serialization.len(),
  161. },
  162. Position::AfterFragment => self.serialization.len(),
  163. }
  164. }
  165. }