encode.hpp
6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/url
//
#ifndef BOOST_URL_IMPL_ENCODE_HPP
#define BOOST_URL_IMPL_ENCODE_HPP
#include <boost/url/detail/encode.hpp>
#include <boost/url/detail/except.hpp>
#include <boost/url/encoding_opts.hpp>
#include <boost/url/grammar/charset.hpp>
#include <boost/url/grammar/hexdig_chars.hpp>
#include <boost/url/grammar/type_traits.hpp>
#include <boost/assert.hpp>
#include <boost/static_assert.hpp>
namespace boost {
namespace urls {
//------------------------------------------------
template<class CharSet>
std::size_t
encoded_size(
string_view s,
CharSet const& unreserved,
encoding_opts opt) noexcept
{
/* If you get a compile error here, it
means that the value you passed does
not meet the requirements stated in
the documentation.
*/
static_assert(
grammar::is_charset<CharSet>::value,
"Type requirements not met");
std::size_t n = 0;
auto it = s.data();
auto const last = it + s.size();
if(! opt.space_as_plus ||
unreserved(' '))
{
while(it != last)
{
if(unreserved(*it))
n += 1;
else
n += 3;
++it;
}
}
else
{
while(it != last)
{
auto c = *it;
if(unreserved(c))
++n;
else if(c == ' ')
++n;
else
n += 3;
++it;
}
}
return n;
}
//------------------------------------------------
template<class CharSet>
std::size_t
encode(
char* dest,
std::size_t size,
string_view s,
CharSet const& unreserved,
encoding_opts opt)
{
/* If you get a compile error here, it
means that the value you passed does
not meet the requirements stated in
the documentation.
*/
static_assert(
grammar::is_charset<CharSet>::value,
"Type requirements not met");
// '%' must be reserved
BOOST_ASSERT(! unreserved('%'));
char const* const hex =
detail::hexdigs[opt.lower_case];
auto const encode = [hex](
char*& dest,
unsigned char c) noexcept
{
*dest++ = '%';
*dest++ = hex[c>>4];
*dest++ = hex[c&0xf];
};
auto it = s.data();
auto const end = dest + size;
auto const last = it + s.size();
auto const dest0 = dest;
auto const end3 = end - 3;
if(! opt.space_as_plus)
{
while(it != last)
{
if(unreserved(*it))
{
if(dest == end)
return dest - dest0;
*dest++ = *it++;
continue;
}
if(dest > end3)
return dest - dest0;
encode(dest, *it++);
}
return dest - dest0;
}
else if(! unreserved(' '))
{
// VFALCO space is usually reserved,
// and we depend on this for an
// optimization. if this assert
// goes off we can split the loop
// below into two versions.
BOOST_ASSERT(! unreserved(' '));
while(it != last)
{
if(unreserved(*it))
{
if(dest == end)
return dest - dest0;
*dest++ = *it++;
continue;
}
if(*it == ' ')
{
if(dest == end)
return dest - dest0;
*dest++ = '+';
++it;
continue;
}
if(dest > end3)
return dest - dest0;
encode(dest, *it++);
}
}
return dest - dest0;
}
//------------------------------------------------
// unsafe encode just
// asserts on the output buffer
//
template<class CharSet>
std::size_t
encode_unsafe(
char* dest,
std::size_t size,
string_view s,
CharSet const& unreserved,
encoding_opts opt)
{
// '%' must be reserved
BOOST_ASSERT(! unreserved('%'));
auto it = s.data();
auto const last = it + s.size();
auto const end = dest + size;
ignore_unused(end);
char const* const hex =
detail::hexdigs[opt.lower_case];
auto const encode = [end, hex](
char*& dest,
unsigned char c) noexcept
{
ignore_unused(end);
*dest++ = '%';
BOOST_ASSERT(dest != end);
*dest++ = hex[c>>4];
BOOST_ASSERT(dest != end);
*dest++ = hex[c&0xf];
};
auto const dest0 = dest;
if(! opt.space_as_plus)
{
while(it != last)
{
BOOST_ASSERT(dest != end);
if(unreserved(*it))
*dest++ = *it++;
else
encode(dest, *it++);
}
}
else
{
// VFALCO space is usually reserved,
// and we depend on this for an
// optimization. if this assert
// goes off we can split the loop
// below into two versions.
BOOST_ASSERT(! unreserved(' '));
while(it != last)
{
BOOST_ASSERT(dest != end);
if(unreserved(*it))
{
*dest++ = *it++;
}
else if(*it == ' ')
{
*dest++ = '+';
++it;
}
else
{
encode(dest, *it++);
}
}
}
return dest - dest0;
}
//------------------------------------------------
template<
class StringToken,
class CharSet>
BOOST_URL_STRTOK_RETURN
encode(
string_view s,
CharSet const& unreserved,
encoding_opts opt,
StringToken&& token) noexcept
{
/* If you get a compile error here, it
means that the value you passed does
not meet the requirements stated in
the documentation.
*/
static_assert(
grammar::is_charset<CharSet>::value,
"Type requirements not met");
auto const n = encoded_size(
s, unreserved, opt);
auto p = token.prepare(n);
if(n > 0)
encode_unsafe(
p, n, s, unreserved, opt);
return token.result();
}
} // urls
} // boost
#endif