100.00% Lines (24/24) 100.00% Functions (3/3)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Alan de Freitas (alandefreitas@gmail.com) 2   // Copyright (c) 2025 Alan de Freitas (alandefreitas@gmail.com)
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 4   // Distributed under the Boost Software License, Version 1.0. (See accompanying
5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/boostorg/url 7   // Official repository: https://github.com/boostorg/url
8   // 8   //
9   9  
10   #ifndef BOOST_URL_IMPL_DECODE_HPP 10   #ifndef BOOST_URL_IMPL_DECODE_HPP
11   #define BOOST_URL_IMPL_DECODE_HPP 11   #define BOOST_URL_IMPL_DECODE_HPP
12   12  
13   #include <boost/assert.hpp> 13   #include <boost/assert.hpp>
14   #include <boost/url/detail/decode.hpp> 14   #include <boost/url/detail/decode.hpp>
15   #include <boost/url/detail/string_view.hpp> 15   #include <boost/url/detail/string_view.hpp>
16   #include <boost/url/pct_string_view.hpp> 16   #include <boost/url/pct_string_view.hpp>
17   #include <utility> 17   #include <utility>
18   18  
19   namespace boost { 19   namespace boost {
20   namespace urls { 20   namespace urls {
21   21  
22   inline 22   inline
23   system::result<std::size_t> 23   system::result<std::size_t>
HITCBC 24   5 decoded_size(core::string_view s) noexcept 24   5 decoded_size(core::string_view s) noexcept
25   { 25   {
HITCBC 26   5 auto const rv = make_pct_string_view(s); 26   5 auto const rv = make_pct_string_view(s);
HITCBC 27   5 if(! rv) 27   5 if(! rv)
HITCBC 28   1 return rv.error(); 28   1 return rv.error();
HITCBC 29   4 return rv->decoded_size(); 29   4 return rv->decoded_size();
30   } 30   }
31   31  
32   inline 32   inline
33   system::result<std::size_t> 33   system::result<std::size_t>
HITCBC 34   7 decode( 34   7 decode(
35   char* dest, 35   char* dest,
36   std::size_t size, 36   std::size_t size,
37   core::string_view s, 37   core::string_view s,
38   encoding_opts opt) noexcept 38   encoding_opts opt) noexcept
39   { 39   {
HITCBC 40   7 auto const rv = make_pct_string_view(s); 40   7 auto const rv = make_pct_string_view(s);
HITCBC 41   7 if(! rv) 41   7 if(! rv)
HITCBC 42   1 return rv.error(); 42   1 return rv.error();
HITCBC 43   12 return detail::decode_unsafe( 43   12 return detail::decode_unsafe(
44   dest, 44   dest,
HITCBC 45   6 dest + size, 45   6 dest + size,
HITCBC 46   6 detail::to_sv(rv.value()), 46   6 detail::to_sv(rv.value()),
HITCBC 47   6 opt); 47   6 opt);
48   } 48   }
49   49  
50   template< 50   template<
51   BOOST_URL_CONSTRAINT(string_token::StringToken) StringToken> 51   BOOST_URL_CONSTRAINT(string_token::StringToken) StringToken>
52   system::result<typename StringToken::result_type> 52   system::result<typename StringToken::result_type>
HITCBC 53   7 decode( 53   7 decode(
54   core::string_view s, 54   core::string_view s,
55   encoding_opts opt, 55   encoding_opts opt,
56   StringToken&& token) 56   StringToken&& token)
57   { 57   {
58   static_assert( 58   static_assert(
59   string_token::is_token< 59   string_token::is_token<
60   StringToken>::value, 60   StringToken>::value,
61   "Type requirements not met"); 61   "Type requirements not met");
62   62  
HITCBC 63   7 auto const rv = make_pct_string_view(s); 63   7 auto const rv = make_pct_string_view(s);
HITCBC 64   7 if(! rv) 64   7 if(! rv)
HITCBC 65   1 return rv.error(); 65   1 return rv.error();
66   66  
HITCBC 67   6 auto const n = rv->decoded_size(); 67   6 auto const n = rv->decoded_size();
HITCBC 68   6 auto p = token.prepare(n); 68   6 auto p = token.prepare(n);
69   // Some tokens might hand back a null/invalid buffer for n == 0, so skip the 69   // Some tokens might hand back a null/invalid buffer for n == 0, so skip the
70   // decode call entirely in that case to avoid touching unspecified memory. 70   // decode call entirely in that case to avoid touching unspecified memory.
HITCBC 71   6 if(n > 0) 71   6 if(n > 0)
HITCBC 72   12 detail::decode_unsafe( 72   12 detail::decode_unsafe(
73   p, 73   p,
HITCBC 74   6 p + n, 74   6 p + n,
HITCBC 75   6 detail::to_sv(rv.value()), 75   6 detail::to_sv(rv.value()),
76   opt); 76   opt);
HITCBC 77   6 return token.result(); 77   6 return token.result();
78   } 78   }
79   79  
80   } // urls 80   } // urls
81   } // boost 81   } // boost
82   82  
83   #endif 83   #endif