include/boost/url/rfc/impl/pct_encoded_rule.hpp

100.0% Lines (36/36) 100.0% Functions (4/4) 100.0% Branches (18/18)
include/boost/url/rfc/impl/pct_encoded_rule.hpp
Line Branch Hits Source Code
1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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_RFC_IMPL_PCT_ENCODED_RULE_HPP
12 #define BOOST_URL_RFC_IMPL_PCT_ENCODED_RULE_HPP
13
14 #include <boost/url/grammar/charset.hpp>
15 #include <boost/url/grammar/error.hpp>
16 #include <boost/url/grammar/hexdig_chars.hpp>
17
18 namespace boost {
19 namespace urls {
20
21 namespace detail {
22
23 template<class CharSet>
24 BOOST_URL_CXX14_CONSTEXPR
25 auto
26 9964 parse_encoded(
27 char const*& it,
28 char const* end,
29 CharSet const& cs) noexcept ->
30 system::result<pct_string_view>
31 {
32 9964 auto const start = it;
33 9964 std::size_t n = 0;
34 356 for(;;)
35 {
36 10320 auto it0 = it;
37 10320 it = grammar::find_if_not(
38 it0, end, cs);
39 10320 n += it - it0;
40
4/4
✓ Branch 0 taken 7353 times.
✓ Branch 1 taken 2967 times.
✓ Branch 2 taken 455 times.
✓ Branch 3 taken 6898 times.
10320 if(it == end || *it != '%')
41 break;
42 455 bool at_end = false;
43 212 for(;;)
44 {
45 667 ++it;
46
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 653 times.
667 if(it == end)
47 {
48 // expected HEXDIG
49 14 BOOST_URL_CONSTEXPR_RETURN_EC(
50 grammar::error::invalid);
51 }
52 653 auto r = grammar::hexdig_value(*it);
53
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 641 times.
653 if(r < 0)
54 {
55 // expected HEXDIG
56 12 BOOST_URL_CONSTEXPR_RETURN_EC(
57 grammar::error::invalid);
58 }
59 641 ++it;
60
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 636 times.
641 if(it == end)
61 {
62 // expected HEXDIG
63 5 BOOST_URL_CONSTEXPR_RETURN_EC(
64 grammar::error::invalid);
65 }
66 636 r = grammar::hexdig_value(*it);
67
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 628 times.
636 if(r < 0)
68 {
69 // expected HEXDIG
70 8 BOOST_URL_CONSTEXPR_RETURN_EC(
71 grammar::error::invalid);
72 }
73 628 ++n;
74 628 ++it;
75
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 568 times.
628 if(it == end)
76 {
77 60 at_end = true;
78 60 break;
79 }
80
2/2
✓ Branch 0 taken 356 times.
✓ Branch 1 taken 212 times.
568 if(*it != '%')
81 356 break;
82 }
83
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 356 times.
416 if(at_end)
84 60 break;
85 }
86 9925 return make_pct_string_view_unsafe(
87 9925 start, it - start, n);
88 }
89
90 } // detail
91
92 //------------------------------------------------
93
94 template<class CharSet>
95 BOOST_URL_CXX14_CONSTEXPR
96 auto
97 9964 implementation_defined::pct_encoded_rule_t<CharSet>::
98 parse(
99 char const*& it,
100 char const* end) const noexcept ->
101 system::result<value_type>
102 {
103 9964 return detail::parse_encoded(
104 9964 it, end, cs_);
105 }
106
107 } // urls
108 } // boost
109
110 #endif
111