123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- #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:
-
-
- core::string_view s;
-
-
- core::string_view front;
-
-
-
-
- int fast_nseg = 0;
-
-
-
-
-
-
- bool encode_colons = false;
-
- virtual void rewind() noexcept = 0;
-
-
-
-
- virtual bool measure(std::size_t& n) = 0;
-
-
- virtual void copy(char*& dest,
- char const* end) noexcept = 0;
- };
- 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;
- };
- 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;
- };
- 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);
- }
- };
- 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;
- };
- 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;
- };
- 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_)
- {
-
- 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;
-
- 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);
- }
- }
- }
- }
- #endif
|