123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877 |
- //
- // Copyright (c) 2022 Vinnie Falco ([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_GRAMMAR_STRING_VIEW_BASE_HPP
- #define BOOST_URL_GRAMMAR_STRING_VIEW_BASE_HPP
- #include <boost/url/detail/config.hpp>
- #include <boost/url/detail/string_view.hpp>
- #include <boost/core/detail/string_view.hpp>
- #include <cstddef>
- #include <iterator>
- #include <string>
- #include <type_traits>
- #include <utility>
- namespace boost {
- namespace urls {
- namespace grammar {
- /** Common functionality for string views
- This base class is used to provide common
- member functions for reference types that
- behave like string views. This cannot be
- instantiated directly; Instead, derive
- from the type and provide constructors
- which offer any desired preconditions
- and invariants.
- */
- class string_view_base
- {
- protected:
- /** The referenced character buffer
- */
- core::string_view s_;
- /** Constructor
- */
- constexpr
- string_view_base(
- core::string_view s) noexcept
- : s_(s)
- {
- }
- /** Constructor
- */
- constexpr
- string_view_base(
- char const* data,
- std::size_t size) noexcept
- : s_(data, size)
- {
- }
- /** Swap
- */
- // VFALCO No idea why this fails in msvc
- /*BOOST_CXX14_CONSTEXPR*/ void swap(
- string_view_base& s ) noexcept
- {
- std::swap(s_, s.s_);
- }
- /** Constructor
- */
- string_view_base() = default;
- /** Constructor
- */
- string_view_base(
- string_view_base const&) = default;
- /** Assignment
- */
- string_view_base& operator=(
- string_view_base const&) = default;
- public:
- /// The character traits
- typedef std::char_traits<char> traits_type;
- /// The value type
- typedef char value_type;
- /// The pointer type
- typedef char* pointer;
- /// The const pointer type
- typedef char const* const_pointer;
- /// The reference type
- typedef char& reference;
- /// The const reference type
- typedef char const& const_reference;
- /// The const iterator type
- typedef char const* const_iterator;
- /// The iterator type
- typedef const_iterator iterator;
- /// The const reverse iterator type
- typedef std::reverse_iterator<
- const_iterator> const_reverse_iterator;
- /// The reverse iterator type
- typedef const_reverse_iterator reverse_iterator;
- /// The size type
- typedef std::size_t size_type;
- /// The difference type
- typedef std::ptrdiff_t difference_type;
- /// A constant used to represent "no position"
- static constexpr std::size_t npos = core::string_view::npos;
- //--------------------------------------------
- /** Conversion
- */
- operator
- core::string_view() const noexcept
- {
- return s_;
- }
- /** Conversion
- */
- #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
- operator
- std::string_view() const noexcept
- {
- return std::string_view(s_);
- }
- #endif
- /** Conversion
- Conversion to std::string is explicit
- because assigning to string using an
- implicit constructor does not preserve
- capacity.
- */
- explicit
- operator
- std::string() const noexcept
- {
- return std::string(s_);
- }
- //--------------------------------------------
- // iterator support
- /** Return an iterator to the beginning
- See `core::string_view::begin`
- */
- BOOST_CONSTEXPR const_iterator begin() const noexcept
- {
- return s_.begin();
- }
- /** Return an iterator to the end
- See `core::string_view::end`
- */
- BOOST_CONSTEXPR const_iterator end() const noexcept
- {
- return s_.end();
- }
- /** Return an iterator to the beginning
- See `core::string_view::cbegin`
- */
- BOOST_CONSTEXPR const_iterator cbegin() const noexcept
- {
- return s_.cbegin();
- }
- /** Return an iterator to the end
- See `core::string_view::cend`
- */
- BOOST_CONSTEXPR const_iterator cend() const noexcept
- {
- return s_.cend();
- }
- /** Return a reverse iterator to the end
- See `core::string_view::rbegin`
- */
- #ifdef __cpp_lib_array_constexpr
- constexpr
- #endif
- const_reverse_iterator rbegin() const noexcept
- {
- return s_.rbegin();
- }
- /** Return a reverse iterator to the beginning
- See `core::string_view::rend`
- */
- #ifdef __cpp_lib_array_constexpr
- constexpr
- #endif
- const_reverse_iterator rend() const noexcept
- {
- return s_.rend();
- }
- /** Return a reverse iterator to the end
- See `core::string_view::crbegin`
- */
- #ifdef __cpp_lib_array_constexpr
- constexpr
- #endif
- const_reverse_iterator crbegin() const noexcept
- {
- return s_.crbegin();
- }
- /** Return a reverse iterator to the beginning
- See `core::string_view::crend`
- */
- #ifdef __cpp_lib_array_constexpr
- constexpr
- #endif
- const_reverse_iterator crend() const noexcept
- {
- return s_.crend();
- }
- // capacity
- /** Return the size
- See `core::string_view::size`
- */
- BOOST_CONSTEXPR size_type size() const noexcept
- {
- return s_.size();
- }
- /** Return the size
- See `core::string_view::length`
- */
- BOOST_CONSTEXPR size_type length() const noexcept
- {
- return s_.length();
- }
- /** Return the maximum allowed size
- See `core::string_view::max_size`
- */
- BOOST_CONSTEXPR size_type max_size() const noexcept
- {
- return s_.max_size();
- }
- /** Return true if the string is empty
- See `core::string_view::size`
- */
- BOOST_CONSTEXPR bool empty() const noexcept
- {
- return s_.empty();
- }
-
- // element access
- /** Access a character
- See `core::string_view::operator[]`
- */
- BOOST_CXX14_CONSTEXPR const_reference
- operator[]( size_type pos ) const noexcept
- {
- return s_[pos];
- }
- /** Access a character
- See `core::string_view::at`
- */
- BOOST_CXX14_CONSTEXPR const_reference
- at( size_type pos ) const
- {
- return s_.at(pos);
- }
- /** Return the first character
- See `core::string_view::front`
- */
- BOOST_CXX14_CONSTEXPR const_reference
- front() const noexcept
- {
- return s_.front();
- }
- /** Return the last character
- See `core::string_view::back`
- */
- BOOST_CXX14_CONSTEXPR const_reference
- back() const noexcept
- {
- return s_.back();
- }
- /** Return a pointer to the character buffer
- See `core::string_view::data`
- */
- BOOST_CONSTEXPR const_pointer
- data() const noexcept
- {
- return s_.data();
- }
- // string operations
- /** Copy the characters to another buffer
- See `core::string_view::copy`
- */
- BOOST_CXX14_CONSTEXPR size_type copy(
- char* s, size_type n, size_type pos = 0 ) const
- {
- return s_.copy(s, n, pos);
- }
- /** Return a view to part of the string
- See `core::string_view::substr`
- */
- BOOST_CXX14_CONSTEXPR core::string_view substr(
- size_type pos = 0, size_type n = core::string_view::npos ) const
- {
- return s_.substr(pos, n);
- }
- // comparison
- /** Return the result of comparing to another string
- See `core::string_view::compare`
- */
- BOOST_CXX14_CONSTEXPR int
- compare( core::string_view str ) const noexcept
- {
- return s_.compare(str);
- }
- /** Return the result of comparing to another string
- See `core::string_view::compare`
- */
- BOOST_CONSTEXPR int compare(
- size_type pos1, size_type n1, core::string_view str ) const
- {
- return s_.compare(pos1, n1, str);
- }
- /** Return the result of comparing to another string
- See `core::string_view::compare`
- */
- BOOST_CONSTEXPR int compare(
- size_type pos1, size_type n1, core::string_view str,
- size_type pos2, size_type n2 ) const
- {
- return s_.compare(pos1, n1, str, pos2, n2);
- }
- /** Return the result of comparing to another string
- See `core::string_view::compare`
- */
- BOOST_CONSTEXPR int compare(
- char const* s ) const noexcept
- {
- return s_.compare(s);
- }
- /** Return the result of comparing to another string
- See `core::string_view::compare`
- */
- BOOST_CONSTEXPR int compare(
- size_type pos1, size_type n1, char const* s ) const
- {
- return s_.compare(pos1, n1, s);
- }
- /** Return the result of comparing to another string
- See `core::string_view::compare`
- */
- BOOST_CONSTEXPR int compare(
- size_type pos1, size_type n1,
- char const* s, size_type n2 ) const
- {
- return s_.compare(pos1, n1, s, n2);
- }
- // starts_with
- /** Return true if a matching prefix exists
- See `core::string_view::starts_with`
- */
- BOOST_CONSTEXPR bool starts_with(
- core::string_view x ) const noexcept
- {
- return s_.starts_with(x);
- }
- /** Return true if a matching prefix exists
- See `core::string_view::starts_with`
- */
- BOOST_CONSTEXPR bool starts_with(
- char x ) const noexcept
- {
- return s_.starts_with(x);
- }
- /** Return true if a matching prefix exists
- See `core::string_view::starts_with`
- */
- BOOST_CONSTEXPR bool starts_with(
- char const* x ) const noexcept
- {
- return s_.starts_with(x);
- }
- // ends_with
- /** Return true if a matching suffix exists
- See `core::string_view::ends_with`
- */
- BOOST_CONSTEXPR bool ends_with(
- core::string_view x ) const noexcept
- {
- return s_.ends_with(x);
- }
- /** Return true if a matching suffix exists
- See `core::string_view::ends_with`
- */
- BOOST_CONSTEXPR bool ends_with(
- char x ) const noexcept
- {
- return s_.ends_with(x);
- }
- /** Return true if a matching suffix exists
- See `core::string_view::ends_with`
- */
- BOOST_CONSTEXPR bool ends_with(
- char const* x ) const noexcept
- {
- return s_.ends_with(x);
- }
- // find
- /** Return the position of matching characters
- See `core::string_view::find`
- */
- BOOST_CONSTEXPR size_type find(
- core::string_view str, size_type pos = 0 ) const noexcept
- {
- return s_.find(str, pos);
- }
- /** Return the position of matching characters
- See `core::string_view::find`
- */
- BOOST_CXX14_CONSTEXPR size_type find(
- char c, size_type pos = 0 ) const noexcept
- {
- return s_.find(c, pos);
- }
- /** Return the position of matching characters
- See `core::string_view::find`
- */
- BOOST_CXX14_CONSTEXPR size_type find(
- char const* s, size_type pos, size_type n ) const noexcept
- {
- return s_.find(s, pos, n);
- }
- /** Return the position of matching characters
- See `core::string_view::find`
- */
- BOOST_CONSTEXPR size_type find(
- char const* s, size_type pos = 0 ) const noexcept
- {
- return s_.find(s, pos);
- }
- // rfind
- /** Return the position of matching characters
- See `core::string_view::rfind`
- */
- BOOST_CONSTEXPR size_type rfind(
- core::string_view str, size_type pos = core::string_view::npos ) const noexcept
- {
- return s_.rfind(str, pos);
- }
- /** Return the position of matching characters
- See `core::string_view::rfind`
- */
- BOOST_CXX14_CONSTEXPR size_type rfind(
- char c, size_type pos = core::string_view::npos ) const noexcept
- {
- return s_.rfind(c, pos);
- }
- /** Return the position of matching characters
- See `core::string_view::rfind`
- */
- BOOST_CXX14_CONSTEXPR size_type rfind(
- char const* s, size_type pos, size_type n ) const noexcept
- {
- return s_.rfind(s, pos, n);
- }
- /** Return the position of matching characters
- See `core::string_view::rfind`
- */
- BOOST_CONSTEXPR size_type rfind(
- char const* s, size_type pos = core::string_view::npos ) const noexcept
- {
- return s_.rfind(s, pos);
- }
- // find_first_of
- /** Return the position of the first match
- See `core::string_view::find_first_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_first_of(
- core::string_view str, size_type pos = 0 ) const noexcept
- {
- return s_.find_first_of(str, pos);
- }
- /** Return the position of the first match
- See `core::string_view::find_first_of`
- */
- BOOST_CONSTEXPR size_type find_first_of(
- char c, size_type pos = 0 ) const noexcept
- {
- return s_.find_first_of(c, pos);
- }
- /** Return the position of the first match
- See `core::string_view::find_first_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_first_of(
- char const* s, size_type pos, size_type n ) const noexcept
- {
- return s_.find_first_of(s, pos, n);
- }
- /** Return the position of the first match
- See `core::string_view::find_first_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_first_of(
- char const* s, size_type pos = 0 ) const noexcept
- {
- return s_.find_first_of(s, pos);
- }
- // find_last_of
- /** Return the position of the last match
- See `core::string_view::find_last_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_last_of(
- core::string_view str, size_type pos = core::string_view::npos ) const noexcept
- {
- return s_.find_last_of(str, pos);
- }
- /** Return the position of the last match
- See `core::string_view::find_last_of`
- */
- BOOST_CONSTEXPR size_type find_last_of(
- char c, size_type pos = core::string_view::npos ) const noexcept
- {
- return s_.find_last_of(c, pos);
- }
- /** Return the position of the last match
- See `core::string_view::find_last_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_last_of(
- char const* s, size_type pos, size_type n ) const noexcept
- {
- return s_.find_last_of(s, pos, n);
- }
- /** Return the position of the last match
- See `core::string_view::find_last_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_last_of(
- char const* s, size_type pos = core::string_view::npos ) const noexcept
- {
- return s_.find_last_of(s, pos);
- }
- // find_first_not_of
- /** Return the position of the first non-match
- See `core::string_view::find_first_not_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_first_not_of(
- core::string_view str, size_type pos = 0 ) const noexcept
- {
- return s_.find_first_not_of(str, pos);
- }
- /** Return the position of the first non-match
- See `core::string_view::find_first_not_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_first_not_of(
- char c, size_type pos = 0 ) const noexcept
- {
- return s_.find_first_not_of(c, pos);
- }
- /** Return the position of the first non-match
- See `core::string_view::find_first_not_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_first_not_of(
- char const* s, size_type pos, size_type n ) const noexcept
- {
- return s_.find_first_not_of(s, pos, n);
- }
- /** Return the position of the first non-match
- See `core::string_view::find_first_not_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_first_not_of(
- char const* s, size_type pos = 0 ) const noexcept
- {
- return s_.find_first_not_of(s, pos);
- }
- // find_last_not_of
- /** Return the position of the last non-match
- See `core::string_view::find_last_not_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_last_not_of(
- core::string_view str, size_type pos = core::string_view::npos ) const noexcept
- {
- return s_.find_last_not_of(str, pos);
- }
- /** Return the position of the last non-match
- See `core::string_view::find_last_not_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_last_not_of(
- char c, size_type pos = core::string_view::npos ) const noexcept
- {
- return s_.find_last_not_of(c, pos);
- }
- /** Return the position of the last non-match
- See `core::string_view::find_last_not_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_last_not_of(
- char const* s, size_type pos, size_type n ) const noexcept
- {
- return s_.find_last_not_of(s, pos, n);
- }
- /** Return the position of the last non-match
- See `core::string_view::find_last_not_of`
- */
- BOOST_CXX14_CONSTEXPR size_type find_last_not_of(
- char const* s, size_type pos = core::string_view::npos ) const noexcept
- {
- return s_.find_last_not_of(s, pos);
- }
- // contains
- /** Return true if matching characters are found
- See `core::string_view::contains`
- */
- BOOST_CONSTEXPR bool contains( core::string_view sv ) const noexcept
- {
- return s_.contains(sv);
- }
- /** Return true if matching characters are found
- See `core::string_view::contains`
- */
- BOOST_CXX14_CONSTEXPR bool contains( char c ) const noexcept
- {
- return s_.contains(c);
- }
- /** Return true if matching characters are found
- See `core::string_view::contains`
- */
- BOOST_CONSTEXPR bool contains( char const* s ) const noexcept
- {
- return s_.contains(s);
- }
- // relational operators
- #ifndef BOOST_URL_DOCS
- private:
- template<class S0, class S1>
- using is_match = std::integral_constant<bool,
- std::is_convertible<S0, core::string_view>::value &&
- std::is_convertible<S1, core::string_view>::value && (
- (std::is_base_of<string_view_base,
- typename std::decay<S0>::type>::value &&
- std::is_convertible<S0 const volatile*,
- string_view_base const volatile*>::value) ||
- (std::is_base_of<string_view_base,
- typename std::decay<S1>::type>::value &&
- std::is_convertible<S1 const volatile*,
- string_view_base const volatile*>::value))>;
- public:
- template<class S0, class S1>
- BOOST_CXX14_CONSTEXPR friend auto operator==(
- S0 const& s0, S1 const& s1) noexcept ->
- typename std::enable_if<
- is_match<S0, S1>::value, bool>::type
- {
- return urls::detail::to_sv(s0) == urls::detail::to_sv(s1);
- }
- template<class S0, class S1>
- BOOST_CXX14_CONSTEXPR friend auto operator!=(
- S0 const& s0, S1 const& s1) noexcept ->
- typename std::enable_if<
- is_match<S0, S1>::value, bool>::type
- {
- return urls::detail::to_sv(s0) != urls::detail::to_sv(s1);
- }
- template<class S0, class S1>
- BOOST_CXX14_CONSTEXPR friend auto operator<(
- S0 const& s0, S1 const& s1) noexcept ->
- typename std::enable_if<
- is_match<S0, S1>::value, bool>::type
- {
- return urls::detail::to_sv(s0) < urls::detail::to_sv(s1);
- }
- template<class S0, class S1>
- BOOST_CXX14_CONSTEXPR friend auto operator<=(
- S0 const& s0, S1 const& s1) noexcept ->
- typename std::enable_if<
- is_match<S0, S1>::value, bool>::type
- {
- return urls::detail::to_sv(s0) <= urls::detail::to_sv(s1);
- }
- template<class S0, class S1>
- BOOST_CXX14_CONSTEXPR friend auto operator>(
- S0 const& s0, S1 const& s1) noexcept ->
- typename std::enable_if<
- is_match<S0, S1>::value, bool>::type
- {
- return urls::detail::to_sv(s0) > urls::detail::to_sv(s1);
- }
- template<class S0, class S1>
- BOOST_CXX14_CONSTEXPR friend auto operator>=(
- S0 const& s0, S1 const& s1) noexcept ->
- typename std::enable_if<
- is_match<S0, S1>::value, bool>::type
- {
- return urls::detail::to_sv(s0) >= urls::detail::to_sv(s1);
- }
- #endif
- //--------------------------------------------
- /** Return the hash of this value
- */
- friend
- std::size_t
- hash_value(
- string_view_base const& s) noexcept
- {
- return hash_value(s.s_);
- }
- BOOST_URL_DECL
- friend
- std::ostream&
- operator<<(
- std::ostream& os,
- string_view_base const& s);
- };
- //------------------------------------------------
- /** Format a string to an output stream
- */
- BOOST_URL_DECL
- std::ostream&
- operator<<(
- std::ostream& os,
- string_view_base const& s);
- } // grammar
- #ifndef BOOST_URL_DOCS
- namespace detail {
- template <>
- inline
- core::string_view
- to_sv(grammar::string_view_base const& s) noexcept
- {
- return s.operator core::string_view();
- }
- } // detail
- #endif
- } // urls
- } // boost
- #endif
|