8
8
9
9
#include < string>
10
10
#include < locale>
11
+ #include < cstddef>
11
12
12
13
namespace skyr {
13
14
inline namespace v1 {
14
15
namespace percent_encoding {
15
16
namespace details {
16
- inline constexpr auto hex_to_letter (char byte) noexcept {
17
- if ((byte >= ' \x00 ' ) && (byte < ' \x0a ' )) {
18
- return static_cast <char >(byte + ' 0' );
17
+ // /
18
+ // / \param value
19
+ // / \return
20
+ inline constexpr auto hex_to_alnum (std::byte value) noexcept {
21
+ if ((value >= std::byte (0x00 )) && (value < std::byte (0x0a ))) {
22
+ return static_cast <char >(std::to_integer<unsigned >(value) + ' 0' );
19
23
}
20
24
21
- if ((byte >= ' \x0a ' ) && (byte < ' \x10 ' )) {
22
- return static_cast <char >(byte - ' \x0a ' + ' A' );
25
+ if ((value >= std::byte ( 0x0a )) && (value < std::byte ( 0x10 ) )) {
26
+ return static_cast <char >(std::to_integer< unsigned >(value) - ' \x0a ' + ' A' );
23
27
}
24
28
25
- return byte ;
29
+ return static_cast < char >(value) ;
26
30
}
27
31
28
- inline constexpr auto is_c0_control_byte (char byte) noexcept {
29
- return (byte <= ' \x1f ' ) || (byte > ' \x7e ' );
32
+ // /
33
+ // / \param value
34
+ // / \return
35
+ inline constexpr auto is_c0_control_byte (std::byte value) noexcept {
36
+ return (value <= std::byte (0x1f )) || (value > std::byte (0x7e ));
30
37
}
31
38
32
- inline constexpr auto is_fragment_byte (char byte) {
39
+ // /
40
+ // / \param value
41
+ // / \return
42
+ inline constexpr auto is_fragment_byte (std::byte value) {
33
43
return
34
- is_c0_control_byte (byte ) ||
35
- (byte == ' \x20 ' ) ||
36
- (byte == ' \x22 ' ) ||
37
- (byte == ' \x3c ' ) ||
38
- (byte == ' \x3e ' ) ||
39
- (byte == ' \x60 ' );
44
+ is_c0_control_byte (value ) ||
45
+ (value == std::byte ( 0x20 ) ) ||
46
+ (value == std::byte ( 0x22 ) ) ||
47
+ (value == std::byte ( 0x3c ) ) ||
48
+ (value == std::byte ( 0x3e ) ) ||
49
+ (value == std::byte ( 0x60 ) );
40
50
}
41
51
42
- inline constexpr auto is_path_byte (char byte) {
52
+ // /
53
+ // / \param value
54
+ // / \return
55
+ inline constexpr auto is_path_byte (std::byte value) {
43
56
return
44
- is_fragment_byte (byte ) ||
45
- (byte == ' \x23 ' ) ||
46
- (byte == ' \x3f ' ) ||
47
- (byte == ' \x7b ' ) ||
48
- (byte == ' \x7d ' );
57
+ is_fragment_byte (value ) ||
58
+ (value == std::byte ( 0x23 ) ) ||
59
+ (value == std::byte ( 0x3f ) ) ||
60
+ (value == std::byte ( 0x7b ) ) ||
61
+ (value == std::byte ( 0x7d ) );
49
62
}
50
63
51
- inline constexpr auto is_userinfo_byte (char byte) {
64
+ // /
65
+ // / \param value
66
+ // / \return
67
+ inline constexpr auto is_userinfo_byte (std::byte value) {
52
68
return
53
- is_path_byte (byte ) ||
54
- (byte == ' \x2f ' ) ||
55
- (byte == ' \x3a ' ) ||
56
- (byte == ' \x3b ' ) ||
57
- (byte == ' \x3d ' ) ||
58
- (byte == ' \x40 ' ) ||
59
- (byte == ' \x5b ' ) ||
60
- (byte == ' \x5c ' ) ||
61
- (byte == ' \x5d ' ) ||
62
- (byte == ' \x5e ' ) ||
63
- (byte == ' \x7c ' );
69
+ is_path_byte (value ) ||
70
+ (value == std::byte ( 0x2f ) ) ||
71
+ (value == std::byte ( 0x3a ) ) ||
72
+ (value == std::byte ( 0x3b ) ) ||
73
+ (value == std::byte ( 0x3d ) ) ||
74
+ (value == std::byte ( 0x40 ) ) ||
75
+ (value == std::byte ( 0x5b ) ) ||
76
+ (value == std::byte ( 0x5c ) ) ||
77
+ (value == std::byte ( 0x5d ) ) ||
78
+ (value == std::byte ( 0x5e ) ) ||
79
+ (value == std::byte ( 0x7c ) );
64
80
}
65
81
} // namespace details
66
82
@@ -83,6 +99,8 @@ struct percent_encoded_char {
83
99
84
100
using impl_type = std::string;
85
101
102
+ static constexpr std::byte mask = std::byte(0x0f );
103
+
86
104
public:
87
105
88
106
// /
@@ -101,27 +119,38 @@ struct percent_encoded_char {
101
119
percent_encoded_char () = default;
102
120
103
121
// /
104
- // / \param byte
105
- percent_encoded_char (char byte, no_encode)
106
- : impl_{byte} {}
122
+ // / \param value
123
+ percent_encoded_char (std::byte value, no_encode)
124
+ : impl_{static_cast <char >(value)} {}
125
+
107
126
// /
108
- // / \param byte
109
- explicit percent_encoded_char (char byte)
127
+ // / \param value
128
+ explicit percent_encoded_char (std:: byte value )
110
129
: impl_{
111
- ' %' ,
112
- details::hex_to_letter (static_cast <char >((static_cast <unsigned >(byte) >> 4u ) & 0x0fu )),
113
- details::hex_to_letter (static_cast <char >(static_cast <unsigned >(byte) & 0x0fu ))} {}
130
+ ' %' , details::hex_to_alnum ((value >> 4u ) & mask), details::hex_to_alnum (value & mask)} {}
131
+
132
+ // /
133
+ // / \return
134
+ [[nodiscard]] auto cbegin () const noexcept {
135
+ return impl_.cbegin ();
136
+ }
137
+
138
+ // /
139
+ // / \return
140
+ [[nodiscard]] auto cend () const noexcept {
141
+ return impl_.cend ();
142
+ }
114
143
115
144
// /
116
145
// / \return
117
146
[[nodiscard]] auto begin () const noexcept {
118
- return impl_. begin ();
147
+ return cbegin ();
119
148
}
120
149
121
150
// /
122
151
// / \return
123
152
[[nodiscard]] auto end () const noexcept {
124
- return impl_. end ();
153
+ return cend ();
125
154
}
126
155
127
156
// /
@@ -160,7 +189,7 @@ struct percent_encoded_char {
160
189
// / \param pred
161
190
// / \return
162
191
template <class Pred >
163
- inline auto percent_encode_byte (char byte, Pred pred) -> percent_encoded_char {
192
+ inline auto percent_encode_byte (std::byte byte, Pred pred) -> percent_encoded_char {
164
193
if (pred (byte)) {
165
194
return percent_encoding::percent_encoded_char (byte);
166
195
}
@@ -169,23 +198,23 @@ inline auto percent_encode_byte(char byte, Pred pred) -> percent_encoded_char {
169
198
}
170
199
171
200
// /
172
- // / \param byte
201
+ // / \param value
173
202
// / \param excludes
174
203
// / \return
175
- inline auto percent_encode_byte (char byte, encode_set excludes) -> percent_encoded_char {
204
+ inline auto percent_encode_byte (std:: byte value , encode_set excludes) -> percent_encoded_char {
176
205
switch (excludes) {
177
206
case encode_set::none:
178
- return percent_encoding::percent_encoded_char (byte );
207
+ return percent_encoding::percent_encoded_char (value );
179
208
case encode_set::c0_control:
180
- return percent_encode_byte (byte , details::is_c0_control_byte);
209
+ return percent_encode_byte (value , details::is_c0_control_byte);
181
210
case encode_set::userinfo:
182
- return percent_encode_byte (byte , details::is_userinfo_byte);
211
+ return percent_encode_byte (value , details::is_userinfo_byte);
183
212
case encode_set::path:
184
- return percent_encode_byte (byte , details::is_path_byte);
213
+ return percent_encode_byte (value , details::is_path_byte);
185
214
case encode_set::fragment:
186
- return percent_encode_byte (byte , details::is_fragment_byte);
215
+ return percent_encode_byte (value , details::is_fragment_byte);
187
216
}
188
- return percent_encoding::percent_encoded_char (byte );
217
+ return percent_encoding::percent_encoded_char (value );
189
218
}
190
219
191
220
// / Tests whether the input string contains percent encoded values
0 commit comments