src/pct_string_view.cpp

100.0% Lines (33 / 33) 100.0% Functions (3 / 3)
pct_string_view.cpp
f(x) Functions (3)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2022 Vinnie Falco (vinnie.falco@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
11 #include <boost/url/detail/config.hpp>
12 #include <boost/url/error.hpp>
13 #include <boost/url/pct_string_view.hpp>
14 #include <boost/url/detail/decode.hpp>
15 #include <boost/url/grammar/hexdig_chars.hpp>
16 #include <boost/url/detail/except.hpp>
17
18 namespace boost {
19 namespace urls {
20
21 void
22 21185x pct_string_view::
23 decode_impl(
24 string_token::arg& dest,
25 encoding_opts opt) const
26 {
27 21185x auto p = dest.prepare(dn_);
28 21185x if(dn_ > 0)
29 19360x detail::decode_unsafe(
30 19360x p, p + dn_, s_, opt);
31 21185x }
32
33 //------------------------------------------------
34
35 8935x pct_string_view::
36 pct_string_view(
37 8935x core::string_view s)
38 : pct_string_view(
39 8935x make_pct_string_view(s
40 8935x ).value(BOOST_URL_POS))
41 {
42 8698x }
43
44 //------------------------------------------------
45
46 system::result<pct_string_view>
47 8983x make_pct_string_view(
48 core::string_view s) noexcept
49 {
50 8983x auto p = s.begin();
51 8983x auto const end = s.end();
52 8983x std::size_t dn = 0;
53 8983x if(s.size() >= 3)
54 {
55 6273x auto const safe_end = end - 2;
56 1192066x while(p < safe_end)
57 {
58 1185890x if(*p != '%')
59 {
60 962958x ++p;
61 }
62 222932x else if(
63 445778x grammar::hexdig_value(p[1]) >= 0 &&
64 222846x grammar::hexdig_value(p[2]) >= 0)
65 {
66 // percent-escape
67 222835x p += 3;
68 }
69 else
70 {
71 // invalid encoding
72 97x BOOST_URL_RETURN_EC(
73 error::bad_pct_hexdig);
74 }
75 1185793x ++dn;
76 }
77 }
78 8886x auto const n = end - p;
79 8886x if( (n >= 1 && p[0] == '%') ||
80 5208x (n >= 2 && p[1] == '%'))
81 {
82 // invalid encoding
83 143x BOOST_URL_RETURN_EC(
84 error::incomplete_encoding);
85 }
86 8743x dn += n;
87 17486x return make_pct_string_view_unsafe(
88 8743x s.data(), s.size(), dn);
89 }
90
91 } // urls
92 } // boost
93
94