include/boost/url/detail/encode.hpp

100.0% Lines (47/47) 100.0% List of functions (3/3)
encode.hpp
f(x) Functions (3)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/boostorg/url
9 //
10
11 #ifndef BOOST_URL_DETAIL_ENCODE_HPP
12 #define BOOST_URL_DETAIL_ENCODE_HPP
13
14 #include <boost/url/encoding_opts.hpp>
15 #include <boost/url/pct_string_view.hpp>
16 #include <boost/url/grammar/hexdig_chars.hpp>
17 #include <boost/core/ignore_unused.hpp>
18 #include <cstdlib>
19
20 namespace boost {
21 namespace urls {
22 namespace detail {
23
24 constexpr
25 char const* const hexdigs[] = {
26 "0123456789ABCDEF",
27 "0123456789abcdef" };
28
29 //------------------------------------------------
30
31 // re-encode is to percent-encode a
32 // string that can already contain
33 // escapes. Characters not in the
34 // unreserved set are escaped, and
35 // escapes are passed through unchanged.
36 //
37 template<class CharSet>
38 std::size_t
39 1724x re_encoded_size_unsafe(
40 core::string_view s,
41 CharSet const& unreserved) noexcept
42 {
43 1724x std::size_t n = 0;
44 1724x auto it = s.begin();
45 1724x auto const end = s.end();
46 7397x while(it != end)
47 {
48 5673x if(*it != '%')
49 {
50 5443x if( unreserved(*it) )
51 5144x n += 1;
52 else
53 299x n += 3;
54 5443x ++it;
55 }
56 else
57 {
58 230x BOOST_ASSERT(end - it >= 3);
59 230x BOOST_ASSERT(
60 grammar::hexdig_value(
61 it[1]) >= 0);
62 230x BOOST_ASSERT(
63 grammar::hexdig_value(
64 it[2]) >= 0);
65 230x n += 3;
66 230x it += 3;
67 }
68 }
69 1724x return n;
70 }
71
72 // unchecked
73 // returns decoded size
74 template<class CharSet>
75 std::size_t
76 2138x re_encode_unsafe(
77 char*& dest_,
78 char const* const end,
79 core::string_view s,
80 CharSet const& unreserved) noexcept
81 {
82 static constexpr bool lower_case = false;
83 2138x char const* const hex = detail::hexdigs[lower_case];
84 2138x auto const encode = [end, hex](
85 char*& dest,
86 char c0) noexcept
87 {
88 318x auto c = static_cast<unsigned char>(c0);
89 318x ignore_unused(end);
90 318x *dest++ = '%';
91 318x BOOST_ASSERT(dest != end);
92 318x *dest++ = hex[c>>4];
93 318x BOOST_ASSERT(dest != end);
94 318x *dest++ = hex[c&0xf];
95 };
96 ignore_unused(end);
97
98 2138x auto dest = dest_;
99 2138x auto const dest0 = dest;
100 2138x auto const last = s.end();
101 2138x std::size_t dn = 0;
102 2138x auto it = s.begin();
103 8372x while(it != last)
104 {
105 6234x BOOST_ASSERT(dest != end);
106 6234x if(*it != '%')
107 {
108 5978x if(unreserved(*it))
109 {
110 5660x *dest++ = *it;
111 }
112 else
113 {
114 318x encode(dest, *it);
115 318x dn += 2;
116 }
117 5978x ++it;
118 }
119 else
120 {
121 256x *dest++ = *it++;
122 256x BOOST_ASSERT(dest != end);
123 256x *dest++ = *it++;
124 256x BOOST_ASSERT(dest != end);
125 256x *dest++ = *it++;
126 256x dn += 2;
127 }
128 }
129 2138x dest_ = dest;
130 2138x return dest - dest0 - dn;
131 }
132
133 } // detail
134 } // urls
135 } // boost
136
137 #endif
138