Skip to content

Commit f2165c5

Browse files
authored
document better our UTF-8 expectations. (#871)
1 parent 6169233 commit f2165c5

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

include/ada/url_pattern.h

+7
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ class url_pattern_component {
189189
bool has_regexp_groups = false;
190190
};
191191

192+
// A URLPattern input can be either a string or a URLPatternInit object.
193+
// If it is a string, it must be a valid UTF-8 string.
192194
using url_pattern_input = std::variant<std::string_view, url_pattern_init>;
193195

194196
// A struct providing the URLPattern matching results for all
@@ -221,19 +223,24 @@ struct url_pattern_options {
221223
// defined in https://wicg.github.io/urlpattern.
222224
// More information about the URL Pattern syntax can be found at
223225
// https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API
226+
//
227+
// We require all strings to be valid UTF-8: it is the user's responsibility
228+
// to ensure that the provided strings are valid UTF-8.
224229
template <url_pattern_regex::regex_concept regex_provider>
225230
class url_pattern {
226231
public:
227232
url_pattern() = default;
228233

229234
/**
235+
* If non-null, base_url must pointer at a valid UTF-8 string.
230236
* @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec
231237
*/
232238
result<std::optional<url_pattern_result>> exec(
233239
const url_pattern_input& input,
234240
const std::string_view* base_url = nullptr);
235241

236242
/**
243+
* If non-null, base_url must pointer at a valid UTF-8 string.
237244
* @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-test
238245
*/
239246
result<bool> test(const url_pattern_input& input,

include/ada/url_pattern_init.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
3232
// either a string or a URLPatternInit struct. If a string is given,
3333
// it will be parsed to create a URLPatternInit. The URLPatternInit
3434
// API is defined as part of the URLPattern specification.
35+
// All provided strings must be valid UTF-8.
3536
struct url_pattern_init {
37+
// All strings must be valid UTF-8.
3638
// @see https://urlpattern.spec.whatwg.org/#process-a-urlpatterninit
3739
static tl::expected<url_pattern_init, errors> process(
3840
url_pattern_init init, std::string_view type,
@@ -92,15 +94,23 @@ struct url_pattern_init {
9294
#endif // ADA_TESTING
9395

9496
bool operator==(const url_pattern_init&) const;
95-
97+
// If present, must be valid UTF-8.
9698
std::optional<std::string> protocol{};
99+
// If present, must be valid UTF-8.
97100
std::optional<std::string> username{};
101+
// If present, must be valid UTF-8.
98102
std::optional<std::string> password{};
103+
// If present, must be valid UTF-8.
99104
std::optional<std::string> hostname{};
105+
// If present, must be valid UTF-8.
100106
std::optional<std::string> port{};
107+
// If present, must be valid UTF-8.
101108
std::optional<std::string> pathname{};
109+
// If present, must be valid UTF-8.
102110
std::optional<std::string> search{};
111+
// If present, must be valid UTF-8.
103112
std::optional<std::string> hash{};
113+
// If present, must be valid UTF-8.
104114
std::optional<std::string> base_url{};
105115
};
106116
} // namespace ada

include/ada/url_search_params-inl.h

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ inline void url_search_params::remove(const std::string_view key,
162162
}
163163

164164
inline void url_search_params::sort() {
165+
// We rely on the fact that the content is valid UTF-8.
165166
std::ranges::stable_sort(params, [](const key_value_pair &lhs,
166167
const key_value_pair &rhs) {
167168
size_t i = 0, j = 0;

include/ada/url_search_params.h

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ using url_search_params_entries_iter =
3333
url_search_params_iter_type::ENTRIES>;
3434

3535
/**
36+
* We require all strings to be valid UTF-8. It is the user's responsibility to
37+
* ensure that the provided strings are valid UTF-8.
3638
* @see https://url.spec.whatwg.org/#interface-urlsearchparams
3739
*/
3840
struct url_search_params {
@@ -55,6 +57,7 @@ struct url_search_params {
5557
[[nodiscard]] inline size_t size() const noexcept;
5658

5759
/**
60+
* Both key and value must be valid UTF-8.
5861
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-append
5962
*/
6063
inline void append(std::string_view key, std::string_view value);
@@ -82,6 +85,7 @@ struct url_search_params {
8285
inline bool has(std::string_view key, std::string_view value) noexcept;
8386

8487
/**
88+
* Both key and value must be valid UTF-8.
8589
* @see https://url.spec.whatwg.org/#dom-urlsearchparams-set
8690
*/
8791
inline void set(std::string_view key, std::string_view value);
@@ -145,6 +149,7 @@ struct url_search_params {
145149
std::vector<key_value_pair> params{};
146150

147151
/**
152+
* The init parameter must be valid UTF-8.
148153
* @see https://url.spec.whatwg.org/#concept-urlencoded-parser
149154
*/
150155
void initialize(std::string_view init);

0 commit comments

Comments
 (0)