Skip to content

Commit

Permalink
introduce a new method (set_host_to_base_host) to replace set_host wh…
Browse files Browse the repository at this point in the history
…en 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
  • Loading branch information
lemire authored Oct 16, 2024
1 parent e685c55 commit 63cb72a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
22 changes: 22 additions & 0 deletions include/ada/url_aggregator-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,28 @@ inline std::ostream &operator<<(std::ostream &out,
const ada::url_aggregator &u) {
return out << u.to_string();
}

void url_aggregator::update_host_to_base_host(
const std::string_view input) noexcept {
ada_log("url_aggregator::update_host_to_base_host ", input);
ADA_ASSERT_TRUE(validate());
ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
if (type != ada::scheme::type::FILE) {
// Let host be the result of host parsing host_view with url is not special.
if (input.empty() && !is_special()) {
if (has_hostname()) {
clear_hostname();
} else if (has_dash_dot()) {
add_authority_slashes_if_needed();
delete_dash_dot();
}
return;
}
}
update_base_hostname(input);
ADA_ASSERT_TRUE(validate());
return;
}
} // namespace ada

#endif // ADA_URL_AGGREGATOR_INL_H
2 changes: 2 additions & 0 deletions include/ada/url_aggregator.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ struct url_aggregator : url_base {
std::string_view new_scheme_with_colon) noexcept;
inline void copy_scheme(const url_aggregator &u) noexcept;

inline void update_host_to_base_host(const std::string_view input) noexcept;

}; // url_aggregator

inline std::ostream &operator<<(std::ostream &out, const ada::url &u);
Expand Down
15 changes: 4 additions & 11 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,7 @@ result_type parse_url_impl(std::string_view user_input,
} else {
url.update_base_authority(base_url->get_href(),
base_url->get_components());
// TODO: Get rid of set_hostname and replace it with
// update_base_hostname
url.set_hostname(base_url->get_hostname());
url.update_host_to_base_host(base_url->get_hostname());
url.update_base_port(base_url->retrieve_base_port());
// cloning the base path includes cloning the has_opaque_path flag
url.has_opaque_path = base_url->has_opaque_path;
Expand Down Expand Up @@ -497,9 +495,7 @@ result_type parse_url_impl(std::string_view user_input,
} else {
url.update_base_authority(base_url->get_href(),
base_url->get_components());
// TODO: Get rid of set_hostname and replace it with
// update_base_hostname
url.set_hostname(base_url->get_hostname());
url.update_host_to_base_host(base_url->get_hostname());
url.update_base_port(base_url->retrieve_base_port());
}
state = ada::state::PATH;
Expand Down Expand Up @@ -736,8 +732,7 @@ result_type parse_url_impl(std::string_view user_input,
if constexpr (result_type_is_ada_url) {
url.host = base_url->host;
} else {
// TODO: Optimization opportunity.
url.set_host(base_url->get_host());
url.update_host_to_base_host(base_url->get_host());
}
// If the code point substring from pointer to the end of input does
// not start with a Windows drive letter and base's path[0] is a
Expand Down Expand Up @@ -848,9 +843,7 @@ result_type parse_url_impl(std::string_view user_input,
url.path = base_url->path;
url.query = base_url->query;
} else {
// TODO: Get rid of set_hostname and replace it with
// update_base_hostname
url.set_hostname(base_url->get_hostname());
url.update_host_to_base_host(base_url->get_hostname());
url.update_base_pathname(base_url->get_pathname());
url.update_base_search(base_url->get_search());
}
Expand Down

0 comments on commit 63cb72a

Please sign in to comment.