| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | 1086 | re_encoded_size_unsafe( | |
| 40 | core::string_view s, | ||
| 41 | CharSet const& unreserved) noexcept | ||
| 42 | { | ||
| 43 | 1086 | std::size_t n = 0; | |
| 44 | 1086 | auto it = s.begin(); | |
| 45 | 1086 | auto const end = s.end(); | |
| 46 |
2/2✓ Branch 0 taken 3685 times.
✓ Branch 1 taken 1086 times.
|
4771 | while(it != end) |
| 47 | { | ||
| 48 |
2/2✓ Branch 0 taken 3526 times.
✓ Branch 1 taken 159 times.
|
3685 | if(*it != '%') |
| 49 | { | ||
| 50 |
2/2✓ Branch 1 taken 3319 times.
✓ Branch 2 taken 207 times.
|
3526 | if( unreserved(*it) ) |
| 51 | 3319 | n += 1; | |
| 52 | else | ||
| 53 | 207 | n += 3; | |
| 54 | 3526 | ++it; | |
| 55 | } | ||
| 56 | else | ||
| 57 | { | ||
| 58 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
|
159 | BOOST_ASSERT(end - it >= 3); |
| 59 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 159 times.
|
159 | BOOST_ASSERT( |
| 60 | grammar::hexdig_value( | ||
| 61 | it[1]) >= 0); | ||
| 62 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 159 times.
|
159 | BOOST_ASSERT( |
| 63 | grammar::hexdig_value( | ||
| 64 | it[2]) >= 0); | ||
| 65 | 159 | n += 3; | |
| 66 | 159 | it += 3; | |
| 67 | } | ||
| 68 | } | ||
| 69 | 1086 | return n; | |
| 70 | } | ||
| 71 | |||
| 72 | // unchecked | ||
| 73 | // returns decoded size | ||
| 74 | template<class CharSet> | ||
| 75 | std::size_t | ||
| 76 | 1286 | 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 | 1286 | char const* const hex = detail::hexdigs[lower_case]; | |
| 84 | 1286 | auto const encode = [end, hex]( | |
| 85 | char*& dest, | ||
| 86 | char c0) noexcept | ||
| 87 | { | ||
| 88 | 213 | auto c = static_cast<unsigned char>(c0); | |
| 89 | 213 | ignore_unused(end); | |
| 90 | 213 | *dest++ = '%'; | |
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
|
213 | BOOST_ASSERT(dest != end); |
| 92 | 213 | *dest++ = hex[c>>4]; | |
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 213 times.
|
213 | BOOST_ASSERT(dest != end); |
| 94 | 213 | *dest++ = hex[c&0xf]; | |
| 95 | }; | ||
| 96 | ignore_unused(end); | ||
| 97 | |||
| 98 | 1286 | auto dest = dest_; | |
| 99 | 1286 | auto const dest0 = dest; | |
| 100 | 1286 | auto const last = s.end(); | |
| 101 | 1286 | std::size_t dn = 0; | |
| 102 | 1286 | auto it = s.begin(); | |
| 103 |
2/2✓ Branch 0 taken 3859 times.
✓ Branch 1 taken 1286 times.
|
5145 | while(it != last) |
| 104 | { | ||
| 105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3859 times.
|
3859 | BOOST_ASSERT(dest != end); |
| 106 |
2/2✓ Branch 0 taken 3692 times.
✓ Branch 1 taken 167 times.
|
3859 | if(*it != '%') |
| 107 | { | ||
| 108 |
2/2✓ Branch 1 taken 3479 times.
✓ Branch 2 taken 213 times.
|
3692 | if(unreserved(*it)) |
| 109 | { | ||
| 110 | 3479 | *dest++ = *it; | |
| 111 | } | ||
| 112 | else | ||
| 113 | { | ||
| 114 | 213 | encode(dest, *it); | |
| 115 | 213 | dn += 2; | |
| 116 | } | ||
| 117 | 3692 | ++it; | |
| 118 | } | ||
| 119 | else | ||
| 120 | { | ||
| 121 | 167 | *dest++ = *it++; | |
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 167 times.
|
167 | BOOST_ASSERT(dest != end); |
| 123 | 167 | *dest++ = *it++; | |
| 124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 167 times.
|
167 | BOOST_ASSERT(dest != end); |
| 125 | 167 | *dest++ = *it++; | |
| 126 | 167 | dn += 2; | |
| 127 | } | ||
| 128 | } | ||
| 129 | 1286 | dest_ = dest; | |
| 130 | 1286 | return dest - dest0 - dn; | |
| 131 | } | ||
| 132 | |||
| 133 | } // detail | ||
| 134 | } // urls | ||
| 135 | } // boost | ||
| 136 | |||
| 137 | #endif | ||
| 138 |