Line data Source code
1 : //
2 : // Copyright (c) 2022 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 :
11 : #include <boost/url/detail/config.hpp>
12 : #include <boost/url/decode_view.hpp>
13 : #include <boost/url/grammar/hexdig_chars.hpp>
14 : #include <ostream>
15 :
16 : namespace boost {
17 : namespace urls {
18 :
19 : //------------------------------------------------
20 :
21 : auto
22 6299 : decode_view::
23 : iterator::
24 : operator*() const noexcept ->
25 : reference
26 : {
27 6299 : if (space_as_plus_ &&
28 53 : *pos_ == '+')
29 6 : return ' ';
30 6293 : if (*pos_ != '%')
31 5917 : return *pos_;
32 376 : auto d0 = grammar::hexdig_value(pos_[1]);
33 376 : auto d1 = grammar::hexdig_value(pos_[2]);
34 : return static_cast<char>(
35 376 : ((static_cast<
36 376 : unsigned char>(d0) << 4) +
37 376 : (static_cast<
38 376 : unsigned char>(d1))));
39 : }
40 :
41 : void
42 2 : decode_view::
43 : write(std::ostream& os) const
44 : {
45 2 : auto it = begin();
46 2 : auto const end_ = end();
47 23 : while(it != end_)
48 21 : os.put(*it++);
49 2 : }
50 :
51 : void
52 1 : decode_view::
53 : remove_prefix( size_type n )
54 : {
55 1 : auto it = begin();
56 1 : auto n0 = n;
57 3 : while (n)
58 : {
59 2 : ++it;
60 2 : --n;
61 : }
62 1 : n_ -= (it.base() - begin().base());
63 1 : dn_ -= n0;
64 1 : p_ = it.base();
65 1 : }
66 :
67 : void
68 1 : decode_view::
69 : remove_suffix( size_type n )
70 : {
71 1 : auto it = end();
72 1 : auto n0 = n;
73 6 : while (n)
74 : {
75 5 : --it;
76 5 : --n;
77 : }
78 1 : n_ -= (end().base() - it.base());
79 1 : dn_ -= n0;
80 1 : }
81 :
82 : bool
83 3 : decode_view::
84 : starts_with( core::string_view s ) const noexcept
85 : {
86 3 : if (s.size() > size())
87 1 : return false;
88 2 : auto it0 = begin();
89 2 : auto it1 = s.begin();
90 2 : std::size_t n = s.size();
91 11 : while (n)
92 : {
93 10 : if (*it0 != *it1)
94 1 : return false;
95 9 : ++it0;
96 9 : ++it1;
97 9 : --n;
98 : }
99 1 : return true;
100 : }
101 :
102 : bool
103 3 : decode_view::
104 : ends_with( core::string_view s ) const noexcept
105 : {
106 3 : if (s.size() > size())
107 1 : return false;
108 2 : auto it0 = end();
109 2 : auto it1 = s.end();
110 2 : std::size_t n = s.size();
111 2 : --it0;
112 2 : --it1;
113 14 : while (n - 1)
114 : {
115 13 : if (*it0 != *it1)
116 1 : return false;
117 12 : --it0;
118 12 : --it1;
119 12 : --n;
120 : }
121 1 : return *it0 == *it1;
122 : }
123 :
124 : bool
125 1 : decode_view::
126 : starts_with( char ch ) const noexcept
127 : {
128 : return
129 2 : !empty() &&
130 2 : front() == ch;
131 : }
132 :
133 : bool
134 1 : decode_view::
135 : ends_with( char ch ) const noexcept
136 : {
137 : return
138 2 : !empty() &&
139 2 : back() == ch;
140 : }
141 :
142 : decode_view::const_iterator
143 2 : decode_view::
144 : find( char ch ) const noexcept
145 : {
146 2 : auto it = begin();
147 2 : auto end = this->end();
148 8 : while (it != end)
149 : {
150 7 : if (*it == ch)
151 1 : return it;
152 6 : ++it;
153 : }
154 1 : return it;
155 : }
156 :
157 : decode_view::const_iterator
158 5 : decode_view::
159 : rfind( char ch ) const noexcept
160 : {
161 5 : if (empty())
162 1 : return end();
163 4 : auto it = end();
164 4 : auto begin = this->begin();
165 4 : --it;
166 27 : while (it != begin)
167 : {
168 25 : if (*it == ch)
169 2 : return it;
170 23 : --it;
171 : }
172 2 : if (*it == ch)
173 1 : return it;
174 1 : return end();
175 : }
176 :
177 : } // urls
178 : } // boost
179 :
|