Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:53:30

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
0004 //
0005 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 // Official repository: https://github.com/boostorg/url
0009 //
0010 
0011 #ifndef BOOST_URL_URL_VIEW_HPP
0012 #define BOOST_URL_URL_VIEW_HPP
0013 
0014 #include <boost/url/detail/config.hpp>
0015 #include <boost/url/url_view_base.hpp>
0016 #include <utility>
0017 
0018 namespace boost {
0019 namespace urls {
0020 
0021 /** A non-owning reference to a valid URL 
0022 
0023     Objects of this type represent valid URL
0024     strings constructed from a parsed, external
0025     character buffer whose storage is managed
0026     by the caller. That is, it acts like a
0027     `core::string_view` in terms of ownership.
0028     The caller is responsible for ensuring
0029     that the lifetime of the underlying
0030     character buffer extends until it is no
0031     longer referenced.
0032 
0033     @par Example 1
0034     Construction from a string parses the input
0035     as a <em>URI-reference</em> and throws an
0036     exception on error. Upon success, the
0037     constructed object points to the passed
0038     character buffer; ownership is not
0039     transferred.
0040     @code
0041     url_view u( "https://www.example.com/index.htm?text=none#a1" );
0042     @endcode
0043 
0044     @par Example 2
0045     Parsing functions like @ref parse_uri_reference
0046     return a @ref result containing either a valid
0047     @ref url_view upon succcess, otherwise they
0048     contain an error. The error can be converted to
0049     an exception by the caller if desired:
0050     @code
0051     system::result< url_view > rv = parse_uri_reference( "https://www.example.com/index.htm?text=none#a1" );
0052     @endcode
0053 
0054     @par BNF
0055     @code
0056     URI-reference = URI / relative-ref
0057 
0058     URI           = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
0059 
0060     relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
0061     @endcode
0062 
0063     @par Specification
0064     @li <a href="https://tools.ietf.org/html/rfc3986"
0065         >Uniform Resource Identifier (URI): Generic Syntax (rfc3986)</a>
0066 
0067     @see
0068         @ref parse_absolute_uri,
0069         @ref parse_origin_form,
0070         @ref parse_relative_ref,
0071         @ref parse_uri,
0072         @ref parse_uri_reference.
0073 */
0074 class BOOST_URL_DECL url_view
0075     : public url_view_base
0076 {
0077     friend std::hash<url_view>;
0078     friend class url_view_base;
0079     friend class params_base;
0080     friend class params_encoded_base;
0081 
0082 #ifndef BOOST_URL_DOCS
0083     // VFALCO docca emits this erroneously
0084     friend struct detail::url_impl;
0085 #endif
0086 
0087     using url_view_base::digest;
0088 
0089     explicit
0090     url_view(
0091         detail::url_impl const& impl) noexcept
0092         : url_view_base(impl)
0093     {
0094     }
0095 
0096 public:
0097     //--------------------------------------------
0098     //
0099     // Special Members
0100     //
0101     //--------------------------------------------
0102 
0103     /** Destructor
0104 
0105         Any params, segments, iterators, or
0106         other views which reference the same
0107         underlying character buffer remain
0108         valid.
0109     */
0110     ~url_view() = default;
0111 
0112     /** Constructor
0113 
0114         Default constructed views refer to
0115         a string with zero length, which
0116         always remains valid. This matches
0117         the grammar for a relative-ref with
0118         an empty path and no query or
0119         fragment.
0120 
0121         @par Example
0122         @code
0123         url_view u;
0124         @endcode
0125 
0126         @par Postconditions
0127         @code
0128         this->empty() == true
0129         @endcode
0130 
0131         @par Complexity
0132         Constant.
0133 
0134         @par Exception Safety
0135         Throws nothing.
0136 
0137         @par BNF
0138         @code
0139         relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
0140         @endcode
0141 
0142         @par Specification
0143         <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-4.2"
0144             >4.2. Relative Reference (rfc3986)</a>
0145     */
0146     url_view() noexcept;
0147 
0148     /** Constructor
0149 
0150         This function constructs a URL from
0151         the string `s`, which must contain a
0152         valid <em>URI</em> or <em>relative-ref</em>
0153         or else an exception is thrown. Upon
0154         successful construction, the view
0155         refers to the characters in the
0156         buffer pointed to by `s`.
0157         Ownership is not transferred; The caller
0158         is responsible for ensuring that the
0159         lifetime of the buffer extends until
0160         it is no longer referenced.
0161 
0162         @par Example
0163         @code
0164         url_view u( "http://www.example.com/index.htm" );
0165         @endcode
0166 
0167         @par Effects
0168         @code
0169         return parse_uri_reference( s ).value();
0170         @endcode
0171 
0172         @par Complexity
0173         Linear in `s.size()`.
0174 
0175         @par Exception Safety
0176         Exceptions thrown on invalid input.
0177 
0178         @throw system_error
0179         The input failed to parse correctly.
0180 
0181         @param s The string to parse.
0182 
0183         @par BNF
0184         @code
0185         URI           = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
0186 
0187         relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
0188         @endcode
0189 
0190         @par Specification
0191         @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-4.1"
0192             >4.1. URI Reference</a>
0193 
0194         @see
0195             @ref parse_uri_reference.
0196     */
0197     url_view(core::string_view s);
0198 
0199     /// @copydoc url_view(core::string_view)
0200     template<
0201         class String
0202 #ifndef BOOST_URL_DOCS
0203         , class = typename std::enable_if<
0204             std::is_convertible<
0205                 String,
0206                 core::string_view
0207                     >::value &&
0208             !std::is_convertible<
0209                 String*,
0210                 url_view_base*
0211                     >::value
0212             >::type
0213 #endif
0214     >
0215     url_view(
0216         String const& s)
0217         : url_view(
0218             detail::to_sv(s))
0219     {
0220     }
0221 
0222     /** Constructor
0223 
0224         After construction, both views
0225         reference the same underlying character
0226         buffer. Ownership is not transferred.
0227 
0228         @par Postconditions
0229         @code
0230         this->buffer().data() == other.buffer().data()
0231         @endcode
0232 
0233         @par Complexity
0234         Constant.
0235 
0236         @par Exception Safety
0237         Throws nothing.
0238     */
0239     url_view(
0240         url_view const& other) noexcept
0241         : url_view(static_cast<
0242             url_view_base const&>(other))
0243     {
0244     }
0245 
0246     /** Constructor
0247 
0248         After construction, both views
0249         reference the same underlying character
0250         buffer. Ownership is not transferred.
0251 
0252         @par Postconditions
0253         @code
0254         this->buffer().data() == other.buffer().data()
0255         @endcode
0256 
0257         @par Complexity
0258         Constant.
0259 
0260         @par Exception Safety
0261         Throws nothing.
0262     */
0263     url_view(
0264         url_view_base const& other) noexcept;
0265 
0266     /** Assignment
0267 
0268         After assignment, both views
0269         reference the same underlying character
0270         buffer. Ownership is not transferred.
0271 
0272         @par Postconditions
0273         @code
0274         this->buffer().data() == other.buffer().data()
0275         @endcode
0276 
0277         @par Complexity
0278         Constant.
0279 
0280         @par Exception Safety
0281         Throws nothing.
0282     */
0283     url_view&
0284     operator=(
0285         url_view const& other) noexcept
0286     {
0287         if (this != &other)
0288             *this = static_cast<
0289                 url_view_base const&>(other);
0290         return *this;
0291     }
0292 
0293     /** Assignment
0294 
0295         After assignment, both views
0296         reference the same underlying character
0297         buffer. Ownership is not transferred.
0298 
0299         @par Postconditions
0300         @code
0301         this->buffer().data() == other.buffer().data()
0302         @endcode
0303 
0304         @par Complexity
0305         Constant.
0306 
0307         @par Exception Safety
0308         Throws nothing.
0309     */
0310     url_view& operator=(
0311         url_view_base const& other) noexcept;
0312 
0313     //--------------------------------------------
0314     //
0315     // Observers
0316     //
0317     //--------------------------------------------
0318 
0319     /** Return the maximum number of characters possible
0320 
0321         This represents the largest number of
0322         characters that are possible in a url,
0323         not including any null terminator.
0324 
0325         @par Complexity
0326         Constant.
0327 
0328         @par Exception Safety
0329         Throws nothing.
0330     */
0331     static
0332     constexpr
0333     std::size_t
0334     max_size() noexcept
0335     {
0336         return BOOST_URL_MAX_SIZE;
0337     }
0338 };
0339 
0340 } // urls
0341 } // boost
0342 
0343 //------------------------------------------------
0344 
0345 // std::hash specialization
0346 #ifndef BOOST_URL_DOCS
0347 namespace std {
0348 template<>
0349 struct hash< ::boost::urls::url_view >
0350 {
0351     hash() = default;
0352     hash(hash const&) = default;
0353     hash& operator=(hash const&) = default;
0354 
0355     explicit
0356     hash(std::size_t salt) noexcept
0357         : salt_(salt)
0358     {
0359     }
0360 
0361     std::size_t
0362     operator()(::boost::urls::url_view const& u) const noexcept
0363     {
0364         return u.digest(salt_);
0365     }
0366 
0367 private:
0368     std::size_t salt_ = 0;
0369 };
0370 } // std
0371 #endif
0372 
0373 #endif