src/decode_view.cpp

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