123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743 |
- //
- // Copyright (c) 2019 Vinnie Falco ([email protected])
- // Copyright (c) 2022 Alan de Freitas ([email protected])
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- // Official repository: https://github.com/boostorg/url
- //
- #ifndef BOOST_URL_SEGMENTS_REF_HPP
- #define BOOST_URL_SEGMENTS_REF_HPP
- #include <boost/url/detail/config.hpp>
- #include <boost/url/segments_base.hpp>
- #include <initializer_list>
- #include <iterator>
- namespace boost {
- namespace urls {
- #ifndef BOOST_URL_DOCS
- class url_base;
- class segments_view;
- #endif
- /** A view representing path segments in a URL
- Objects of this type are used to interpret
- the path as a bidirectional view of segments,
- where each segment is a string with percent
- escapes automatically decoded.
- The view does not retain ownership of the
- elements and instead references the original
- character buffer. The caller is responsible
- for ensuring that the lifetime of the buffer
- extends until it is no longer referenced.
- The view is modifiable; calling non-const
- members causes changes to the referenced
- url.
- @par Example
- @code
- url u( "/path/to/file.txt" );
- segments_ref ps = u.segments();
- @endcode
- Percent escapes in strings returned when
- dereferencing iterators are automatically
- decoded.
- Reserved characters in strings supplied
- to modifier functions are automatically
- percent-escaped.
- @par Iterator Invalidation
- Changes to the underlying character buffer
- can invalidate iterators which reference it.
- Modifications made through the container
- invalidate some or all iterators:
- <br>
- @li @ref push_back : Only `end()`.
- @li @ref assign, @ref clear,
- @ref operator= : All elements.
- @li @ref erase : Erased elements and all
- elements after (including `end()`).
- @li @ref insert : All elements at or after
- the insertion point (including `end()`).
- @li @ref replace : Modified
- elements and all elements
- after (including `end()`).
- @see
- @ref segments_encoded_ref,
- @ref segments_encoded_view,
- @ref segments_view.
- */
- class segments_ref
- : public segments_base
- {
- url_base* u_ = nullptr;
- friend class url_base;
- friend class segments_encoded_ref;
- segments_ref(url_base& u) noexcept;
- public:
- //--------------------------------------------
- //
- // Special Members
- //
- //--------------------------------------------
- /** Constructor
- After construction, both views
- reference the same url. Ownership is not
- transferred; the caller is responsible
- for ensuring the lifetime of the url
- extends until it is no longer
- referenced.
- @par Postconditions
- @code
- &this->url() == &other.url();
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @param other The other view.
- */
- segments_ref(
- segments_ref const& other) = default;
- /** Assignment
- The existing contents are replaced
- by a copy of the other segments.
- <br>
- All iterators are invalidated.
- @note
- None of the character buffers referenced
- by `other` may overlap the buffer of the
- underlying url, or else the behavior
- is undefined.
- @par Effects
- @code
- this->assign( other.begin(), other.end() );
- @endcode
- @par Complexity
- Linear in `other.buffer().size()`.
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @param other The segments to assign.
- */
- /** @{ */
- BOOST_URL_DECL
- segments_ref&
- operator=(segments_ref const& other);
- BOOST_URL_DECL
- segments_ref&
- operator=(segments_view const& other);
- /** @} */
- /** Assignment
- The existing contents are replaced
- by a copy of the contents of the
- initializer list.
- Reserved characters in the list are
- automatically escaped.
- <br>
- All iterators are invalidated.
- @par Example
- @code
- url u;
- u.segments() = { "path", "to", "file.txt" };
- @endcode
- @par Preconditions
- None of the character buffers referenced
- by the list may overlap the character
- buffer of the underlying url, or else
- the behavior is undefined.
- @par Effects
- @code
- this->assign( init.begin(), init.end() );
- @endcode
- @par Complexity
- Linear in `init.size() + this->url().encoded_query().size() + this->url().encoded_fragment().size()`.
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @param init The list of segments to assign.
- */
- BOOST_URL_DECL
- segments_ref&
- operator=(std::initializer_list<
- core::string_view> init);
- /** Conversion
- @see
- @ref segments_view.
- */
- BOOST_URL_DECL
- operator
- segments_view() const noexcept;
- //--------------------------------------------
- //
- // Observers
- //
- //--------------------------------------------
- /** Return the referenced url
- This function returns the url referenced
- by the view.
- @par Example
- @code
- url u( "/path/to/file.txt" );
- assert( &u.segments().url() == &u );
- @endcode
- @par Exception Safety
- Throws nothing.
- */
- url_base&
- url() const noexcept
- {
- return *u_;
- }
- //--------------------------------------------
- //
- // Modifiers
- //
- //--------------------------------------------
- /** Clear the contents of the container
- <br>
- All iterators are invalidated.
- @par Effects
- @code
- this->url().set_encoded_path( "" );
- @endcode
- @par Postconditions
- @code
- this->empty() == true
- @endcode
- @par Complexity
- Linear in `this->url().encoded_query().size() + this->url().encoded_fragment().size()`.
- @par Exception Safety
- Throws nothing.
- */
- void
- clear() noexcept;
- /** Assign segments
- The existing contents are replaced
- by a copy of the contents of the
- initializer list.
- Reserved characters in the list are
- automatically escaped.
- <br>
- All iterators are invalidated.
- @note
- None of the character buffers referenced
- by `init` may overlap the character buffer
- of the underlying url, or else the behavior
- is undefined.
- @par Example
- @code
- url u;
- u.segments().assign( { "path", "to", "file.txt" } );
- @endcode
- @par Complexity
- Linear in `init.size() + this->url().encoded_query().size() + this->url().encoded_fragment().size()`.
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @param init The list of segments to assign.
- */
- BOOST_URL_DECL
- void
- assign(std::initializer_list<
- core::string_view> init);
- /** Assign segments
- The existing contents are replaced
- by a copy of the contents of the range.
- Reserved characters in the range are
- automatically escaped.
- <br>
- All iterators are invalidated.
- @note
- None of the character buffers referenced
- by the range may overlap the character
- buffer of the underlying url, or else
- the behavior is undefined.
- @par Mandates
- @code
- std::is_convertible< std::iterator_traits< FwdIt >::reference_type, core::string_view >::value == true
- @endcode
- @par Complexity
- Linear in `std::distance( first, last ) + this->url().encoded_query().size() + this->url().encoded_fragment().size()`.
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @param first, last The range of segments
- to assign.
- */
- template<class FwdIt>
- void
- assign(FwdIt first, FwdIt last);
- //--------------------------------------------
- /** Insert segments
- This function inserts a segment
- before the specified position.
- Reserved characters in the segment are
- automatically escaped.
- <br>
- All iterators that are equal to
- `before` or come after are invalidated.
- @par Complexity
- Linear in `s.size() + this->url().encoded_resource().size()`.
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @return An iterator to the inserted
- segment.
- @param before An iterator before which
- the segment is inserted. This may
- be equal to `end()`.
- @param s The segment to insert.
- */
- BOOST_URL_DECL
- iterator
- insert(
- iterator before,
- core::string_view s);
- /** Insert segments
- This function inserts the segments
- in an initializer list before the
- specified position.
- Reserved characters in the list are
- percent-escaped in the result.
- <br>
- All iterators that are equal to
- `before` or come after are invalidated.
- @note
- None of the character buffers referenced
- by the list may overlap the character
- buffer of the underlying url, or else
- the behavior is undefined.
- @par Example
- @code
- url u( "/file.txt" );
- u.segments().insert( u.segments().begin(), { "path", "to" } );
- @endcode
- @par Complexity
- Linear in `init.size() + this->url().encoded_resource().size()`.
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @return An iterator to the first
- element inserted, or `before` if
- `init.size() == 0`.
- @param before An iterator before which
- the list is inserted. This may
- be equal to `end()`.
- @param init The list of segments to insert.
- */
- BOOST_URL_DECL
- iterator
- insert(
- iterator before,
- std::initializer_list<core::string_view> init);
- /** Insert segments
- This function inserts the segments in
- a range before the specified position.
- Reserved characters in the list are
- automatically escaped.
- <br>
- All iterators that are equal to
- `before` or come after are invalidated.
- @note
- None of the character buffers referenced
- by the range may overlap the character
- buffer of the underlying url, or else
- the behavior is undefined.
- @par Mandates
- @code
- std::is_convertible< std::iterator_traits< FwdIt >::reference_type, core::string_view >::value == true
- @endcode
- @par Complexity
- Linear in `std::distance( first, last ) + this->url().encoded_resource().size()`.
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @return An iterator to the first
- segment inserted, or `before` if
- `init.empty()`.
- @param before An iterator before which
- the range is inserted. This may
- be equal to `end()`.
- @param first, last The range of segments
- to insert.
- */
- template<class FwdIt>
- iterator
- insert(
- iterator before,
- FwdIt first,
- FwdIt last);
- //--------------------------------------------
- /** Erase segments
- This function removes a segment.
- <br>
- All iterators that are equal to
- `pos` or come after are invalidated.
- @par Complexity
- Linear in `this->url().encoded_resource().size()`.
- @par Exception Safety
- Throws nothing.
- @return An iterator to one past
- the removed segment.
- @param pos An iterator to the segment.
- */
- iterator
- erase(
- iterator pos) noexcept;
- /** Erase segments
- This function removes a range of segments.
- <br>
- All iterators that are equal to
- `first` or come after are invalidated.
- @par Complexity
- Linear in `this->url().encoded_resource().size()`.
- @par Exception Safety
- Throws nothing.
- @return An iterator to one past
- the removed range.
- @param first, last The range of
- segments to erase.
- */
- BOOST_URL_DECL
- iterator
- erase(
- iterator first,
- iterator last) noexcept;
- //--------------------------------------------
- /** Replace segments
- This function replaces the segment at
- the specified position.
- Reserved characters in the string are
- automatically escaped.
- <br>
- All iterators that are equal to
- `pos` or come after are invalidated.
- @par Complexity
- Linear in `s.size() + this->url().encoded_resouce().size()`.
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @return An iterator to the replaced segment.
- @param pos An iterator to the segment.
- @param s The string to assign.
- */
- BOOST_URL_DECL
- iterator
- replace(
- iterator pos,
- core::string_view s);
- /** Replace segments
- This function replaces a range of
- segments with one segment.
- Reserved characters in the string are
- automatically escaped.
- <br>
- All iterators that are equal to
- `from` or come after are invalidated.
- @par Complexity
- Linear in `s.size() + this->url().encoded_resouce().size()`.
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @return An iterator to the new segment.
- @param from, to The range of segments to replace.
- @param s The string to assign.
- */
- BOOST_URL_DECL
- iterator
- replace(
- iterator from,
- iterator to,
- core::string_view s);
- /** Replace segments
- This function replaces a range of
- segments with a list of segments in
- an initializer list.
- Reserved characters in the list are
- automatically escaped.
- <br>
- All iterators that are equal to
- `from` or come after are invalidated.
- @par Preconditions
- None of the character buffers referenced
- by the list may overlap the character
- buffer of the underlying url, or else
- the behavior is undefined.
- @par Complexity
- Linear in `init.size() + this->url().encoded_resouce().size()`.
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @return An iterator to the first
- segment inserted, or one past `to` if
- `init.size() == 0`.
- @param from, to The range of segments to replace.
- @param init The list of segments to assign.
- */
- BOOST_URL_DECL
- iterator
- replace(
- iterator from,
- iterator to,
- std::initializer_list<
- core::string_view> init);
- /** Replace segments
- This function replaces a range of
- segments with annother range of segments.
- Reserved characters in the new range are
- automatically escaped.
- <br>
- All iterators that are equal to
- `from` or come after are invalidated.
- @par Preconditions
- None of the character buffers referenced
- by the new range may overlap the character
- buffer of the underlying url, or else
- the behavior is undefined.
- @par Complexity
- Linear in `std::distance( first, last ) + this->url().encoded_resouce().size()`.
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @return An iterator to the first
- segment inserted, or one past `to` if
- `init.size() == 0`.
- @param from, to The range of segments to replace.
- @param first, last The range of segments to assign.
- */
- template<class FwdIt>
- iterator
- replace(
- iterator from,
- iterator to,
- FwdIt first,
- FwdIt last);
- /** Append a segment
- This function appends a segment to
- the end of the path.
- Reserved characters in the string are
- automatically escaped.
- <br>
- All end iterators are invalidated.
- @par Postconditions
- @code
- this->back() == s
- @endcode
- @par Exception Safety
- Strong guarantee.
- Calls to allocate may throw.
- @param s The segment to append.
- */
- void
- push_back(
- core::string_view s);
- /** Remove the last segment
- This function removes the last segment
- from the container.
- <br>
- Iterators to the last segment as well
- as all end iterators are invalidated.
- @par Preconditions
- @code
- not this->empty()
- @endcode
- @par Exception Safety
- Throws nothing.
- */
- void
- pop_back() noexcept;
- private:
- template<class FwdIt>
- iterator
- insert(
- iterator before,
- FwdIt first,
- FwdIt last,
- std::input_iterator_tag) = delete;
- template<class FwdIt>
- iterator
- insert(
- iterator before,
- FwdIt first,
- FwdIt last,
- std::forward_iterator_tag);
- };
- } // urls
- } // boost
- // This include is at the bottom of
- // url_base.hpp because of a circular dependency
- //
- // #include <boost/url/impl/segments_ref.hpp>
- #endif
|