Skip to content

Commit 63cb72a

Browse files
authored
introduce a new method (set_host_to_base_host) to replace set_host when set_host is unnecessary (#762)
* introduce a new method (set_host_to_base_host) to replace parse_host when parse_host is unnecessary * lint * changed the function name
1 parent e685c55 commit 63cb72a

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

include/ada/url_aggregator-inl.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,28 @@ inline std::ostream &operator<<(std::ostream &out,
11091109
const ada::url_aggregator &u) {
11101110
return out << u.to_string();
11111111
}
1112+
1113+
void url_aggregator::update_host_to_base_host(
1114+
const std::string_view input) noexcept {
1115+
ada_log("url_aggregator::update_host_to_base_host ", input);
1116+
ADA_ASSERT_TRUE(validate());
1117+
ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
1118+
if (type != ada::scheme::type::FILE) {
1119+
// Let host be the result of host parsing host_view with url is not special.
1120+
if (input.empty() && !is_special()) {
1121+
if (has_hostname()) {
1122+
clear_hostname();
1123+
} else if (has_dash_dot()) {
1124+
add_authority_slashes_if_needed();
1125+
delete_dash_dot();
1126+
}
1127+
return;
1128+
}
1129+
}
1130+
update_base_hostname(input);
1131+
ADA_ASSERT_TRUE(validate());
1132+
return;
1133+
}
11121134
} // namespace ada
11131135

11141136
#endif // ADA_URL_AGGREGATOR_INL_H

include/ada/url_aggregator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ struct url_aggregator : url_base {
313313
std::string_view new_scheme_with_colon) noexcept;
314314
inline void copy_scheme(const url_aggregator &u) noexcept;
315315

316+
inline void update_host_to_base_host(const std::string_view input) noexcept;
317+
316318
}; // url_aggregator
317319

318320
inline std::ostream &operator<<(std::ostream &out, const ada::url &u);

src/parser.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,7 @@ result_type parse_url_impl(std::string_view user_input,
427427
} else {
428428
url.update_base_authority(base_url->get_href(),
429429
base_url->get_components());
430-
// TODO: Get rid of set_hostname and replace it with
431-
// update_base_hostname
432-
url.set_hostname(base_url->get_hostname());
430+
url.update_host_to_base_host(base_url->get_hostname());
433431
url.update_base_port(base_url->retrieve_base_port());
434432
// cloning the base path includes cloning the has_opaque_path flag
435433
url.has_opaque_path = base_url->has_opaque_path;
@@ -497,9 +495,7 @@ result_type parse_url_impl(std::string_view user_input,
497495
} else {
498496
url.update_base_authority(base_url->get_href(),
499497
base_url->get_components());
500-
// TODO: Get rid of set_hostname and replace it with
501-
// update_base_hostname
502-
url.set_hostname(base_url->get_hostname());
498+
url.update_host_to_base_host(base_url->get_hostname());
503499
url.update_base_port(base_url->retrieve_base_port());
504500
}
505501
state = ada::state::PATH;
@@ -736,8 +732,7 @@ result_type parse_url_impl(std::string_view user_input,
736732
if constexpr (result_type_is_ada_url) {
737733
url.host = base_url->host;
738734
} else {
739-
// TODO: Optimization opportunity.
740-
url.set_host(base_url->get_host());
735+
url.update_host_to_base_host(base_url->get_host());
741736
}
742737
// If the code point substring from pointer to the end of input does
743738
// not start with a Windows drive letter and base's path[0] is a
@@ -848,9 +843,7 @@ result_type parse_url_impl(std::string_view user_input,
848843
url.path = base_url->path;
849844
url.query = base_url->query;
850845
} else {
851-
// TODO: Get rid of set_hostname and replace it with
852-
// update_base_hostname
853-
url.set_hostname(base_url->get_hostname());
846+
url.update_host_to_base_host(base_url->get_hostname());
854847
url.update_base_pathname(base_url->get_pathname());
855848
url.update_base_search(base_url->get_search());
856849
}

0 commit comments

Comments
 (0)