Skip to content

Commit f0559f1

Browse files
authored
percent_encode and percent_decode are no longer template functions and return std::string (#123)
1 parent 7b463ef commit f0559f1

10 files changed

Lines changed: 19 additions & 21 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ int main() {
142142
std::cout << "Port: " << url.port<std::uint16_t>().value() << std::endl;
143143

144144
std::cout << "Pathname: "
145-
<< skyr::percent_decode<std::string>(url.pathname()).value() << std::endl;
145+
<< skyr::percent_decode(url.pathname()).value() << std::endl;
146146

147147
std::cout << "Search parameters:" << std::endl;
148148
const auto &search = url.search_parameters();

examples/example_06.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ int main(int argc, char *argv[]) {
1111
using skyr::percent_decode;
1212

1313
auto url = skyr::url("http://example.org/\xf0\x9f\x92\xa9");
14-
auto value = percent_decode<std::string>(url.record().path.back()).value();
14+
auto value = percent_decode(url.record().path.back()).value();
1515
std::cout << value << std::endl;
1616
}

examples/example_07.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ int main(int argc, char *argv[]) {
1212

1313
auto url = skyr::url(
1414
"https://example.org/\xf0\x9f\x8f\xb3\xef\xb8\x8f\xe2\x80\x8d\xf0\x9f\x8c\x88");
15-
std::cout << percent_decode<std::string>(url.href()).value() << std::endl;
15+
std::cout << percent_decode(url.href()).value() << std::endl;
1616
}

examples/example_08.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int main(int argc, char *argv[]) {
1414
"https://example.org/?q=\xf0\x9f\x8f\xb3\xef\xb8\x8f\xe2\x80\x8d\xf0\x9f\x8c\x88&key=e1f7bc78");
1515
url.search_parameters().sort();
1616
for (auto [name, value] : url.search_parameters()) {
17-
auto decoded_value = percent_decode<std::string>(value).value();
17+
auto decoded_value = percent_decode(value).value();
1818
std::cout << name << ": " << decoded_value << std::endl;
1919
}
2020
}

include/skyr/v1/filesystem/path.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ inline auto from_path(const stdfs::path &path) -> tl::expected<url, url_parse_er
4040
/// \returns a path object or an error on failure
4141
inline auto to_path(const url &input) -> tl::expected<stdfs::path, path_errc> {
4242
auto pathname = input.pathname();
43-
auto decoded = skyr::percent_decode<std::string>(pathname);
43+
auto decoded = skyr::percent_decode(pathname);
4444
if (!decoded) {
4545
return tl::make_unexpected(path_errc::percent_decoding_error);
4646
}

include/skyr/v1/json/json.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ inline auto encode_query(const nlohmann::json &json, char separator='&', char eq
3737
for (auto &[key, value] : json.items()) {
3838

3939
if (value.is_string()) {
40-
result += percent_encode<std::string>(key);
40+
result += percent_encode(key);
4141
result += equal;
42-
result += percent_encode<std::string>(value.get<std::string>());
42+
result += percent_encode(value.get<std::string>());
4343
result += separator;
4444
}
4545
else if (value.is_array()) {
4646
for (auto &element : value.items()) {
47-
result += percent_encode<std::string>(key);
47+
result += percent_encode(key);
4848
result += equal;
49-
result += percent_encode<std::string>(element.value().get<std::string>());
49+
result += percent_encode(element.value().get<std::string>());
5050
result += separator;
5151
}
5252
}
5353
else {
54-
result += percent_encode<std::string>(key);
54+
result += percent_encode(key);
5555
result += equal;
5656
result += separator;
5757
}
@@ -67,8 +67,8 @@ inline auto decode_query(std::string_view query, char separator='&', char equal=
6767

6868
nlohmann::json object;
6969
for (auto [key, value] : query_parameter_range(query)) {
70-
const auto key_ = skyr::percent_decode<std::string>(key).value();
71-
const auto value_ = value? skyr::percent_decode<std::string>(value.value()).value() : std::string();
70+
const auto key_ = skyr::percent_decode(key).value();
71+
const auto value_ = value? skyr::percent_decode(value.value()).value() : std::string();
7272

7373
if (object.contains(key_)) {
7474
auto current_value = object[key_];

include/skyr/v1/percent_encoding/percent_decode.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ namespace skyr {
1515
inline namespace v1 {
1616
/// Percent decodes the input
1717
/// \returns The percent decoded output when successful, an error otherwise.
18-
template <class Output>
1918
inline auto percent_decode(std::string_view input) {
2019
using namespace v1::percent_encoding;
21-
return as<Output>(input | views::decode);
20+
return as<std::string>(input | views::decode);
2221
}
2322
} // namespace v1
2423
} // namespace skyr

include/skyr/v1/percent_encoding/percent_encode.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ namespace skyr {
1515
inline namespace v1 {
1616
/// Percent encodes the input
1717
/// \returns The percent encoded output when successful, an error otherwise.
18-
template <class Output>
1918
inline auto percent_encode(std::string_view input) {
2019
using namespace v1::percent_encoding;
21-
return as<Output>(input | views::encode);
20+
return as<std::string>(input | views::encode);
2221
}
2322
} // namespace v1
2423
} // namespace skyr

src/v1/core/host.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ auto parse_host(
6868
[] (auto &&h) -> tl::expected<host, url_parse_errc> { return host{h}; });
6969
}
7070

71-
auto domain = percent_decode<std::string>(input);
71+
auto domain = percent_decode(input);
7272
if (!domain) {
7373
return tl::make_unexpected(url_parse_errc::cannot_decode_host_point);
7474
}

src/v1/url/url_search_parameters.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ auto url_search_parameters::to_string() const -> string_type {
9393
else {
9494
result.append("&");
9595
}
96-
result.append(percent_encode<string_type>(name));
96+
result.append(percent_encode(name));
9797
result.append("=");
98-
result.append(percent_encode<string_type>(value));
98+
result.append(percent_encode(value));
9999
}
100100

101101
return result;
@@ -107,8 +107,8 @@ void url_search_parameters::initialize(std::string_view query) {
107107
}
108108

109109
for (auto [name, value] : query_parameter_range(query)) {
110-
auto name_ = percent_decode<std::string>(name).value_or(std::string(name));
111-
auto value_ = value? percent_decode<std::string>(value.value()).value_or(std::string(value.value())) : std::string();
110+
auto name_ = percent_decode(name).value_or(std::string(name));
111+
auto value_ = value? percent_decode(value.value()).value_or(std::string(value.value())) : std::string();
112112
parameters_.emplace_back(name_, value_);
113113
}
114114
}

0 commit comments

Comments
 (0)