Repository navigation
Expand file tree
/
Copy pathencode.hpp
More file actions
157 lines (147 loc) · 2.97 KB
/
Copy pathencode.hpp
File metadata and controls
157 lines (147 loc) · 2.97 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
// Copyright (c) Glyn Matthews 2011.
// 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)
#ifndef __BOOST_NETWORK_URI_ENCODE_INC__
#define __BOOST_NETWORK_URI_ENCODE_INC__
#include <iterator>
#include <boost/iterator/iterator_traits.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <cassert>
#include <string>
namespace boost {
namespace network {
namespace uri {
namespace detail {
template <typename CharT>
inline CharT hex_to_letter(CharT in) {
switch (in) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
return in + '0';
case 10:
case 11:
case 12:
case 13:
case 14:
default:
return in - 10 + 'A';
}
return CharT();
}
template <typename CharT, class OutputIterator>
void encode_char(CharT in, OutputIterator &out) {
switch (in) {
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
case 'd':
case 'D':
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'h':
case 'H':
case 'i':
case 'I':
case 'j':
case 'J':
case 'k':
case 'K':
case 'l':
case 'L':
case 'm':
case 'M':
case 'n':
case 'N':
case 'o':
case 'O':
case 'p':
case 'P':
case 'q':
case 'Q':
case 'r':
case 'R':
case 's':
case 'S':
case 't':
case 'T':
case 'u':
case 'U':
case 'v':
case 'V':
case 'w':
case 'W':
case 'x':
case 'X':
case 'y':
case 'Y':
case 'z':
case 'Z':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
case '.':
case '_':
case '~':
case '/':
out++ = in;
break;
default:
out++ = '%';
out++ = hex_to_letter((in >> 4) & 0x0f);
out++ = hex_to_letter(in & 0x0f);
;
}
}
} // namespace detail
template <class InputIterator, class OutputIterator>
OutputIterator encode(const InputIterator &in_begin,
const InputIterator &in_end,
const OutputIterator &out_begin) {
InputIterator it = in_begin;
OutputIterator out = out_begin;
while (it != in_end) {
detail::encode_char(*it, out);
++it;
}
return out;
}
template <class SinglePassRange, class OutputIterator>
inline OutputIterator encode(const SinglePassRange &range,
const OutputIterator &out) {
return encode(std::begin(range), std::end(range), out);
}
inline std::string encoded(const std::string &input) {
std::string encoded;
encode(input, std::back_inserter(encoded));
return encoded;
}
} // namespace uri
} // namespace network
} // namespace boost
#endif // __BOOST_NETWORK_URI_ENCODE_INC__