Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions include/ada/unicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ ada_really_inline unsigned constexpr convert_hex_to_binary(char c) noexcept;
std::string percent_decode(std::string_view input, size_t first_percent);

/**
* @private
* Decode an application/x-www-form-urlencoded component: map '+' to space,
* then percent-decode. Single allocation; no intermediate string.
*
Expand All @@ -207,6 +208,20 @@ std::string percent_decode(std::string_view input, size_t first_percent);
*/
std::string form_urlencoded_decode(std::string_view input);

/**
* @private
* application/x-www-form-urlencoded byte serializer: percent-encode with the
* form set, mapping 0x20 SPACE to '+' instead of '%20'.
* @see https://url.spec.whatwg.org/#concept-urlencoded-byte-serializer
*/
std::string form_urlencoded_encode(std::string_view input);

/**
* @private
* Append a form-urlencoded component to `out` (no intermediate string).
*/
void form_urlencoded_encode_append(std::string_view input, std::string& out);

/**
* @private
* Returns a percent-encoding string whether percent encoding was needed or not.
Expand Down
17 changes: 4 additions & 13 deletions include/ada/url_search_params-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#ifndef ADA_URL_SEARCH_PARAMS_INL_H
#define ADA_URL_SEARCH_PARAMS_INL_H

#include "ada/character_sets-inl.h"
#include "ada/unicode.h"
#include "ada/url_search_params.h"

Expand Down Expand Up @@ -120,22 +119,14 @@ inline bool url_search_params::has(std::string_view key,
}

inline std::string url_search_params::to_string() const {
auto character_set = ada::character_sets::WWW_FORM_URLENCODED_PERCENT_ENCODE;
std::string out{};
for (size_t i = 0; i < params.size(); i++) {
auto key = ada::unicode::percent_encode(params[i].first, character_set);
auto value = ada::unicode::percent_encode(params[i].second, character_set);

// Performance optimization: Move this inside percent_encode.
std::ranges::replace(key, ' ', '+');
std::ranges::replace(value, ' ', '+');

if (i != 0) {
out += "&";
out += '&';
}
out.append(key);
out += "=";
out.append(value);
unicode::form_urlencoded_encode_append(params[i].first, out);
out += '=';
unicode::form_urlencoded_encode_append(params[i].second, out);
}
return out;
}
Expand Down
Loading
Loading