Skip to content

Commit 9737407

Browse files
committed
further reduce unnecessary string copies
1 parent a8ce381 commit 9737407

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

include/ada/url_pattern-inl.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include "ada/url_pattern_helpers.h"
1010
#include "ada/url_pattern.h"
1111

12+
#include <algorithm>
1213
#include <string_view>
14+
#include <utility>
1315

1416
namespace ada {
1517

@@ -330,36 +332,34 @@ result<std::optional<url_pattern_result>> url_pattern<regex_provider>::match(
330332
// https://github.com/cloudflare/workerd/blob/8620d14012513a6ce04d079e401d3becac3c67bd/src/workerd/jsg/url.c%2B%2B#L2038
331333
protocol = url->get_protocol().substr(0, url->get_protocol().size() - 1);
332334
// Set username to url’s username.
333-
username = url->get_username();
335+
username = std::move(url->get_username());
334336
// Set password to url’s password.
335-
password = url->get_password();
337+
password = std::move(url->get_password());
336338
// Set hostname to url’s host, serialized, or the empty string if the value
337339
// is null.
338-
hostname = url->get_hostname();
340+
hostname = std::move(url->get_hostname());
339341
// Set port to url’s port, serialized, or the empty string if the value is
340342
// null.
341-
port = url->get_port();
343+
port = std::move(url->get_port());
342344
// Set pathname to the result of URL path serializing url.
343-
pathname = url->get_pathname();
345+
pathname = std::move(url->get_pathname());
344346
// Set search to url’s query or the empty string if the value is null.
345347
// IMPORTANT: Not documented on the URLPattern spec, but search prefix '?'
346348
// is removed. Similar work was done on workerd:
347349
// https://github.com/cloudflare/workerd/blob/8620d14012513a6ce04d079e401d3becac3c67bd/src/workerd/jsg/url.c%2B%2B#L2232
348350
if (url->has_search()) {
349351
auto view = url->get_search();
350-
search = view.starts_with("?") ? url->get_search().substr(1) : view;
351-
} else {
352-
search = "";
352+
search =
353+
view.starts_with("?") ? url->get_search().substr(1) : std::move(view);
353354
}
354355
// Set hash to url’s fragment or the empty string if the value is null.
355356
// IMPORTANT: Not documented on the URLPattern spec, but hash prefix '#' is
356357
// removed. Similar work was done on workerd:
357358
// https://github.com/cloudflare/workerd/blob/8620d14012513a6ce04d079e401d3becac3c67bd/src/workerd/jsg/url.c%2B%2B#L2242
358359
if (url->has_hash()) {
359360
auto view = url->get_hash();
360-
hash = view.starts_with("#") ? url->get_hash().substr(1) : view;
361-
} else {
362-
hash = "";
361+
hash =
362+
view.starts_with("#") ? url->get_hash().substr(1) : std::move(view);
363363
}
364364
}
365365

src/url_pattern_helpers.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ generate_regular_expression_and_name_list(
1515

1616
// Let name list be a new list
1717
std::vector<std::string> name_list{};
18-
const std::string full_wildcard_regexp_value = ".*";
1918

2019
// For each part of part list:
2120
for (const url_pattern_part& part : part_list) {
@@ -61,7 +60,7 @@ generate_regular_expression_and_name_list(
6160
// Otherwise if part's type is "full-wildcard"
6261
else if (part.type == url_pattern_part_type::FULL_WILDCARD) {
6362
// then set regexp value to full wildcard regexp value.
64-
regexp_value = full_wildcard_regexp_value;
63+
regexp_value = ".*";
6564
}
6665

6766
// If part's prefix is the empty string and part's suffix is the empty
@@ -140,7 +139,7 @@ generate_regular_expression_and_name_list(
140139
result += "$";
141140

142141
// Return (result, name list)
143-
return {result, name_list};
142+
return {std::move(result), std::move(name_list)};
144143
}
145144

146145
bool is_ipv6_address(std::string_view input) noexcept {

0 commit comments

Comments
 (0)