-
Notifications
You must be signed in to change notification settings - Fork 78
feat: add simple url encoder & decoder #457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
+225
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wgtmac
reviewed
Dec 30, 2025
util for common use
HuaHuaY
reviewed
Dec 31, 2025
wgtmac
reviewed
Jan 2, 2026
wgtmac
reviewed
Jan 2, 2026
Member
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Putting together all my comments, the code may look like this (disclaimer: gemini wrote it):
namespace {
bool IsUnreserved(unsigned char c) {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
c == '-' || c == '.' || c == '_' || c == '~';
}
char ToHex(unsigned char v) {
static constexpr std::array<char, 16> kHexChars = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
return kHexChars[v & 0x0F];
}
int FromHex(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
} // namespace
std::string UrlEncoder::Encode(std::string_view str_to_encode) {
std::string escaped;
escaped.reserve(str_to_encode.size() * 3 / 2); // Heuristic reservation
for (unsigned char c : str_to_encode) {
if (IsUnreserved(c)) {
escaped += static_cast<char>(c);
} else {
escaped += '%';
escaped += ToHex(c >> 4);
escaped += ToHex(c);
}
}
return escaped;
}
std::string UrlEncoder::Decode(std::string_view str_to_decode) {
std::string result;
result.reserve(str_to_decode.size());
for (size_t i = 0; i < str_to_decode.size(); ++i) {
char c = str_to_decode[i];
if (c == '%' && i + 2 < str_to_decode.size()) {
int h1 = FromHex(str_to_decode[i + 1]);
int h2 = FromHex(str_to_decode[i + 2]);
if (h1 >= 0 && h2 >= 0) {
result += static_cast<char>((h1 << 4) | h2);
i += 2;
} else {
result += c;
}
} else if (c == '+') {
result += ' ';
} else {
result += c;
}
}
return result;
}
Contributor
Author
|
Rewrite the code translate from curl_easy_escape, curl_easy_unescape |
wgtmac
reviewed
Jan 5, 2026
wgtmac
approved these changes
Jan 5, 2026
HuaHuaY
approved these changes
Jan 5, 2026
Member
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The url encode and decode function will be used for generating partitions' path, so move it to the common util for later use.