Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/url/detail/encode.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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_DETAIL_ENCODE_HPP
0012 #define BOOST_URL_DETAIL_ENCODE_HPP
0013 
0014 #include <boost/url/encoding_opts.hpp>
0015 #include <boost/url/pct_string_view.hpp>
0016 #include <boost/url/grammar/hexdig_chars.hpp>
0017 #include <boost/core/ignore_unused.hpp>
0018 #include <cstdlib>
0019 
0020 namespace boost {
0021 namespace urls {
0022 namespace detail {
0023 
0024 constexpr
0025 char const* const hexdigs[] = {
0026     "0123456789ABCDEF",
0027     "0123456789abcdef" };
0028 
0029 //------------------------------------------------
0030 
0031 // re-encode is to percent-encode a
0032 // string that can already contain
0033 // escapes. Characters not in the
0034 // unreserved set are escaped, and
0035 // escapes are passed through unchanged.
0036 //
0037 template<class CharSet>
0038 std::size_t
0039 re_encoded_size_unsafe(
0040     core::string_view s,
0041     CharSet const& unreserved) noexcept
0042 {
0043     std::size_t n = 0;
0044     auto it = s.begin();
0045     auto const end = s.end();
0046     while(it != end)
0047     {
0048         if(*it != '%')
0049         {
0050             if( unreserved(*it) )
0051                 n += 1;
0052             else
0053                 n += 3;
0054             ++it;
0055         }
0056         else
0057         {
0058             BOOST_ASSERT(end - it >= 3);
0059             BOOST_ASSERT(
0060                     grammar::hexdig_value(
0061                             it[1]) >= 0);
0062             BOOST_ASSERT(
0063                     grammar::hexdig_value(
0064                             it[2]) >= 0);
0065             n += 3;
0066             it += 3;
0067         }
0068     }
0069     return n;
0070 }
0071 
0072 // unchecked
0073 // returns decoded size
0074 template<class CharSet>
0075 std::size_t
0076 re_encode_unsafe(
0077     char*& dest_,
0078     char const* const end,
0079     core::string_view s,
0080     CharSet const& unreserved) noexcept
0081 {
0082     static constexpr bool lower_case = false;
0083     char const* const hex = detail::hexdigs[lower_case];
0084     auto const encode = [end, hex](
0085             char*& dest,
0086             char c0) noexcept
0087     {
0088         auto c = static_cast<unsigned char>(c0);
0089         ignore_unused(end);
0090         *dest++ = '%';
0091         BOOST_ASSERT(dest != end);
0092         *dest++ = hex[c>>4];
0093         BOOST_ASSERT(dest != end);
0094         *dest++ = hex[c&0xf];
0095     };
0096     ignore_unused(end);
0097 
0098     auto dest = dest_;
0099     auto const dest0 = dest;
0100     auto const last = s.end();
0101     std::size_t dn = 0;
0102     auto it = s.begin();
0103     while(it != last)
0104     {
0105         BOOST_ASSERT(dest != end);
0106         if(*it != '%')
0107         {
0108             if(unreserved(*it))
0109             {
0110                 *dest++ = *it;
0111             }
0112             else
0113             {
0114                 encode(dest, *it);
0115                 dn += 2;
0116             }
0117             ++it;
0118         }
0119         else
0120         {
0121             *dest++ = *it++;
0122             BOOST_ASSERT(dest != end);
0123             *dest++ = *it++;
0124             BOOST_ASSERT(dest != end);
0125             *dest++ = *it++;
0126             dn += 2;
0127         }
0128     }
0129     dest_ = dest;
0130     return dest - dest0 - dn;
0131 }
0132 
0133 } // detail
0134 } // urls
0135 } // boost
0136 
0137 #endif