slicing.rs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 core::ops::{Index, Range, RangeFrom, RangeFull, RangeTo};
  9. use crate::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. // Counts how many base-10 digits are required to represent n in the given base
  35. fn count_digits(n: u16) -> usize {
  36. match n {
  37. 0..=9 => 1,
  38. 10..=99 => 2,
  39. 100..=999 => 3,
  40. 1000..=9999 => 4,
  41. 10000..=65535 => 5,
  42. }
  43. }
  44. #[test]
  45. fn test_count_digits() {
  46. assert_eq!(count_digits(0), 1);
  47. assert_eq!(count_digits(1), 1);
  48. assert_eq!(count_digits(9), 1);
  49. assert_eq!(count_digits(10), 2);
  50. assert_eq!(count_digits(99), 2);
  51. assert_eq!(count_digits(100), 3);
  52. assert_eq!(count_digits(9999), 4);
  53. assert_eq!(count_digits(65535), 5);
  54. }
  55. /// Indicates a position within a URL based on its components.
  56. ///
  57. /// A range of positions can be used for slicing `Url`:
  58. ///
  59. /// ```rust
  60. /// # use url::{Url, Position};
  61. /// # fn something(some_url: Url) {
  62. /// let serialization: &str = &some_url[..];
  63. /// let serialization_without_fragment: &str = &some_url[..Position::AfterQuery];
  64. /// let authority: &str = &some_url[Position::BeforeUsername..Position::AfterPort];
  65. /// let data_url_payload: &str = &some_url[Position::BeforePath..Position::AfterQuery];
  66. /// let scheme_relative: &str = &some_url[Position::BeforeUsername..];
  67. /// # }
  68. /// ```
  69. ///
  70. /// In a pseudo-grammar (where `[`…`]?` makes a sub-sequence optional),
  71. /// URL components and delimiters that separate them are:
  72. ///
  73. /// ```notrust
  74. /// url =
  75. /// scheme ":"
  76. /// [ "//" [ username [ ":" password ]? "@" ]? host [ ":" port ]? ]?
  77. /// path [ "?" query ]? [ "#" fragment ]?
  78. /// ```
  79. ///
  80. /// When a given component is not present,
  81. /// its "before" and "after" position are the same
  82. /// (so that `&some_url[BeforeFoo..AfterFoo]` is the empty string)
  83. /// and component ordering is preserved
  84. /// (so that a missing query "is between" a path and a fragment).
  85. ///
  86. /// The end of a component and the start of the next are either the same or separate
  87. /// by a delimiter.
  88. /// (Note that the initial `/` of a path is considered part of the path here, not a delimiter.)
  89. /// For example, `&url[..BeforeFragment]` would include a `#` delimiter (if present in `url`),
  90. /// so `&url[..AfterQuery]` might be desired instead.
  91. ///
  92. /// `BeforeScheme` and `AfterFragment` are always the start and end of the entire URL,
  93. /// so `&url[BeforeScheme..X]` is the same as `&url[..X]`
  94. /// and `&url[X..AfterFragment]` is the same as `&url[X..]`.
  95. #[derive(Copy, Clone, Debug)]
  96. pub enum Position {
  97. BeforeScheme,
  98. AfterScheme,
  99. BeforeUsername,
  100. AfterUsername,
  101. BeforePassword,
  102. AfterPassword,
  103. BeforeHost,
  104. AfterHost,
  105. BeforePort,
  106. AfterPort,
  107. BeforePath,
  108. AfterPath,
  109. BeforeQuery,
  110. AfterQuery,
  111. BeforeFragment,
  112. AfterFragment,
  113. }
  114. impl Url {
  115. #[inline]
  116. fn index(&self, position: Position) -> usize {
  117. match position {
  118. Position::BeforeScheme => 0,
  119. Position::AfterScheme => self.scheme_end as usize,
  120. Position::BeforeUsername => {
  121. if self.has_authority() {
  122. self.scheme_end as usize + "://".len()
  123. } else {
  124. debug_assert!(self.byte_at(self.scheme_end) == b':');
  125. debug_assert!(self.scheme_end + ":".len() as u32 == self.username_end);
  126. self.scheme_end as usize + ":".len()
  127. }
  128. }
  129. Position::AfterUsername => self.username_end as usize,
  130. Position::BeforePassword => {
  131. if self.has_authority() && self.byte_at(self.username_end) == b':' {
  132. self.username_end as usize + ":".len()
  133. } else {
  134. debug_assert!(self.username_end == self.host_start);
  135. self.username_end as usize
  136. }
  137. }
  138. Position::AfterPassword => {
  139. if self.has_authority() && self.byte_at(self.username_end) == b':' {
  140. debug_assert!(self.byte_at(self.host_start - "@".len() as u32) == b'@');
  141. self.host_start as usize - "@".len()
  142. } else {
  143. debug_assert!(self.username_end == self.host_start);
  144. self.host_start as usize
  145. }
  146. }
  147. Position::BeforeHost => self.host_start as usize,
  148. Position::AfterHost => self.host_end as usize,
  149. Position::BeforePort => {
  150. if self.port.is_some() {
  151. debug_assert!(self.byte_at(self.host_end) == b':');
  152. self.host_end as usize + ":".len()
  153. } else {
  154. self.host_end as usize
  155. }
  156. }
  157. Position::AfterPort => {
  158. if let Some(port) = self.port {
  159. debug_assert!(self.byte_at(self.host_end) == b':');
  160. self.host_end as usize + ":".len() + count_digits(port)
  161. } else {
  162. self.host_end as usize
  163. }
  164. }
  165. Position::BeforePath => self.path_start as usize,
  166. Position::AfterPath => match (self.query_start, self.fragment_start) {
  167. (Some(q), _) => q as usize,
  168. (None, Some(f)) => f as usize,
  169. (None, None) => self.serialization.len(),
  170. },
  171. Position::BeforeQuery => match (self.query_start, self.fragment_start) {
  172. (Some(q), _) => {
  173. debug_assert!(self.byte_at(q) == b'?');
  174. q as usize + "?".len()
  175. }
  176. (None, Some(f)) => f as usize,
  177. (None, None) => self.serialization.len(),
  178. },
  179. Position::AfterQuery => match self.fragment_start {
  180. None => self.serialization.len(),
  181. Some(f) => f as usize,
  182. },
  183. Position::BeforeFragment => match self.fragment_start {
  184. Some(f) => {
  185. debug_assert!(self.byte_at(f) == b'#');
  186. f as usize + "#".len()
  187. }
  188. None => self.serialization.len(),
  189. },
  190. Position::AfterFragment => self.serialization.len(),
  191. }
  192. }
  193. }