Skip to content

Commit fc68b04

Browse files
committed
Using char instead of std::byte.
1 parent 3a07c5d commit fc68b04

7 files changed

+59
-83
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ Parses a string, "🏳️‍🌈", using a base URL, "https://example.org/":
101101

102102
int main(int argc, char *argv[]) {
103103
auto base = skyr::make_url(http://github.com/"%3Cspan%20class=%22pl-corl%22%3Ehttps://example.org/%3C/span%3E");
104-
auto url = skyr::make_url(http://github.com/"/xf0/x9f/x8f/xb3/xef/xb8/x8f/xe2/x80/x8d/xf0/x9f/x8c/x88",%20base.value());
104+
auto url = skyr::make_url(http://github.com/%3C/div%3E%3C/code%3E%3Cdiv%20aria-hidden=%22true%22%20style=%22left:-2px%22%20class=%22position-absolute%20top-0%20d-flex%20user-select-none%20DiffLineTableCellParts-module__in-progress-comment-indicator--hx3m3%22%3E%3C/div%3E%3Cdiv%20aria-hidden=%22true%22%20class=%22position-absolute%20top-0%20d-flex%20user-select-none%20DiffLineTableCellParts-module__comment-indicator--eI0hb%22%3E%3C/div%3E%3C/td%3E%3C/tr%3E%3Ctr%20class=%22diff-line-row%22%3E%20%3Ctd%20style=%22background-color:var(--diffBlob-additionNum-bgColor,%20var(--diffBlob-addition-bgColor-num));text-align:center" data-grid-cell-id="diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5-104-105-0" data-selected="false" role="gridcell" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
105+
"\xf0\x9f\x8f\xb3\xef\xb8\x8f\xe2\x80\x8d\xf0\x9f\x8c\x88", base.value());
105106
if (url) {
106107
std::cout << url.value().href() << std::endl;
107108
}

include/skyr/percent_encode.hpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ class exclude_set {
3636
public:
3737
/// Tests whether the byte is in the excluded set
3838
/// \param byte Input byte
39-
/// \returns `true` if `in` is in the excluded set, `false` otherwise
40-
bool contains(std::byte byte) const {
41-
return contains_impl(static_cast<char>(byte));
39+
/// \returns `true` if `in` is in the excluded set, `false`
40+
/// otherwise
41+
bool contains(char byte) const {
42+
return contains_impl(byte);
4243
}
4344

4445
private:
@@ -65,7 +66,8 @@ class fragment_set : public exclude_set {
6566
private:
6667
bool contains_impl(char byte) const override {
6768
return
68-
c0_control_set_.contains(std::byte(byte)) || (set_.find(byte) != set_.end());
69+
c0_control_set_.contains(byte) ||
70+
(set_.find(byte) != set_.end());
6971
}
7072

7173
private:
@@ -82,7 +84,7 @@ class query_set : public exclude_set {
8284
private:
8385
bool contains_impl(char byte) const override {
8486
return
85-
fragment_set_.contains(std::byte(byte)) || (byte == 0x27);
87+
fragment_set_.contains(byte) || (byte == 0x27);
8688
}
8789

8890
private:
@@ -98,7 +100,7 @@ class path_set : public exclude_set {
98100
private:
99101
bool contains_impl(char byte) const override {
100102
return
101-
fragment_set_.contains(std::byte(byte)) ||
103+
fragment_set_.contains(byte) ||
102104
(set_.find(byte) != set_.end());
103105
}
104106

@@ -117,7 +119,7 @@ class userinfo_set : public exclude_set {
117119
private:
118120
bool contains_impl(char byte) const override {
119121
return
120-
path_set_.contains(std::byte(byte)) ||
122+
path_set_.contains(byte) ||
121123
(set_.find(byte) != set_.end());
122124
}
123125

@@ -133,7 +135,7 @@ class userinfo_set : public exclude_set {
133135
/// \returns A percent encoded string if `in` is not in the
134136
/// exclude set, `in` as a string otherwise
135137
std::string percent_encode_byte(
136-
std::byte byte, const exclude_set &excludes = c0_control_set());
138+
char byte, const exclude_set &excludes = c0_control_set());
137139

138140
/// Percent encodes a string
139141
/// \param input A string of bytes
@@ -155,7 +157,7 @@ expected<std::string, std::error_code> percent_encode(
155157
/// \param input An string of the for %XX, where X is a hexadecimal
156158
/// value
157159
/// \returns The percent decoded byte, or an error on failure
158-
expected<std::byte, std::error_code> percent_decode_byte(
160+
expected<char, std::error_code> percent_decode_byte(
159161
std::string_view input);
160162

161163
/// Percent decodes a string

src/percent_encode.cpp

+27-25
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,42 @@ std::error_code make_error_code(percent_encode_errc error) {
4141
}
4242

4343
namespace {
44-
inline std::byte hex_to_letter(std::byte byte) {
45-
if ((static_cast<char>(byte) >= 0) && (static_cast<char>(byte) < 10)) {
46-
return static_cast<std::byte>(static_cast<char>(byte) + '0');
44+
inline char hex_to_letter(char byte) {
45+
if ((byte >= 0) && (byte < 10)) {
46+
return byte + '0';
4747
}
4848

49-
if ((static_cast<char>(byte) >= 10) && (static_cast<char>(byte) < 16)) {
50-
return static_cast<std::byte>(static_cast<char>(byte) - char(10) + 'A');
49+
if ((byte >= 10) && (byte < 16)) {
50+
return byte - char(10) + 'A';
5151
}
5252

5353
return byte;
5454
}
5555

56-
inline expected<std::byte, std::error_code> letter_to_hex(std::byte byte) {
57-
if ((static_cast<char>(byte) >= '0') && (static_cast<char>(byte) <= '9')) {
58-
return static_cast<std::byte>(static_cast<char>(byte) - '0');
56+
inline expected<char, std::error_code> letter_to_hex(char byte) {
57+
if ((byte >= '0') && (byte <= '9')) {
58+
return byte - '0';
5959
}
6060

61-
if ((static_cast<char>(byte) >= 'a') && (static_cast<char>(byte) <= 'f')) {
62-
return static_cast<std::byte>(static_cast<char>(byte) + char(10) - 'a');
61+
if ((byte >= 'a') && (byte <= 'f')) {
62+
return byte + char(10) - 'a';
6363
}
6464

65-
if ((static_cast<char>(byte) >= 'A') && (static_cast<char>(byte) <= 'F')) {
66-
return static_cast<std::byte>(static_cast<char>(byte) + char(10) - 'A');
65+
if ((byte >= 'A') && (byte <= 'F')) {
66+
return byte + char(10) - 'A';
6767
}
6868

69-
return make_unexpected(make_error_code(percent_encode_errc::non_hex_input));
69+
return make_unexpected(make_error_code(
70+
percent_encode_errc::non_hex_input));
7071
}
7172
} // namespace
7273

73-
std::string percent_encode_byte(std::byte byte, const exclude_set &excludes) {
74+
std::string percent_encode_byte(char byte, const exclude_set &excludes) {
7475
auto encoded = std::string{};
7576
if (excludes.contains(byte)) {
7677
encoded += '%';
77-
encoded += static_cast<char>(hex_to_letter((byte >> 4) & std::byte(0x0f)));
78-
encoded += static_cast<char>(hex_to_letter(byte & std::byte(0x0f)));
78+
encoded += hex_to_letter((byte >> 4) & 0x0f);
79+
encoded += hex_to_letter(byte & 0x0f);
7980
}
8081
else {
8182
encoded += static_cast<char>(byte);
@@ -89,7 +90,7 @@ expected<std::string, std::error_code> percent_encode(
8990
auto first = begin(input), last = end(input);
9091
auto it = first;
9192
while (it != last) {
92-
result += percent_encode_byte(static_cast<std::byte>(*it), excludes);
93+
result += percent_encode_byte(*it, excludes);
9394
++it;
9495
}
9596
return result;
@@ -99,33 +100,34 @@ expected<std::string, std::error_code> percent_encode(
99100
std::u32string_view input, const exclude_set &excludes) {
100101
auto bytes = utf32_to_bytes(input);
101102
if (!bytes) {
102-
return make_unexpected(make_error_code(percent_encode_errc::overflow));
103+
return make_unexpected(make_error_code(
104+
percent_encode_errc::overflow));
103105
}
104106
return percent_encode(bytes.value(), excludes);
105107
}
106108

107-
expected<std::byte, std::error_code> percent_decode_byte(std::string_view input) {
109+
expected<char, std::error_code> percent_decode_byte(std::string_view input) {
108110
if ((input.size() < 3) || (input.front() != '%')) {
109-
return make_unexpected(make_error_code(percent_encode_errc::non_hex_input));
111+
return make_unexpected(make_error_code(
112+
percent_encode_errc::non_hex_input));
110113
}
111114

112115
auto it = begin(input);
113116
++it;
114117
auto h0 = *it;
115-
auto v0 = letter_to_hex(static_cast<std::byte>(h0));
118+
auto v0 = letter_to_hex(h0);
116119
if (!v0) {
117120
return make_unexpected(std::move(v0.error()));
118121
}
119122

120123
++it;
121124
auto h1 = *it;
122-
auto v1 = letter_to_hex(static_cast<std::byte>(h1));
125+
auto v1 = letter_to_hex(h1);
123126
if (!v1) {
124127
return make_unexpected(std::move(v1.error()));
125128
}
126129

127-
return static_cast<std::byte>(
128-
(0x10 * static_cast<char>(v0.value())) + static_cast<char>(v1.value()));
130+
return (0x10 * v0.value()) + v1.value();
129131
}
130132

131133
expected<std::string, std::error_code> percent_decode(std::string_view input) {
@@ -142,7 +144,7 @@ expected<std::string, std::error_code> percent_decode(std::string_view input) {
142144
if (!byte) {
143145
return make_unexpected(std::move(byte.error()));
144146
}
145-
result.push_back(static_cast<char>(byte.value()));
147+
result.push_back(byte.value());
146148
it += 3;
147149
} else {
148150
result.push_back(*it++);

src/url.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ expected<void, std::error_code> url::set_username(string_type &&username) {
9595

9696
new_url.username.clear();
9797
for (auto c : username) {
98-
auto pct_encoded = percent_encode_byte(static_cast<std::byte>(c), userinfo_set());
98+
auto pct_encoded = percent_encode_byte(c, userinfo_set());
9999
new_url.username += pct_encoded;
100100
}
101101

@@ -115,7 +115,7 @@ expected<void, std::error_code> url::set_password(string_type &&password) {
115115

116116
new_url.password.clear();
117117
for (auto c : password) {
118-
auto pct_encoded = percent_encode_byte(static_cast<std::byte>(c), userinfo_set());
118+
auto pct_encoded = percent_encode_byte(c, userinfo_set());
119119
new_url.password += pct_encoded;
120120
}
121121

src/url_parser_context.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ expected<std::string, url_parse_errc> parse_opaque_host(std::string_view input)
5959

6060
auto output = std::string();
6161
for (auto c : input) {
62-
output += percent_encode_byte(static_cast<std::byte>(c));
62+
output += percent_encode_byte(c);
6363
}
6464
return output;
6565
}
@@ -461,7 +461,7 @@ expected<url_parse_action, url_parse_errc> url_parser_context::parse_authority(c
461461
continue;
462462
}
463463

464-
auto pct_encoded = percent_encode_byte(static_cast<std::byte>(byte), userinfo_set());
464+
auto pct_encoded = percent_encode_byte(byte, userinfo_set());
465465
if (password_token_seen_flag) {
466466
url.password += pct_encoded;
467467
} else {
@@ -795,7 +795,7 @@ expected<url_parse_action, url_parse_errc> url_parser_context::parse_path(char b
795795
url.validation_error = true;
796796
}
797797

798-
buffer += percent_encode_byte(static_cast<std::byte>(byte), path_set());
798+
buffer += percent_encode_byte(byte, path_set());
799799
}
800800

801801
return url_parse_action::increment;
@@ -817,7 +817,7 @@ expected<url_parse_action, url_parse_errc> url_parser_context::parse_cannot_be_a
817817
url.validation_error = true;
818818
}
819819
if (!is_eof()) {
820-
url.path[0] += percent_encode_byte(static_cast<std::byte>(byte));
820+
url.path[0] += percent_encode_byte(byte);
821821
}
822822
}
823823
return url_parse_action::increment;
@@ -832,7 +832,7 @@ expected<url_parse_action, url_parse_errc> url_parser_context::parse_query(char
832832
(byte > '~') ||
833833
(is_in(byte, "\"#<>")) ||
834834
((byte == '\'') && url.is_special())) {
835-
url.query.value() += percent_encode_byte(static_cast<std::byte>(byte), query_set());
835+
url.query.value() += percent_encode_byte(byte, query_set());
836836
} else {
837837
url.query.value().push_back(byte);
838838
}
@@ -844,7 +844,7 @@ expected<url_parse_action, url_parse_errc> url_parser_context::parse_fragment(ch
844844
if (byte == '\0') {
845845
url.validation_error = true;
846846
} else {
847-
url.fragment.value() += percent_encode_byte(static_cast<std::byte>(byte), fragment_set());
847+
url.fragment.value() += percent_encode_byte(byte, fragment_set());
848848
}
849849
return url_parse_action::increment;
850850
}

tests/percent_decoding_tests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99

1010
TEST(percent_decode, decode_codepoints_set) {
11-
for (auto i = 0; i < 0xff; ++i) {
11+
for (auto i = 0x00; i < 0xff; ++i) {
1212
char buffer[8];
1313
std::snprintf(buffer, sizeof(buffer), "%02X", i);
1414
auto input = std::string("%") + buffer;
1515
auto decoded = skyr::percent_decode_byte(input);
1616
ASSERT_TRUE(decoded);
17-
EXPECT_EQ(static_cast<std::byte>(i), decoded.value());
17+
EXPECT_EQ(static_cast<char>(i), decoded.value());
1818
}
1919
}
2020

tests/percent_encoding_tests.cpp

+9-38
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,40 @@
66
#include <gtest/gtest.h>
77
#include <skyr/percent_encode.hpp>
88

9-
class encode_fragment_tests : public ::testing::TestWithParam<std::byte> {};
9+
class encode_fragment_tests : public ::testing::TestWithParam<char> {};
1010

1111
INSTANTIATE_TEST_CASE_P(
1212
encode_fragment,
1313
encode_fragment_tests,
1414
::testing::Values(
15-
std::byte(' '),
16-
std::byte('\"'),
17-
std::byte('<'),
18-
std::byte('>'),
19-
std::byte('`')));
15+
' ', '\"', '<', '>', '`'));
2016

2117
TEST_P(encode_fragment_tests, encode_fragment_set) {
2218
auto encoded = skyr::percent_encode_byte(GetParam(), skyr::fragment_set());
2319
ASSERT_TRUE(skyr::is_percent_encoded(encoded));
2420
}
2521

26-
class encode_path_tests : public ::testing::TestWithParam<std::byte> {};
22+
class encode_path_tests : public ::testing::TestWithParam<char> {};
2723

2824
INSTANTIATE_TEST_CASE_P(
2925
encode_path,
3026
encode_path_tests,
3127
::testing::Values(
32-
std::byte(' '),
33-
std::byte('\"'),
34-
std::byte('<'),
35-
std::byte('>'),
36-
std::byte('`'),
37-
std::byte('#'),
38-
std::byte('?'),
39-
std::byte('{'),
40-
std::byte('}')));
28+
' ', '\"', '<', '>', '`', '#', '?', '{', '}'));
4129

4230
TEST_P(encode_path_tests, encode_path_set) {
4331
auto encoded = skyr::percent_encode_byte(GetParam(), skyr::path_set());
4432
ASSERT_TRUE(skyr::is_percent_encoded(encoded));
4533
}
4634

47-
class encode_userinfo_tests : public ::testing::TestWithParam<std::byte> {};
35+
class encode_userinfo_tests : public ::testing::TestWithParam<char> {};
4836

4937
INSTANTIATE_TEST_CASE_P(
5038
encode_userinfo,
5139
encode_userinfo_tests,
5240
::testing::Values(
53-
std::byte(' '),
54-
std::byte('\"'),
55-
std::byte('<'),
56-
std::byte('>'),
57-
std::byte('`'),
58-
std::byte('#'),
59-
std::byte('?'),
60-
std::byte('{'),
61-
std::byte('}'),
62-
std::byte('/'),
63-
std::byte(':'),
64-
std::byte(';'),
65-
std::byte('='),
66-
std::byte('@'),
67-
std::byte('['),
68-
std::byte('\\'),
69-
std::byte(']'),
70-
std::byte('^'),
71-
std::byte('|')));
41+
' ', '\"', '<', '>', '`', '#', '?', '{', '}', '/',
42+
':', ';', '=', '@', '[', '\\', ']', '^', '|'));
7243

7344
TEST_P(encode_userinfo_tests, encode_userinfo_set) {
7445
auto encoded = skyr::percent_encode_byte(GetParam(), skyr::userinfo_set());
@@ -77,7 +48,7 @@ TEST_P(encode_userinfo_tests, encode_userinfo_set) {
7748

7849
TEST(encode_tests, encode_codepoints_before_0x20_set) {
7950
for (auto i = 0; i < 0x20; ++i) {
80-
auto encoded = skyr::percent_encode_byte(static_cast<std::byte>(i));
51+
auto encoded = skyr::percent_encode_byte(i);
8152
char buffer[8];
8253
std::snprintf(buffer, sizeof(buffer), "%02X", i);
8354
auto output = std::string("%") + buffer;
@@ -87,7 +58,7 @@ TEST(encode_tests, encode_codepoints_before_0x20_set) {
8758

8859
TEST(encode_tests, encode_codepoints_before_0x7e_set) {
8960
for (auto i = 0x7f; i <= 0xff; ++i) {
90-
auto encoded = skyr::percent_encode_byte(static_cast<std::byte>(i));
61+
auto encoded = skyr::percent_encode_byte(i);
9162
char buffer[8];
9263
std::snprintf(buffer, sizeof(buffer), "%02X", i);
9364
auto output = std::string("%") + buffer;

0 commit comments

Comments
 (0)