include/boost/url/impl/decode.hpp

97.1% Lines (34/35) 100.0% List of functions (3/3)
decode.hpp
f(x) Functions (3)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Alan de Freitas (alandefreitas@gmail.com)
3 //
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)
6 //
7 // Official repository: https://github.com/boostorg/url
8 //
9
10 #ifndef BOOST_URL_IMPL_DECODE_HPP
11 #define BOOST_URL_IMPL_DECODE_HPP
12
13 #include <boost/assert.hpp>
14 #include <boost/url/detail/decode.hpp>
15 #include <boost/url/detail/string_view.hpp>
16 #include <boost/url/pct_string_view.hpp>
17 #include <utility>
18
19 namespace boost {
20 namespace urls {
21
22 inline
23 system::result<std::size_t>
24 5x decoded_size(core::string_view s) noexcept
25 {
26 5x auto const rv = make_pct_string_view(s);
27 5x if(! rv)
28 1x return rv.error();
29 4x return rv->decoded_size();
30 }
31
32 inline
33 system::result<std::size_t>
34 7x decode(
35 char* dest,
36 std::size_t size,
37 core::string_view s,
38 encoding_opts opt) noexcept
39 {
40 7x auto const rv = make_pct_string_view(s);
41 7x if(! rv)
42 1x return rv.error();
43 12x return detail::decode_unsafe(
44 dest,
45 6x dest + size,
46 6x detail::to_sv(rv.value()),
47 6x opt);
48 }
49
50 template<
51 BOOST_URL_CONSTRAINT(string_token::StringToken) StringToken>
52 system::result<typename StringToken::result_type>
53 7x decode(
54 core::string_view s,
55 encoding_opts opt,
56 StringToken&& token)
57 {
58 static_assert(
59 string_token::is_token<
60 StringToken>::value,
61 "Type requirements not met");
62
63 7x auto const rv = make_pct_string_view(s);
64 7x if(! rv)
65 1x return rv.error();
66
67 6x auto const n = rv->decoded_size();
68 6x auto p = token.prepare(n);
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.
71 6x if(n > 0)
72 12x detail::decode_unsafe(
73 p,
74 6x p + n,
75 6x detail::to_sv(rv.value()),
76 opt);
77 6x return token.result();
78 }
79
80 } // urls
81 } // boost
82
83 #endif
84