Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-10 09:13:12

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
0004 //
0005 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 // Official repository: https://github.com/boostorg/url
0009 //
0010 
0011 #ifndef BOOST_URL_SEGMENTS_VIEW_HPP
0012 #define BOOST_URL_SEGMENTS_VIEW_HPP
0013 
0014 #include <boost/url/detail/config.hpp>
0015 #include <boost/url/segments_base.hpp>
0016 #include <boost/core/detail/string_view.hpp>
0017 
0018 namespace boost {
0019 namespace urls {
0020 
0021 /** A view representing path segments in a URL
0022 
0023     Objects of this type are used to interpret
0024     the path as a bidirectional view of segment
0025     strings.
0026 
0027     The view does not retain ownership of the
0028     elements and instead references the original
0029     character buffer. The caller is responsible
0030     for ensuring that the lifetime of the buffer
0031     extends until it is no longer referenced.
0032 
0033     @par Example
0034     @code
0035     url_view u( "/path/to/file.txt" );
0036 
0037     segments_view ps = u.segments();
0038 
0039     assert( ps.buffer().data() == u.buffer().data() );
0040     @endcode
0041 
0042     Percent escapes in strings returned when
0043     dereferencing iterators are automatically
0044     decoded.
0045 
0046     @par Iterator Invalidation
0047     Changes to the underlying character buffer
0048     can invalidate iterators which reference it.
0049 
0050     @see
0051         @ref segments_encoded_view,
0052         @ref segments_encoded_ref,
0053         @ref segments_ref.
0054 */
0055 class segments_view
0056     : public segments_base
0057 {
0058     friend class url_view_base;
0059     friend class segments_encoded_view;
0060     friend class segments_ref;
0061 
0062     segments_view(
0063         detail::path_ref const& ref) noexcept;
0064 
0065 public:
0066     /** Constructor
0067 
0068         Default-constructed segments have
0069         zero elements.
0070 
0071         @par Example
0072         @code
0073         segments_view ps;
0074         @endcode
0075 
0076         @par Effects
0077         @code
0078         return segments_view( "" );
0079         @endcode
0080 
0081         @par Complexity
0082         Constant.
0083 
0084         @par Exception Safety
0085         Throws nothing.
0086     */
0087     segments_view() = default;
0088 
0089     /** Constructor
0090 
0091         After construction, viewss
0092         reference the same underlying character
0093         buffer.
0094 
0095         Ownership is not transferred; the caller
0096         is responsible for ensuring the lifetime
0097         of the buffer extends until it is no
0098         longer referenced.
0099 
0100         @par Postconditions
0101         @code
0102         this->buffer().data() == other.buffer().data()
0103         @endcode
0104 
0105         @par Complexity
0106         Constant
0107 
0108         @par Exception Safety
0109         Throws nothing
0110 
0111         @param other The other view.
0112     */
0113     segments_view(
0114         segments_view const& other) = default;
0115 
0116     /** Constructor
0117 
0118         This function constructs segments from
0119         a valid path string, which can contain
0120         percent escapes.
0121         Upon construction, the view references
0122         the character buffer pointed to by `s`.
0123         caller is responsible for ensuring
0124         that the lifetime of the buffer
0125         extends until the view is destroyed.
0126 
0127         @par Example
0128         @code
0129         segments_view ps( "/path/to/file.txt" );
0130         @endcode
0131 
0132         @par Effects
0133         @code
0134         return parse_path( s ).value();
0135         @endcode
0136 
0137         @par Postconditions
0138         @code
0139         this->buffer().data() == s.data()
0140         @endcode
0141 
0142         @par Complexity
0143         Linear in `s`.
0144 
0145         @par Exception Safety
0146         Exceptions thrown on invalid input.
0147 
0148         @throw system_error
0149         `s` contains an invalid path.
0150 
0151         @param s The string to parse.
0152 
0153         @par BNF
0154         @code
0155         path = [ "/" ] [ segment *( "/" segment ) ]
0156 
0157         segment = *pchar
0158         @endcode
0159 
0160         @par Specification
0161         @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3"
0162             >3.3.  Path</a>
0163     */
0164     BOOST_URL_DECL
0165     segments_view(
0166         core::string_view s);
0167 
0168     /** Constructor
0169 
0170         This function creates a new @ref segments_view
0171         from a pair of iterators referring to
0172         elements of another segments view. The
0173         resulting view references the same
0174         underlying character buffer as the
0175         original.
0176 
0177         The constructed view preserves the
0178         original absolute flag when `first`
0179         selects the first segment and otherwise
0180         produces an absolute subview: if the
0181         source path is relative and `first ==
0182         ps.begin()` the new view is relative,
0183         and in every other case the subview is
0184         absolute with the separator immediately
0185         preceding `*first` retained at the front.
0186         This ensures the underlying text can be
0187         reconstructed by concatenating the buffers
0188         of adjacent subviews.
0189 
0190         The caller is responsible for ensuring
0191         that the lifetime of the original buffer
0192         extends until the constructed view is no
0193         longer referenced.
0194 
0195         @par Example
0196         @code
0197         segments_view ps( "/path/to/file.txt" );
0198 
0199         segments_view sub(
0200             std::next(ps.begin()),
0201             ps.end());
0202 
0203         segments_view first_half(
0204             ps.begin(),
0205             std::next(ps.begin()));
0206 
0207         // sub represents "/to/file.txt"
0208         std::string combined(first_half.buffer());
0209         combined.append(sub.buffer());
0210         BOOST_ASSERT(combined == ps.buffer());
0211         @endcode
0212 
0213         @par Preconditions
0214         The iterators must be valid and belong to
0215         the same @ref segments_view.
0216 
0217         @par Postconditions
0218         `sub.buffer()` references characters in the
0219         original `ps.buffer()`.
0220 
0221         @par Complexity
0222         Constant
0223 
0224         @par Exception Safety
0225         Throws nothing.
0226 
0227         @param first The beginning iterator.
0228         @param last The ending iterator.
0229     */
0230     BOOST_URL_DECL
0231     segments_view(
0232         iterator first,
0233         iterator last) noexcept;
0234 
0235     /** Assignment
0236 
0237         After assignment, both views
0238         reference the same underlying character
0239         buffer.
0240 
0241         Ownership is not transferred; the caller
0242         is responsible for ensuring the lifetime
0243         of the buffer extends until it is no
0244         longer referenced.
0245 
0246         @par Postconditions
0247         @code
0248         this->buffer().data() == other.buffer().data()
0249         @endcode
0250 
0251         @par Complexity
0252         Constant
0253 
0254         @par Exception Safety
0255         Throws nothing
0256 
0257         @param other The other view.
0258         @return A reference to this object.
0259     */
0260     segments_view&
0261     operator=(segments_view const& other) = default;
0262 };
0263 
0264 } // urls
0265 } // boost
0266 
0267 #endif