segments_view.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_VIEW_HPP
  11. #define BOOST_URL_SEGMENTS_VIEW_HPP
  12. #include <boost/url/detail/config.hpp>
  13. #include <boost/url/segments_base.hpp>
  14. #include <boost/core/detail/string_view.hpp>
  15. namespace boost {
  16. namespace urls {
  17. /** A view representing path segments in a URL
  18. Objects of this type are used to interpret
  19. the path as a bidirectional view of segment
  20. strings.
  21. The view does not retain ownership of the
  22. elements and instead references the original
  23. character buffer. The caller is responsible
  24. for ensuring that the lifetime of the buffer
  25. extends until it is no longer referenced.
  26. @par Example
  27. @code
  28. url_view u( "/path/to/file.txt" );
  29. segments_view ps = u.segments();
  30. assert( ps.buffer().data() == u.buffer().data() );
  31. @endcode
  32. Percent escapes in strings returned when
  33. dereferencing iterators are automatically
  34. decoded.
  35. @par Iterator Invalidation
  36. Changes to the underlying character buffer
  37. can invalidate iterators which reference it.
  38. @see
  39. @ref segments_encoded_view,
  40. @ref segments_encoded_ref,
  41. @ref segments_ref.
  42. */
  43. class segments_view
  44. : public segments_base
  45. {
  46. friend class url_view_base;
  47. friend class segments_encoded_view;
  48. friend class segments_ref;
  49. segments_view(
  50. detail::path_ref const& ref) noexcept;
  51. public:
  52. /** Constructor
  53. Default-constructed segments have
  54. zero elements.
  55. @par Example
  56. @code
  57. segments_view ps;
  58. @endcode
  59. @par Effects
  60. @code
  61. return segments_view( "" );
  62. @endcode
  63. @par Complexity
  64. Constant.
  65. @par Exception Safety
  66. Throws nothing.
  67. */
  68. segments_view() = default;
  69. /** Constructor
  70. After construction, viewss
  71. reference the same underlying character
  72. buffer.
  73. Ownership is not transferred; the caller
  74. is responsible for ensuring the lifetime
  75. of the buffer extends until it is no
  76. longer referenced.
  77. @par Postconditions
  78. @code
  79. this->buffer().data() == other.buffer().data()
  80. @endcode
  81. @par Complexity
  82. Constant
  83. @par Exception Safety
  84. Throws nothing
  85. */
  86. segments_view(
  87. segments_view const& other) = default;
  88. /** Constructor
  89. This function constructs segments from
  90. a valid path string, which can contain
  91. percent escapes.
  92. Upon construction, the view references
  93. the character buffer pointed to by `s`.
  94. caller is responsible for ensuring
  95. that the lifetime of the buffer
  96. extends until the view is destroyed.
  97. @par Example
  98. @code
  99. segments_view ps( "/path/to/file.txt" );
  100. @endcode
  101. @par Effects
  102. @code
  103. return parse_path( s ).value();
  104. @endcode
  105. @par Postconditions
  106. @code
  107. this->buffer().data() == s.data()
  108. @endcode
  109. @par Complexity
  110. Linear in `s`.
  111. @par Exception Safety
  112. Exceptions thrown on invalid input.
  113. @throw system_error
  114. `s` contains an invalid path.
  115. @param s The string to parse.
  116. @par BNF
  117. @code
  118. path = [ "/" ] [ segment *( "/" segment ) ]
  119. segment = *pchar
  120. @endcode
  121. @par Specification
  122. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3"
  123. >3.3. Path</a>
  124. */
  125. BOOST_URL_DECL
  126. segments_view(
  127. core::string_view s);
  128. /** Assignment
  129. After assignment, both views
  130. reference the same underlying character
  131. buffer.
  132. Ownership is not transferred; the caller
  133. is responsible for ensuring the lifetime
  134. of the buffer extends until it is no
  135. longer referenced.
  136. @par Postconditions
  137. @code
  138. this->buffer().data() == other.buffer().data()
  139. @endcode
  140. @par Complexity
  141. Constant
  142. @par Exception Safety
  143. Throws nothing
  144. */
  145. segments_view&
  146. operator=(segments_view const& other) = default;
  147. };
  148. } // urls
  149. } // boost
  150. #endif