Skip to content

Commit

Permalink
adding some support for lifetime bounds (#705)
Browse files Browse the repository at this point in the history
* adding some support for lifetime bounds

* lint
  • Loading branch information
lemire committed Jul 26, 2024
1 parent ebb42cc commit 021ae44
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
12 changes: 12 additions & 0 deletions include/ada/common_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,16 @@ namespace ada {
#define ADA_NEON 1
#endif

#ifndef __has_cpp_attribute
#define ada_lifetime_bound
#elif __has_cpp_attribute(msvc::lifetimebound)
#define ada_lifetime_bound [[msvc::lifetimebound]]
#elif __has_cpp_attribute(clang::lifetimebound)
#define ada_lifetime_bound [[clang::lifetimebound]]
#elif __has_cpp_attribute(lifetimebound)
#define ada_lifetime_bound [[lifetimebound]]
#else
#define ada_lifetime_bound
#endif

#endif // ADA_COMMON_DEFS_H
4 changes: 2 additions & 2 deletions include/ada/url_aggregator-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,8 @@ inline bool url_aggregator::has_port() const noexcept {
buffer[components.host_end + 1] == '.';
}

[[nodiscard]] inline std::string_view url_aggregator::get_href()
const noexcept {
[[nodiscard]] inline std::string_view url_aggregator::get_href() const noexcept
ada_lifetime_bound {
ada_log("url_aggregator::get_href");
return buffer;
}
Expand Down
26 changes: 16 additions & 10 deletions include/ada/url_aggregator.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,38 @@ struct url_aggregator : url_base {
* @see https://url.spec.whatwg.org/#dom-url-href
* @see https://url.spec.whatwg.org/#concept-url-serializer
*/
[[nodiscard]] inline std::string_view get_href() const noexcept;
[[nodiscard]] inline std::string_view get_href() const noexcept
ada_lifetime_bound;
/**
* The username getter steps are to return this's URL's username.
* This function does not allocate memory.
* @return a lightweight std::string_view.
* @see https://url.spec.whatwg.org/#dom-url-username
*/
[[nodiscard]] std::string_view get_username() const noexcept;
[[nodiscard]] std::string_view get_username() const noexcept
ada_lifetime_bound;
/**
* The password getter steps are to return this's URL's password.
* This function does not allocate memory.
* @return a lightweight std::string_view.
* @see https://url.spec.whatwg.org/#dom-url-password
*/
[[nodiscard]] std::string_view get_password() const noexcept;
[[nodiscard]] std::string_view get_password() const noexcept
ada_lifetime_bound;
/**
* Return this's URL's port, serialized.
* This function does not allocate memory.
* @return a lightweight std::string_view.
* @see https://url.spec.whatwg.org/#dom-url-port
*/
[[nodiscard]] std::string_view get_port() const noexcept;
[[nodiscard]] std::string_view get_port() const noexcept ada_lifetime_bound;
/**
* Return U+0023 (#), followed by this's URL's fragment.
* This function does not allocate memory.
* @return a lightweight std::string_view..
* @see https://url.spec.whatwg.org/#dom-url-hash
*/
[[nodiscard]] std::string_view get_hash() const noexcept;
[[nodiscard]] std::string_view get_hash() const noexcept ada_lifetime_bound;
/**
* Return url's host, serialized, followed by U+003A (:) and url's port,
* serialized.
Expand All @@ -94,23 +97,25 @@ struct url_aggregator : url_base {
* @return a lightweight std::string_view.
* @see https://url.spec.whatwg.org/#dom-url-host
*/
[[nodiscard]] std::string_view get_host() const noexcept;
[[nodiscard]] std::string_view get_host() const noexcept ada_lifetime_bound;
/**
* Return this's URL's host, serialized.
* This function does not allocate memory.
* When there is no host, this function returns the empty view.
* @return a lightweight std::string_view.
* @see https://url.spec.whatwg.org/#dom-url-hostname
*/
[[nodiscard]] std::string_view get_hostname() const noexcept;
[[nodiscard]] std::string_view get_hostname() const noexcept
ada_lifetime_bound;
/**
* The pathname getter steps are to return the result of URL path serializing
* this's URL.
* This function does not allocate memory.
* @return a lightweight std::string_view.
* @see https://url.spec.whatwg.org/#dom-url-pathname
*/
[[nodiscard]] std::string_view get_pathname() const noexcept;
[[nodiscard]] std::string_view get_pathname() const noexcept
ada_lifetime_bound;
/**
* Compute the pathname length in bytes without instantiating a view or a
* string.
Expand All @@ -124,15 +129,16 @@ struct url_aggregator : url_base {
* @return a lightweight std::string_view.
* @see https://url.spec.whatwg.org/#dom-url-search
*/
[[nodiscard]] std::string_view get_search() const noexcept;
[[nodiscard]] std::string_view get_search() const noexcept ada_lifetime_bound;
/**
* The protocol getter steps are to return this's URL's scheme, followed by
* U+003A (:).
* This function does not allocate memory.
* @return a lightweight std::string_view.
* @see https://url.spec.whatwg.org/#dom-url-protocol
*/
[[nodiscard]] std::string_view get_protocol() const noexcept;
[[nodiscard]] std::string_view get_protocol() const noexcept
ada_lifetime_bound;

/**
* A URL includes credentials if its username or password is not the empty
Expand Down
27 changes: 18 additions & 9 deletions src/url_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,8 @@ bool url_aggregator::set_hostname(const std::string_view input) {
return "null";
}

[[nodiscard]] std::string_view url_aggregator::get_username() const noexcept {
[[nodiscard]] std::string_view url_aggregator::get_username() const noexcept
ada_lifetime_bound {
ada_log("url_aggregator::get_username");
if (has_non_empty_username()) {
return helpers::substring(buffer, components.protocol_end + 2,
Expand All @@ -655,7 +656,8 @@ bool url_aggregator::set_hostname(const std::string_view input) {
return "";
}

[[nodiscard]] std::string_view url_aggregator::get_password() const noexcept {
[[nodiscard]] std::string_view url_aggregator::get_password() const noexcept
ada_lifetime_bound {
ada_log("url_aggregator::get_password");
if (has_non_empty_password()) {
return helpers::substring(buffer, components.username_end + 1,
Expand All @@ -664,7 +666,8 @@ bool url_aggregator::set_hostname(const std::string_view input) {
return "";
}

[[nodiscard]] std::string_view url_aggregator::get_port() const noexcept {
[[nodiscard]] std::string_view url_aggregator::get_port() const noexcept
ada_lifetime_bound {
ada_log("url_aggregator::get_port");
if (components.port == url_components::omitted) {
return "";
Expand All @@ -673,7 +676,8 @@ bool url_aggregator::set_hostname(const std::string_view input) {
components.pathname_start);
}

[[nodiscard]] std::string_view url_aggregator::get_hash() const noexcept {
[[nodiscard]] std::string_view url_aggregator::get_hash() const noexcept
ada_lifetime_bound {
ada_log("url_aggregator::get_hash");
// If this's URL's fragment is either null or the empty string, then return
// the empty string. Return U+0023 (#), followed by this's URL's fragment.
Expand All @@ -686,7 +690,8 @@ bool url_aggregator::set_hostname(const std::string_view input) {
return helpers::substring(buffer, components.hash_start);
}

[[nodiscard]] std::string_view url_aggregator::get_host() const noexcept {
[[nodiscard]] std::string_view url_aggregator::get_host() const noexcept
ada_lifetime_bound {
ada_log("url_aggregator::get_host");
// Technically, we should check if there is a hostname, but
// the code below works even if there isn't.
Expand All @@ -704,7 +709,8 @@ bool url_aggregator::set_hostname(const std::string_view input) {
return helpers::substring(buffer, start, components.pathname_start);
}

[[nodiscard]] std::string_view url_aggregator::get_hostname() const noexcept {
[[nodiscard]] std::string_view url_aggregator::get_hostname() const noexcept
ada_lifetime_bound {
ada_log("url_aggregator::get_hostname");
// Technically, we should check if there is a hostname, but
// the code below works even if there isn't.
Expand All @@ -718,7 +724,8 @@ bool url_aggregator::set_hostname(const std::string_view input) {
return helpers::substring(buffer, start, components.host_end);
}

[[nodiscard]] std::string_view url_aggregator::get_pathname() const noexcept {
[[nodiscard]] std::string_view url_aggregator::get_pathname() const noexcept
ada_lifetime_bound {
ada_log("url_aggregator::get_pathname pathname_start = ",
components.pathname_start, " buffer.size() = ", buffer.size(),
" components.search_start = ", components.search_start,
Expand All @@ -732,7 +739,8 @@ bool url_aggregator::set_hostname(const std::string_view input) {
return helpers::substring(buffer, components.pathname_start, ending_index);
}

[[nodiscard]] std::string_view url_aggregator::get_search() const noexcept {
[[nodiscard]] std::string_view url_aggregator::get_search() const noexcept
ada_lifetime_bound {
ada_log("url_aggregator::get_search");
// If this's URL's query is either null or the empty string, then return the
// empty string. Return U+003F (?), followed by this's URL's query.
Expand All @@ -749,7 +757,8 @@ bool url_aggregator::set_hostname(const std::string_view input) {
return helpers::substring(buffer, components.search_start, ending_index);
}

[[nodiscard]] std::string_view url_aggregator::get_protocol() const noexcept {
[[nodiscard]] std::string_view url_aggregator::get_protocol() const noexcept
ada_lifetime_bound {
ada_log("url_aggregator::get_protocol");
return helpers::substring(buffer, 0, components.protocol_end);
}
Expand Down

0 comments on commit 021ae44

Please sign in to comment.