123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- //
- // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
- //
- // 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_DETAIL_ANY_SEGMENTS_ITER_HPP
- #define BOOST_URL_DETAIL_ANY_SEGMENTS_ITER_HPP
- #include <boost/url/pct_string_view.hpp>
- #include <boost/static_assert.hpp>
- #include <cstddef>
- #include <iterator>
- #include <type_traits>
- namespace boost {
- namespace urls {
- namespace detail {
- struct BOOST_SYMBOL_VISIBLE
- any_segments_iter
- {
- protected:
- explicit
- any_segments_iter(
- core::string_view s_ = {}) noexcept
- : s(s_)
- {
- }
- virtual ~any_segments_iter() = default;
- public:
- // this is adjusted
- // when self-intersecting
- core::string_view s;
- // the first segment,
- // to handle special cases
- core::string_view front;
- // quick number of segments
- // 0 = zero
- // 1 = one
- // 2 = two, or more
- int fast_nseg = 0;
- // whether the segments should encode colons
- // when we measure and copy. the calling
- // function uses this for the first
- // segment in some cases, such as:
- // "x:y:z" -> remove_scheme -> "y%3Az"
- // as "y:z" would no longer represent a path
- bool encode_colons = false;
- // Rewind the iterator to the beginning
- virtual void rewind() noexcept = 0;
- // Measure and increment the current
- // element. n is increased by the
- // encoded size. Returns false on
- // end of range.
- virtual bool measure(std::size_t& n) = 0;
- // Copy and increment the current
- // element, encoding as needed.
- virtual void copy(char*& dest,
- char const* end) noexcept = 0;
- };
- //------------------------------------------------
- //
- // segment_iter
- //
- //------------------------------------------------
- // A 1-segment range
- // allowing self-intersection
- struct BOOST_SYMBOL_VISIBLE
- segment_iter
- : any_segments_iter
- {
- virtual ~segment_iter() = default;
- explicit
- segment_iter(
- core::string_view s) noexcept;
- private:
- bool at_end_ = false;
- void rewind() noexcept override;
- bool measure(std::size_t&) noexcept override;
- void copy(char*&, char const*) noexcept override;
- };
- //------------------------------------------------
- //
- // segments_iter
- //
- //------------------------------------------------
- struct segments_iter_base
- {
- protected:
- BOOST_URL_DECL static void
- measure_impl(std::size_t&,
- core::string_view, bool) noexcept;
- BOOST_URL_DECL static void
- copy_impl(char*&, char const*,
- core::string_view, bool) noexcept;
- };
- // iterates segments in a
- // plain segment range
- template<class FwdIt>
- struct segments_iter
- : any_segments_iter
- , segments_iter_base
- {
- BOOST_STATIC_ASSERT(
- std::is_convertible<
- typename std::iterator_traits<
- FwdIt>::reference,
- core::string_view>::value);
- segments_iter(
- FwdIt first,
- FwdIt last) noexcept
- : it_(first)
- , it0_(first)
- , end_(last)
- {
- if(first != last)
- {
- front = *first;
- auto it = first;
- if(++it == last)
- fast_nseg = 1;
- else
- fast_nseg = 2;
- }
- else
- {
- fast_nseg = 0;
- }
- }
- private:
- FwdIt it_;
- FwdIt it0_;
- FwdIt end_;
- void
- rewind() noexcept override
- {
- it_ = it0_;
- }
- bool
- measure(
- std::size_t& n) noexcept override
- {
- if(it_ == end_)
- return false;
- measure_impl(n,
- detail::to_sv(*it_),
- encode_colons);
- ++it_;
- return true;
- }
- void
- copy(
- char*& dest,
- char const* end) noexcept override
- {
- copy_impl(dest, end,
- detail::to_sv(*it_++),
- encode_colons);
- }
- };
- //------------------------------------------------
- //
- // segment_encoded_iter
- //
- //------------------------------------------------
- // A 1-segment range
- // allowing self-intersection
- struct BOOST_SYMBOL_VISIBLE
- segment_encoded_iter
- : any_segments_iter
- {
- virtual ~segment_encoded_iter() = default;
- explicit
- segment_encoded_iter(
- pct_string_view const& s) noexcept;
- private:
- bool at_end_ = false;
- void rewind() noexcept override;
- bool measure(std::size_t&) noexcept override;
- void copy(char*&, char const*) noexcept override;
- };
- //------------------------------------------------
- //
- // segments_encoded_iter
- //
- //------------------------------------------------
- // Validating and copying from
- // a string of encoded segments
- struct segments_encoded_iter_base
- {
- protected:
- BOOST_URL_DECL static void
- measure_impl(std::size_t&,
- core::string_view, bool) noexcept;
- BOOST_URL_DECL static void
- copy_impl(char*&, char const*,
- core::string_view, bool) noexcept;
- };
- // iterates segments in an
- // encoded segment range
- template<class FwdIt>
- struct segments_encoded_iter
- : public any_segments_iter
- , public segments_encoded_iter_base
- {
- BOOST_STATIC_ASSERT(
- std::is_convertible<
- typename std::iterator_traits<
- FwdIt>::reference,
- core::string_view>::value);
- segments_encoded_iter(
- FwdIt first,
- FwdIt last)
- : it_(first)
- , it0_(first)
- , end_(last)
- {
- if(it_ != end_)
- {
- // throw on invalid input
- front = pct_string_view(
- detail::to_sv(*first));
- auto it = first;
- if(++it == last)
- fast_nseg = 1;
- else
- fast_nseg = 2;
- }
- else
- {
- fast_nseg = 0;
- }
- }
- private:
- FwdIt it_;
- FwdIt it0_;
- FwdIt end_;
- void
- rewind() noexcept override
- {
- it_ = it0_;
- }
- bool
- measure(
- std::size_t& n) override
- {
- if(it_ == end_)
- return false;
- // throw on invalid input
- measure_impl(n,
- pct_string_view(
- detail::to_sv(*it_++)),
- encode_colons);
- return true;
- }
- void
- copy(
- char*& dest,
- char const* end) noexcept override
- {
- copy_impl(dest, end,
- detail::to_sv(*it_++),
- encode_colons);
- }
- };
- //------------------------------------------------
- template<class FwdIt>
- segments_iter<FwdIt>
- make_segments_iter(
- FwdIt first, FwdIt last)
- {
- return segments_iter<
- FwdIt>(first, last);
- }
- template<class FwdIt>
- segments_encoded_iter<FwdIt>
- make_segments_encoded_iter(
- FwdIt first, FwdIt last)
- {
- return segments_encoded_iter<
- FwdIt>(first, last);
- }
- } // detail
- } // urls
- } // boost
- #endif
|