123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742 |
- //
- // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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_GRAMMAR_IMPL_RANGE_HPP
- #define BOOST_URL_GRAMMAR_IMPL_RANGE_HPP
- #include <boost/url/detail/except.hpp>
- #include <boost/url/grammar/error.hpp>
- #include <boost/url/grammar/recycled.hpp>
- #include <boost/core/empty_value.hpp>
- #include <boost/assert.hpp>
- #include <boost/static_assert.hpp>
- #include <exception>
- #include <iterator>
- #include <new>
- #include <stddef.h> // ::max_align_t
- namespace boost {
- namespace urls {
- namespace grammar {
- // VFALCO This could be reused for
- // other things that need to type-erase
- //------------------------------------------------
- //
- // any_rule
- //
- //------------------------------------------------
- // base class for the type-erased rule pair
- template<class T>
- struct range<T>::
- any_rule
- {
- virtual
- ~any_rule() = default;
- virtual
- void
- move(void* dest) noexcept
- {
- ::new(dest) any_rule(
- std::move(*this));
- }
- virtual
- void
- copy(void* dest) const noexcept
- {
- ::new(dest) any_rule(*this);
- }
- virtual
- system::result<T>
- first(
- char const*&,
- char const*) const noexcept
- {
- return system::error_code{};
- }
- virtual
- system::result<T>
- next(
- char const*&,
- char const*) const noexcept
- {
- return system::error_code{};
- }
- };
- //------------------------------------------------
- // small
- template<class T>
- template<class R, bool Small>
- struct range<T>::impl1
- : any_rule
- , private empty_value<R>
- {
- explicit
- impl1(R const& next) noexcept
- : empty_value<R>(
- empty_init,
- next)
- {
- }
- private:
- impl1(impl1&&) noexcept = default;
- impl1(impl1 const&) noexcept = default;
- void
- move(void* dest
- ) noexcept override
- {
- ::new(dest) impl1(
- std::move(*this));
- }
- void
- copy(void* dest
- ) const noexcept override
- {
- ::new(dest) impl1(*this);
- }
- system::result<T>
- first(
- char const*& it,
- char const* end)
- const noexcept override
- {
- return grammar::parse(
- it, end, this->get());
- }
- system::result<T>
- next(
- char const*& it,
- char const* end)
- const noexcept override
- {
- return grammar::parse(
- it, end, this->get());
- }
- };
- //------------------------------------------------
- // big
- template<class T>
- template<class R>
- struct range<T>::impl1<R, false>
- : any_rule
- {
- explicit
- impl1(R const& next) noexcept
- {
- ::new(p_->addr()) impl{next};
- }
- private:
- struct impl
- {
- R r;
- };
- recycled_ptr<
- aligned_storage<impl>> p_;
- impl1(impl1&&) noexcept = default;
- impl1(impl1 const&) noexcept = default;
- impl const&
- get() const noexcept
- {
- return *reinterpret_cast<
- impl const*>(p_->addr());
- }
- ~impl1()
- {
- if(p_)
- get().~impl();
- }
- void
- move(void* dest
- ) noexcept override
- {
- ::new(dest) impl1(
- std::move(*this));
- }
- void
- copy(void* dest
- ) const noexcept override
- {
- ::new(dest) impl1(*this);
- }
- system::result<T>
- first(
- char const*& it,
- char const* end)
- const noexcept override
- {
- return grammar::parse(
- it, end, this->get().r);
- }
- system::result<T>
- next(
- char const*& it,
- char const* end)
- const noexcept override
- {
- return grammar::parse(
- it, end, this->get().r);
- }
- };
- //------------------------------------------------
- // small
- template<class T>
- template<
- class R0, class R1, bool Small>
- struct range<T>::impl2
- : any_rule
- , private empty_value<R0, 0>
- , private empty_value<R1, 1>
- {
- impl2(
- R0 const& first,
- R1 const& next) noexcept
- : empty_value<R0,0>(
- empty_init, first)
- , empty_value<R1,1>(
- empty_init, next)
- {
- }
- private:
- impl2(impl2&&) noexcept = default;
- impl2(impl2 const&) noexcept = default;
- void
- move(void* dest
- ) noexcept override
- {
- ::new(dest) impl2(
- std::move(*this));
- }
- void
- copy(void* dest
- ) const noexcept override
- {
- ::new(dest) impl2(*this);
- }
- system::result<T>
- first(
- char const*& it,
- char const* end)
- const noexcept override
- {
- return grammar::parse(it, end,
- empty_value<
- R0,0>::get());
- }
- system::result<T>
- next(
- char const*& it,
- char const* end)
- const noexcept override
- {
- return grammar::parse(it, end,
- empty_value<
- R1,1>::get());
- }
- };
- //------------------------------------------------
- // big
- template<class T>
- template<
- class R0, class R1>
- struct range<T>::impl2<R0, R1, false>
- : any_rule
- {
- impl2(
- R0 const& first,
- R1 const& next) noexcept
- {
- ::new(p_->addr()) impl{
- first, next};
- }
- private:
- struct impl
- {
- R0 first;
- R1 next;
- };
- recycled_ptr<
- aligned_storage<impl>> p_;
- impl2(impl2&&) noexcept = default;
- impl2(impl2 const&) noexcept = default;
- impl const&
- get() const noexcept
- {
- return *reinterpret_cast<
- impl const*>(p_->addr());
- }
- ~impl2()
- {
- if(p_)
- get().~impl();
- }
- void
- move(void* dest
- ) noexcept override
- {
- ::new(dest) impl2(
- std::move(*this));
- }
- void
- copy(void* dest
- ) const noexcept override
- {
- ::new(dest) impl2(*this);
- }
- system::result<T>
- first(
- char const*& it,
- char const* end)
- const noexcept override
- {
- return grammar::parse(
- it, end, get().first);
- }
- system::result<T>
- next(
- char const*& it,
- char const* end)
- const noexcept override
- {
- return grammar::parse(
- it, end, get().next);
- }
- };
- //------------------------------------------------
- //
- // iterator
- //
- //------------------------------------------------
- template<class T>
- class range<T>::
- iterator
- {
- public:
- using value_type = T;
- using reference = T const&;
- using pointer = void const*;
- using difference_type =
- std::ptrdiff_t;
- using iterator_category =
- std::forward_iterator_tag;
- iterator() = default;
- iterator(
- iterator const&) = default;
- iterator& operator=(
- iterator const&) = default;
- reference
- operator*() const noexcept
- {
- return *rv_;
- }
- bool
- operator==(
- iterator const& other) const noexcept
- {
- // can't compare iterators
- // from different containers!
- BOOST_ASSERT(r_ == other.r_);
- return p_ == other.p_;
- }
- bool
- operator!=(
- iterator const& other) const noexcept
- {
- return !(*this == other);
- }
- iterator&
- operator++() noexcept
- {
- BOOST_ASSERT(
- p_ != nullptr);
- auto const end =
- r_->s_.data() +
- r_->s_.size();
- rv_ = r_->get().next(p_, end);
- if( !rv_ )
- p_ = nullptr;
- return *this;
- }
- iterator
- operator++(int) noexcept
- {
- auto tmp = *this;
- ++*this;
- return tmp;
- }
- private:
- friend class range<T>;
- range<T> const* r_ = nullptr;
- char const* p_ = nullptr;
- system::result<T> rv_;
- iterator(
- range<T> const& r) noexcept
- : r_(&r)
- , p_(r.s_.data())
- {
- auto const end =
- r_->s_.data() +
- r_->s_.size();
- rv_ = r_->get().first(p_, end);
- if( !rv_ )
- p_ = nullptr;
- }
- constexpr
- iterator(
- range<T> const& r,
- int) noexcept
- : r_(&r)
- , p_(nullptr)
- {
- }
- };
- //------------------------------------------------
- template<class T>
- template<class R>
- range<T>::
- range(
- core::string_view s,
- std::size_t n,
- R const& next)
- : s_(s)
- , n_(n)
- {
- BOOST_STATIC_ASSERT(
- sizeof(impl1<R, false>) <=
- BufferSize);
- ::new(&get()) impl1<R,
- sizeof(impl1<R, true>) <=
- BufferSize>(next);
- }
- //------------------------------------------------
- template<class T>
- template<
- class R0, class R1>
- range<T>::
- range(
- core::string_view s,
- std::size_t n,
- R0 const& first,
- R1 const& next)
- : s_(s)
- , n_(n)
- {
- BOOST_STATIC_ASSERT(
- sizeof(impl2<R0, R1, false>) <=
- BufferSize);
- ::new(&get()) impl2<R0, R1,
- sizeof(impl2<R0, R1, true>
- ) <= BufferSize>(
- first, next);
- }
- //------------------------------------------------
- template<class T>
- range<T>::
- ~range()
- {
- get().~any_rule();
- }
- template<class T>
- range<T>::
- range() noexcept
- {
- ::new(&get()) any_rule{};
- char const* it = nullptr;
- get().first(it, nullptr);
- get().next(it, nullptr);
- }
- template<class T>
- range<T>::
- range(
- range&& other) noexcept
- : s_(other.s_)
- , n_(other.n_)
- {
- other.s_ = {};
- other.n_ = {};
- other.get().move(&get());
- other.get().~any_rule();
- ::new(&other.get()) any_rule{};
- }
- template<class T>
- range<T>::
- range(
- range const& other) noexcept
- : s_(other.s_)
- , n_(other.n_)
- {
- other.get().copy(&get());
- }
- template<class T>
- auto
- range<T>::
- operator=(
- range&& other) noexcept ->
- range&
- {
- s_ = other.s_;
- n_ = other.n_;
- other.s_ = {};
- other.n_ = 0;
- // VFALCO we rely on nothrow move
- // construction here, but if necessary we
- // could move to a local buffer first.
- get().~any_rule();
- other.get().move(&get());
- other.get().~any_rule();
- ::new(&other.get()) any_rule{};
- return *this;
- }
- template<class T>
- auto
- range<T>::
- operator=(
- range const& other) noexcept ->
- range&
- {
- s_ = other.s_;
- n_ = other.n_;
- // VFALCO we rely on nothrow copy
- // construction here, but if necessary we
- // could construct to a local buffer first.
- get().~any_rule();
- other.get().copy(&get());
- return *this;
- }
- template<class T>
- auto
- range<T>::
- begin() const noexcept ->
- iterator
- {
- return { *this };
- }
- template<class T>
- auto
- range<T>::
- end() const noexcept ->
- iterator
- {
- return { *this, 0 };
- }
- //------------------------------------------------
- template<class R>
- auto
- range_rule_t<R>::
- parse(
- char const*& it,
- char const* end) const ->
- system::result<value_type>
- {
- using T = typename R::value_type;
- std::size_t n = 0;
- auto const it0 = it;
- auto it1 = it;
- auto rv = (grammar::parse)(
- it, end, next_);
- if( !rv )
- {
- if(rv.error() != error::end_of_range)
- {
- // rewind unless error::end_of_range
- it = it1;
- }
- if(n < N_)
- {
- // too few
- BOOST_URL_RETURN_EC(
- error::mismatch);
- }
- // good
- return range<T>(
- core::string_view(it0, it - it0),
- n, next_);
- }
- for(;;)
- {
- ++n;
- it1 = it;
- rv = (grammar::parse)(
- it, end, next_);
- if( !rv )
- {
- if(rv.error() != error::end_of_range)
- {
- // rewind unless error::end_of_range
- it = it1;
- }
- break;
- }
- if(n >= M_)
- {
- // too many
- BOOST_URL_RETURN_EC(
- error::mismatch);
- }
- }
- if(n < N_)
- {
- // too few
- BOOST_URL_RETURN_EC(
- error::mismatch);
- }
- // good
- return range<T>(
- core::string_view(it0, it - it0),
- n, next_);
- }
- //------------------------------------------------
- template<class R0, class R1>
- auto
- range_rule_t<R0, R1>::
- parse(
- char const*& it,
- char const* end) const ->
- system::result<range<typename
- R0::value_type>>
- {
- using T = typename R0::value_type;
- std::size_t n = 0;
- auto const it0 = it;
- auto it1 = it;
- auto rv = (grammar::parse)(
- it, end, first_);
- if( !rv )
- {
- if(rv.error() != error::end_of_range)
- {
- // rewind unless error::end_of_range
- it = it1;
- }
- if(n < N_)
- {
- // too few
- BOOST_URL_RETURN_EC(
- error::mismatch);
- }
- // good
- return range<T>(
- core::string_view(it0, it - it0),
- n, first_, next_);
- }
- for(;;)
- {
- ++n;
- it1 = it;
- rv = (grammar::parse)(
- it, end, next_);
- if( !rv )
- {
- if(rv.error() != error::end_of_range)
- {
- // rewind unless error::end_of_range
- it = it1;
- }
- break;
- }
- if(n >= M_)
- {
- // too many
- BOOST_URL_RETURN_EC(
- error::mismatch);
- }
- }
- if(n < N_)
- {
- // too few
- BOOST_URL_RETURN_EC(
- error::mismatch);
- }
- // good
- return range<T>(
- core::string_view(it0, it - it0),
- n, first_, next_);
- }
- } // grammar
- } // urls
- } // boost
- #endif
|