segments_encoded_view.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  3. // Copyright (c) 2022 Alan de Freitas ([email protected])
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/url
  9. //
  10. #ifndef BOOST_URL_SEGMENTS_ENCODED_VIEW_HPP
  11. #define BOOST_URL_SEGMENTS_ENCODED_VIEW_HPP
  12. #include <boost/url/detail/config.hpp>
  13. #include <boost/url/error_types.hpp>
  14. #include <boost/url/segments_encoded_base.hpp>
  15. #include <boost/url/segments_view.hpp>
  16. #include <boost/core/detail/string_view.hpp>
  17. #include <iosfwd>
  18. #include <utility>
  19. namespace boost {
  20. namespace urls {
  21. /** A view representing path segments in a URL
  22. Objects of this type are used to interpret
  23. the path as a bidirectional view of segment
  24. strings.
  25. The view does not retain ownership of the
  26. elements and instead references the original
  27. character buffer. The caller is responsible
  28. for ensuring that the lifetime of the buffer
  29. extends until it is no longer referenced.
  30. @par Example
  31. @code
  32. url_view u( "/path/to/file.txt" );
  33. segments_encoded_view ps = u.encoded_segments();
  34. assert( ps.buffer().data() == u.buffer().data() );
  35. @endcode
  36. Strings produced when elements are returned
  37. have type @ref param_pct_view and represent
  38. encoded strings. Strings passed to member
  39. functions may contain percent escapes, and
  40. throw exceptions on invalid inputs.
  41. @par Iterator Invalidation
  42. Changes to the underlying character buffer
  43. can invalidate iterators which reference it.
  44. @see
  45. @ref segments_view,
  46. @ref segments_encoded_ref,
  47. @ref segments_ref.
  48. */
  49. class segments_encoded_view
  50. : public segments_encoded_base
  51. {
  52. friend class url_view_base;
  53. friend class segments_encoded_ref;
  54. segments_encoded_view(
  55. detail::path_ref const& ref) noexcept;
  56. public:
  57. /** Constructor
  58. Default-constructed segments have
  59. zero elements.
  60. @par Example
  61. @code
  62. segments_encoded_view ps;
  63. @endcode
  64. @par Effects
  65. @code
  66. return segments_encoded_view( "" );
  67. @endcode
  68. @par Complexity
  69. Constant.
  70. @par Exception Safety
  71. Throws nothing.
  72. */
  73. segments_encoded_view() = default;
  74. /** Constructor
  75. After construction, both views
  76. reference the same character buffer.
  77. Ownership is not transferred; the caller
  78. is responsible for ensuring the lifetime
  79. of the buffer extends until it is no
  80. longer referenced.
  81. @par Postconditions
  82. @code
  83. this->buffer().data() == other.buffer().data()
  84. @endcode
  85. @par Complexity
  86. Constant.
  87. @par Exception Safety
  88. Throws nothing
  89. */
  90. segments_encoded_view(
  91. segments_encoded_view const&) noexcept = default;
  92. /** Constructor
  93. This function constructs segments from
  94. a valid path string, which can contain
  95. percent escapes.
  96. Upon construction, the view references
  97. the character buffer pointed to by `s`.
  98. caller is responsible for ensuring
  99. that the lifetime of the buffer
  100. extends until the view is destroyed.
  101. @par Example
  102. @code
  103. segments_encoded_view ps( "/path/to/file.txt" );
  104. @endcode
  105. @par Effects
  106. @code
  107. return parse_path( s ).value();
  108. @endcode
  109. @par Postconditions
  110. @code
  111. this->buffer().data() == s.data()
  112. @endcode
  113. @par Complexity
  114. Linear in `s`.
  115. @par Exception Safety
  116. Exceptions thrown on invalid input.
  117. @throw system_error
  118. `s` contains an invalid path.
  119. @param s The string to parse.
  120. @par BNF
  121. @code
  122. path = [ "/" ] [ segment *( "/" segment ) ]
  123. segment = *pchar
  124. @endcode
  125. @par Specification
  126. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3"
  127. >3.3. Path</a>
  128. */
  129. BOOST_URL_DECL
  130. segments_encoded_view(
  131. core::string_view s);
  132. /** Assignment
  133. After assignment, both views
  134. reference the same underlying character
  135. buffer.
  136. Ownership is not transferred; the caller
  137. is responsible for ensuring the lifetime
  138. of the buffer extends until it is no
  139. longer referenced.
  140. @par Postconditions
  141. @code
  142. this->buffer().data() == other.buffer().data()
  143. @endcode
  144. @par Complexity
  145. Constant
  146. @par Exception Safety
  147. Throws nothing
  148. */
  149. segments_encoded_view&
  150. operator=(
  151. segments_encoded_view const&) = default;
  152. /** Conversion
  153. This conversion returns a new view which
  154. references the same underlying character
  155. buffer, and whose iterators and members
  156. return ordinary strings with decoding
  157. applied to any percent escapes.
  158. Ownership is not transferred; the caller
  159. is responsible for ensuring the lifetime
  160. of the buffer extends until it is no
  161. longer referenced.
  162. @par Example
  163. @code
  164. segments_view ps = parse_path( "/path/to/file.txt" ).value();
  165. @endcode
  166. @par Postconditions
  167. @code
  168. segments_view( *this ).buffer().data() == this->buffer().data()
  169. @endcode
  170. @par Complexity
  171. Constant
  172. @par Exception Safety
  173. Throws nothing
  174. */
  175. BOOST_URL_DECL
  176. operator
  177. segments_view() const noexcept;
  178. //--------------------------------------------
  179. BOOST_URL_DECL
  180. friend
  181. system::result<segments_encoded_view>
  182. parse_path(core::string_view s) noexcept;
  183. };
  184. } // urls
  185. } // boost
  186. #endif