Skip to content

Commit 383a746

Browse files
committed
add IsUnreserved func for encode check
1 parent 4e9ce9d commit 383a746

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/iceberg/util/url_encoder.cc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,35 @@
1919

2020
#include "iceberg/util/url_encoder.h"
2121

22+
#include <locale>
23+
2224
namespace iceberg {
2325

2426
namespace {
27+
28+
bool IsUnreserved(unsigned char c) {
29+
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
30+
c == '-' || c == '.' || c == '_' || c == '~';
31+
}
32+
2533
// Helper: convert hex char to int (0–15), returns -1 if invalid
2634
constexpr int8_t FromHex(char c) {
2735
if (c >= '0' && c <= '9') return c - '0';
2836
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
2937
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
3038
return -1;
3139
}
40+
3241
} // namespace
3342

3443
std::string UrlEncoder::Encode(std::string_view str_to_encode) {
3544
static const char* hex_chars = "0123456789ABCDEF";
3645
std::string result;
37-
result.reserve(str_to_encode.size() * 3 /* Worst case: every char becomes %XX */);
46+
result.reserve(str_to_encode.size() * 3 / 2 /* Heuristic reservation */);
3847

39-
for (unsigned char c : str_to_encode) {
40-
if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
41-
result += static_cast<char>(c);
48+
for (char c : str_to_encode) {
49+
if (IsUnreserved(c)) {
50+
result += c;
4251
} else {
4352
result += '%';
4453
result += hex_chars[c >> 4];

src/iceberg/util/url_encoder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <string_view>
2424

2525
#include "iceberg/iceberg_export.h"
26-
#include "iceberg/result.h"
2726

2827
/// \file iceberg/util/url_encoder.h
2928
/// \brief URL encoding and decoding.

0 commit comments

Comments
 (0)