123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- //
- // 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_PCT_STRING_VIEW_HPP
- #define BOOST_URL_PCT_STRING_VIEW_HPP
- #include <boost/url/detail/config.hpp>
- #include <boost/url/encoding_opts.hpp>
- #include <boost/url/error_types.hpp>
- #include <boost/core/detail/string_view.hpp>
- #include <boost/url/grammar/string_token.hpp>
- #include <boost/url/grammar/string_view_base.hpp>
- #include <cstddef>
- #include <iterator>
- #include <string>
- #include <type_traits>
- #include <utility>
- namespace boost {
- namespace urls {
- //------------------------------------------------
- #ifndef BOOST_URL_DOCS
- class decode_view;
- class pct_string_view;
- pct_string_view
- make_pct_string_view_unsafe(
- char const*, std::size_t,
- std::size_t) noexcept;
- namespace detail {
- core::string_view&
- ref(pct_string_view& s) noexcept;
- } // detail
- #endif
- //------------------------------------------------
- /** A reference to a valid percent-encoded string
- Objects of this type behave like a
- `core::string_view` and have the same interface,
- but offer an additional invariant: they can
- only be constructed from strings containing
- valid percent-escapes.
- Attempting construction from a string
- containing invalid or malformed percent
- escapes results in an exception.
- @par Operators
- The following operators are supported between
- @ref pct_string_view and any object that is
- convertible to `core::string_view`
- @code
- bool operator==( pct_string_view, pct_string_view ) noexcept;
- bool operator!=( pct_string_view, pct_string_view ) noexcept;
- bool operator<=( pct_string_view, pct_string_view ) noexcept;
- bool operator< ( pct_string_view, pct_string_view ) noexcept;
- bool operator> ( pct_string_view, pct_string_view ) noexcept;
- bool operator>=( pct_string_view, pct_string_view ) noexcept;
- @endcode
- */
- class pct_string_view final
- : public grammar::string_view_base
- {
- std::size_t dn_ = 0;
- #ifndef BOOST_URL_DOCS
- friend
- pct_string_view
- make_pct_string_view_unsafe(
- char const*, std::size_t,
- std::size_t) noexcept;
- friend
- core::string_view&
- detail::ref(pct_string_view&) noexcept;
- #endif
- // unsafe
- pct_string_view(
- char const* data,
- std::size_t size,
- std::size_t dn) noexcept
- : string_view_base(data, size)
- , dn_(dn)
- {
- }
- BOOST_URL_DECL
- void
- decode_impl(
- string_token::arg& dest,
- encoding_opts opt) const;
- public:
- /** Constructor
- Default constructed string are empty.
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- */
- constexpr pct_string_view() = default;
- /** Constructor
- The copy references the same
- underlying character buffer.
- Ownership is not transferred.
- @par Postconditions
- @code
- this->data() == other.data()
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par other The string to copy.
- */
- constexpr
- pct_string_view(
- pct_string_view const& other) = default;
- /** Constructor
- The newly constructed string references
- the specified character buffer.
- Ownership is not transferred.
- @par Postconditions
- @code
- this->data() == core::string_view(s).data()
- @endcode
- @par Complexity
- Linear in `core::string_view(s).size()`.
- @par Exception Safety
- Exceptions thrown on invalid input.
- @throw system_error
- The string contains an invalid percent encoding.
- @tparam String A type convertible to `core::string_view`
- @param s The string to construct from.
- */
- template<
- class String
- #ifndef BOOST_URL_DOCS
- , class = typename std::enable_if<
- std::is_convertible<
- String,
- core::string_view
- >::value>::type
- #endif
- >
- pct_string_view(
- String const& s)
- : pct_string_view(
- detail::to_sv(s))
- {
- }
- /** Constructor (deleted)
- */
- pct_string_view(
- std::nullptr_t) = delete;
- /** Constructor
- The newly constructed string references
- the specified character buffer. Ownership
- is not transferred.
- @par Postconditions
- @code
- this->data() == s && this->size() == len
- @endcode
- @par Complexity
- Linear in `len`.
- @par Exception Safety
- Exceptions thrown on invalid input.
- @throw system_error
- The string contains an invalid percent encoding.
- @param s, len The string to construct from.
- */
- pct_string_view(
- char const* s,
- std::size_t len)
- : pct_string_view(
- core::string_view(s, len))
- {
- }
- /** Constructor
- The newly constructed string references
- the specified character buffer. Ownership
- is not transferred.
- @par Postconditions
- @code
- this->data() == s.data() && this->size() == s.size()
- @endcode
- @par Complexity
- Linear in `s.size()`.
- @par Exception Safety
- Exceptions thrown on invalid input.
- @throw system_error
- The string contains an invalid percent encoding.
- @param s The string to construct from.
- */
- BOOST_URL_DECL
- pct_string_view(
- core::string_view s);
- /** Assignment
- The copy references the same
- underlying character buffer.
- Ownership is not transferred.
- @par Postconditions
- @code
- this->data() == other.data()
- @endcode
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @par other The string to copy.
- */
- pct_string_view& operator=(
- pct_string_view const& other) = default;
- friend
- BOOST_URL_DECL
- system::result<pct_string_view>
- make_pct_string_view(
- core::string_view s) noexcept;
- //--------------------------------------------
- /** Return the decoded size
- This function returns the number of
- characters in the resulting string if
- percent escapes were converted into
- ordinary characters.
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- */
- std::size_t
- decoded_size() const noexcept
- {
- return dn_;
- }
- /** Return the string as a range of decoded characters
- @par Complexity
- Constant.
- @par Exception Safety
- Throws nothing.
- @see
- @ref decode_view.
- */
- decode_view
- operator*() const noexcept;
- /** Return the string with percent-decoding
- This function converts percent escapes
- in the string into ordinary characters
- and returns the result.
- When called with no arguments, the
- return type is `std::string`.
- Otherwise, the return type and style
- of output is determined by which string
- token is passed.
- @par Example
- @code
- assert( pct_string_view( "Program%20Files" ).decode() == "Program Files" );
- @endcode
- @par Complexity
- Linear in `this->size()`.
- @par Exception Safety
- Calls to allocate may throw.
- String tokens may throw exceptions.
- @param opt The options for encoding. If
- this parameter is omitted, the default
- options are used.
- @param token An optional string token.
- If this parameter is omitted, then
- a new `std::string` is returned.
- Otherwise, the function return type
- is the result type of the token.
- @see
- @ref encoding_opts,
- @ref string_token::return_string.
- */
- template<BOOST_URL_STRTOK_TPARAM>
- BOOST_URL_STRTOK_RETURN
- decode(
- encoding_opts opt = {},
- BOOST_URL_STRTOK_ARG(token)) const
- {
- /* If you get a compile error here, it
- means that the token you passed does
- not meet the requirements stated
- in the documentation.
- */
- static_assert(
- string_token::is_token<
- StringToken>::value,
- "Type requirements not met");
- decode_impl(token, opt);
- return token.result();
- }
- #ifndef BOOST_URL_DOCS
- // arrow support
- pct_string_view const*
- operator->() const noexcept
- {
- return this;
- }
- #endif
- //--------------------------------------------
- // VFALCO No idea why this fails in msvc
- /** Swap
- */
- /*BOOST_CXX14_CONSTEXPR*/ void swap(
- pct_string_view& s ) noexcept
- {
- string_view_base::swap(s);
- std::swap(dn_, s.dn_);
- }
- };
- //------------------------------------------------
- #ifndef BOOST_URL_DOCS
- namespace detail {
- // obtain modifiable reference to
- // underlying string, to handle
- // self-intersection on modifiers.
- inline
- core::string_view&
- ref(pct_string_view& s) noexcept
- {
- return s.s_;
- }
- } // detail
- #endif
- //------------------------------------------------
- /** Return a valid percent-encoded string
- If `s` is a valid percent-encoded string,
- the function returns the buffer as a valid
- view which may be used to perform decoding
- or measurements.
- Otherwise the result contains an error code.
- Upon success, the returned view references
- the original character buffer;
- Ownership is not transferred.
- @par Complexity
- Linear in `s.size()`.
- @par Exception Safety
- Throws nothing.
- @param s The string to validate.
- */
- BOOST_URL_DECL
- system::result<pct_string_view>
- make_pct_string_view(
- core::string_view s) noexcept;
- #ifndef BOOST_URL_DOCS
- // VFALCO semi-private for now
- inline
- pct_string_view
- make_pct_string_view_unsafe(
- char const* data,
- std::size_t size,
- std::size_t decoded_size) noexcept
- {
- #if 0
- BOOST_ASSERT(! make_pct_string_view(
- core::string_view(data, size)).has_error());
- #endif
- return pct_string_view(
- data, size, decoded_size);
- }
- #endif
- #ifndef BOOST_URL_DOCS
- namespace detail {
- template <>
- inline
- core::string_view
- to_sv(pct_string_view const& s) noexcept
- {
- return s.substr();
- }
- } // detail
- #endif
- } // urls
- } // boost
- #endif
|