Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert more functions to constexpr #736

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions include/ada/url-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,19 @@ inline void url::update_base_port(std::optional<uint16_t> input) {
port = input;
}

inline void url::clear_pathname() { path.clear(); }
constexpr void url::clear_pathname() { path.clear(); }

inline void url::clear_search() { query = std::nullopt; }
constexpr void url::clear_search() { query = std::nullopt; }

[[nodiscard]] inline bool url::has_hash() const noexcept {
[[nodiscard]] constexpr bool url::has_hash() const noexcept {
return hash.has_value();
}

[[nodiscard]] inline bool url::has_search() const noexcept {
[[nodiscard]] constexpr bool url::has_search() const noexcept {
return query.has_value();
}

inline void url::set_protocol_as_file() { type = ada::scheme::type::FILE; }
constexpr void url::set_protocol_as_file() { type = ada::scheme::type::FILE; }

inline void url::set_scheme(std::string &&new_scheme) noexcept {
type = ada::scheme::get_scheme_type(new_scheme);
Expand All @@ -170,12 +170,12 @@ inline void url::set_scheme(std::string &&new_scheme) noexcept {
}
}

inline void url::copy_scheme(ada::url &&u) noexcept {
constexpr void url::copy_scheme(ada::url &&u) noexcept {
non_special_scheme = u.non_special_scheme;
type = u.type;
}

inline void url::copy_scheme(const ada::url &u) {
constexpr void url::copy_scheme(const ada::url &u) {
non_special_scheme = u.non_special_scheme;
type = u.type;
}
Expand Down
26 changes: 13 additions & 13 deletions include/ada/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ struct url : url_base {
* @return a newly allocated string.
* @see https://url.spec.whatwg.org/#dom-url-pathname
*/
[[nodiscard]] std::string_view get_pathname() const noexcept;
[[nodiscard]] constexpr std::string_view get_pathname() const noexcept;

/**
* Compute the pathname length in bytes without instantiating a view or a
Expand Down Expand Up @@ -283,9 +283,9 @@ struct url : url_base {
[[nodiscard]] ada_really_inline ada::url_components get_components()
const noexcept;
/** @return true if the URL has a hash component */
[[nodiscard]] inline bool has_hash() const noexcept override;
[[nodiscard]] constexpr bool has_hash() const noexcept override;
/** @return true if the URL has a search component */
[[nodiscard]] inline bool has_search() const noexcept override;
[[nodiscard]] constexpr bool has_search() const noexcept override;

private:
friend ada::url ada::parser::parse_url<ada::url>(std::string_view,
Expand Down Expand Up @@ -361,12 +361,6 @@ struct url : url_base {
return this->parse_port(view, false);
}

/**
* Take the scheme from another URL. The scheme string is copied from the
* provided url.
*/
inline void copy_scheme(const ada::url &u);

/**
* Parse the host from the provided input. We assume that
* the input does not contain spaces or tabs. Control
Expand All @@ -380,9 +374,9 @@ struct url : url_base {
template <bool has_state_override = false>
[[nodiscard]] ada_really_inline bool parse_scheme(std::string_view input);

inline void clear_pathname() override;
inline void clear_search() override;
inline void set_protocol_as_file();
constexpr void clear_pathname() override;
constexpr void clear_search() override;
constexpr void set_protocol_as_file();

/**
* Parse the path from the provided input.
Expand All @@ -407,7 +401,13 @@ struct url : url_base {
* Take the scheme from another URL. The scheme string is moved from the
* provided url.
*/
inline void copy_scheme(ada::url &&u) noexcept;
constexpr void copy_scheme(ada::url &&u) noexcept;

/**
* Take the scheme from another URL. The scheme string is copied from the
* provided url.
*/
constexpr void copy_scheme(const ada::url &u);

}; // struct url

Expand Down
36 changes: 18 additions & 18 deletions include/ada/url_aggregator-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ inline void url_aggregator::append_base_username(const std::string_view input) {
ADA_ASSERT_TRUE(validate());
}

inline void url_aggregator::clear_password() {
constexpr void url_aggregator::clear_password() {
ada_log("url_aggregator::clear_password ", to_string(), "\n", to_diagram());
ADA_ASSERT_TRUE(validate());
if (!has_password()) {
Expand Down Expand Up @@ -634,7 +634,7 @@ inline void url_aggregator::clear_hash() {
ADA_ASSERT_TRUE(validate());
}

inline void url_aggregator::clear_pathname() {
constexpr void url_aggregator::clear_pathname() {
ada_log("url_aggregator::clear_pathname");
ADA_ASSERT_TRUE(validate());
uint32_t ending_index = uint32_t(buffer.size());
Expand Down Expand Up @@ -669,7 +669,7 @@ inline void url_aggregator::clear_pathname() {
ada_log("url_aggregator::clear_pathname completed, running checks... ok");
}

inline void url_aggregator::clear_hostname() {
constexpr void url_aggregator::clear_hostname() {
ada_log("url_aggregator::clear_hostname");
ADA_ASSERT_TRUE(validate());
if (!has_authority()) {
Expand Down Expand Up @@ -706,22 +706,22 @@ inline void url_aggregator::clear_hostname() {
ADA_ASSERT_TRUE(validate());
}

[[nodiscard]] inline bool url_aggregator::has_hash() const noexcept {
[[nodiscard]] constexpr bool url_aggregator::has_hash() const noexcept {
ada_log("url_aggregator::has_hash");
return components.hash_start != url_components::omitted;
}

[[nodiscard]] inline bool url_aggregator::has_search() const noexcept {
[[nodiscard]] constexpr bool url_aggregator::has_search() const noexcept {
ada_log("url_aggregator::has_search");
return components.search_start != url_components::omitted;
}

ada_really_inline bool url_aggregator::has_credentials() const noexcept {
constexpr bool url_aggregator::has_credentials() const noexcept {
ada_log("url_aggregator::has_credentials");
return has_non_empty_username() || has_non_empty_password();
}

inline bool url_aggregator::cannot_have_credentials_or_port() const {
constexpr bool url_aggregator::cannot_have_credentials_or_port() const {
ada_log("url_aggregator::cannot_have_credentials_or_port");
return type == ada::scheme::type::FILE ||
components.host_start == components.host_end;
Expand All @@ -732,7 +732,7 @@ url_aggregator::get_components() const noexcept {
return components;
}

[[nodiscard]] inline bool ada::url_aggregator::has_authority() const noexcept {
[[nodiscard]] constexpr bool ada::url_aggregator::has_authority() const noexcept {
ada_log("url_aggregator::has_authority");
// Performance: instead of doing this potentially expensive check, we could
// have a boolean in the struct.
Expand Down Expand Up @@ -767,28 +767,28 @@ inline void ada::url_aggregator::add_authority_slashes_if_needed() noexcept {
ADA_ASSERT_TRUE(validate());
}

inline void ada::url_aggregator::reserve(uint32_t capacity) {
constexpr void ada::url_aggregator::reserve(uint32_t capacity) {
buffer.reserve(capacity);
}

inline bool url_aggregator::has_non_empty_username() const noexcept {
constexpr bool url_aggregator::has_non_empty_username() const noexcept {
ada_log("url_aggregator::has_non_empty_username");
return components.protocol_end + 2 < components.username_end;
}

inline bool url_aggregator::has_non_empty_password() const noexcept {
constexpr bool url_aggregator::has_non_empty_password() const noexcept {
ada_log("url_aggregator::has_non_empty_password");
return components.host_start - components.username_end > 0;
}

inline bool url_aggregator::has_password() const noexcept {
constexpr bool url_aggregator::has_password() const noexcept {
ada_log("url_aggregator::has_password");
// This function does not care about the length of the password
return components.host_start > components.username_end &&
buffer[components.username_end] == ':';
}

inline bool url_aggregator::has_empty_hostname() const noexcept {
constexpr bool url_aggregator::has_empty_hostname() const noexcept {
if (!has_hostname()) {
return false;
}
Expand All @@ -801,18 +801,18 @@ inline bool url_aggregator::has_empty_hostname() const noexcept {
return components.username_end != components.host_start;
}

inline bool url_aggregator::has_hostname() const noexcept {
constexpr bool url_aggregator::has_hostname() const noexcept {
return has_authority();
}

inline bool url_aggregator::has_port() const noexcept {
constexpr bool url_aggregator::has_port() const noexcept {
ada_log("url_aggregator::has_port");
// A URL cannot have a username/password/port if its host is null or the empty
// string, or its scheme is "file".
return has_hostname() && components.pathname_start != components.host_end;
}

[[nodiscard]] inline bool url_aggregator::has_dash_dot() const noexcept {
[[nodiscard]] constexpr bool url_aggregator::has_dash_dot() const noexcept {
// If url's host is null, url does not have an opaque path, url's path's size
// is greater than 1, and url's path[0] is the empty string, then append
// U+002F (/) followed by U+002E (.) to output.
Expand Down Expand Up @@ -844,7 +844,7 @@ 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]] constexpr std::string_view url_aggregator::get_href() const noexcept
ada_lifetime_bound {
ada_log("url_aggregator::get_href");
return buffer;
Expand Down Expand Up @@ -889,7 +889,7 @@ ada_really_inline size_t url_aggregator::parse_port(
return consumed;
}

inline void url_aggregator::set_protocol_as_file() {
constexpr void url_aggregator::set_protocol_as_file() {
ada_log("url_aggregator::set_protocol_as_file ");
ADA_ASSERT_TRUE(validate());
type = ada::scheme::type::FILE;
Expand Down
40 changes: 20 additions & 20 deletions include/ada/url_aggregator.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ 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]] constexpr std::string_view get_href() const noexcept
ada_lifetime_bound;
/**
* The username getter steps are to return this's URL's username.
Expand Down Expand Up @@ -114,7 +114,7 @@ struct url_aggregator : url_base {
* @return a lightweight std::string_view.
* @see https://url.spec.whatwg.org/#dom-url-pathname
*/
[[nodiscard]] std::string_view get_pathname() const noexcept
[[nodiscard]] constexpr std::string_view get_pathname() const noexcept
ada_lifetime_bound;
/**
* Compute the pathname length in bytes without instantiating a view or a
Expand Down Expand Up @@ -144,7 +144,7 @@ struct url_aggregator : url_base {
* A URL includes credentials if its username or password is not the empty
* string.
*/
[[nodiscard]] ada_really_inline bool has_credentials() const noexcept;
[[nodiscard]] ada_really_inline constexpr bool has_credentials() const noexcept;

/**
* Useful for implementing efficient serialization for the URL.
Expand Down Expand Up @@ -183,24 +183,24 @@ struct url_aggregator : url_base {
* @return true if the URL is valid, otherwise return true of the offsets are
* possible.
*/
[[nodiscard]] bool validate() const noexcept;
[[nodiscard]] constexpr bool validate() const noexcept;

/** @return true if it has an host but it is the empty string */
[[nodiscard]] inline bool has_empty_hostname() const noexcept;
[[nodiscard]] constexpr bool has_empty_hostname() const noexcept;
/** @return true if it has a host (included an empty host) */
[[nodiscard]] inline bool has_hostname() const noexcept;
[[nodiscard]] constexpr bool has_hostname() const noexcept;
/** @return true if the URL has a non-empty username */
[[nodiscard]] inline bool has_non_empty_username() const noexcept;
[[nodiscard]] constexpr bool has_non_empty_username() const noexcept;
/** @return true if the URL has a non-empty password */
[[nodiscard]] inline bool has_non_empty_password() const noexcept;
[[nodiscard]] constexpr bool has_non_empty_password() const noexcept;
/** @return true if the URL has a (non default) port */
[[nodiscard]] inline bool has_port() const noexcept;
[[nodiscard]] constexpr bool has_port() const noexcept;
/** @return true if the URL has a password */
[[nodiscard]] inline bool has_password() const noexcept;
[[nodiscard]] constexpr bool has_password() const noexcept;
/** @return true if the URL has a hash component */
[[nodiscard]] inline bool has_hash() const noexcept override;
[[nodiscard]] constexpr bool has_hash() const noexcept override;
/** @return true if the URL has a search component */
[[nodiscard]] inline bool has_search() const noexcept override;
[[nodiscard]] constexpr bool has_search() const noexcept override;

inline void clear_port();
inline void clear_hash();
Expand Down Expand Up @@ -233,7 +233,7 @@ struct url_aggregator : url_base {
* To optimize performance, you may indicate how much memory to allocate
* within this instance.
*/
inline void reserve(uint32_t capacity);
constexpr void reserve(uint32_t capacity);

ada_really_inline size_t parse_port(
std::string_view view, bool check_trailing_content) noexcept override;
Expand Down Expand Up @@ -268,7 +268,7 @@ struct url_aggregator : url_base {
* A URL cannot have a username/password/port if its host is null or the empty
* string, or its scheme is "file".
*/
[[nodiscard]] inline bool cannot_have_credentials_or_port() const;
[[nodiscard]] constexpr bool cannot_have_credentials_or_port() const;

template <bool override_hostname = false>
bool set_host_or_hostname(std::string_view input);
Expand All @@ -290,19 +290,19 @@ struct url_aggregator : url_base {
inline void update_base_port(uint32_t input);
inline void append_base_pathname(std::string_view input);
[[nodiscard]] inline uint32_t retrieve_base_port() const;
inline void clear_hostname();
inline void clear_password();
inline void clear_pathname() override;
[[nodiscard]] inline bool has_dash_dot() const noexcept;
constexpr void clear_hostname();
constexpr void clear_password();
constexpr void clear_pathname() override;
[[nodiscard]] constexpr bool has_dash_dot() const noexcept;
void delete_dash_dot();
inline void consume_prepared_path(std::string_view input);
template <bool has_state_override = false>
[[nodiscard]] ada_really_inline bool parse_scheme_with_colon(
std::string_view input);
ada_really_inline uint32_t replace_and_resize(uint32_t start, uint32_t end,
std::string_view input);
[[nodiscard]] inline bool has_authority() const noexcept;
inline void set_protocol_as_file();
[[nodiscard]] constexpr bool has_authority() const noexcept;
constexpr void set_protocol_as_file();
inline void set_scheme(std::string_view new_scheme) noexcept;
/**
* Fast function to set the scheme from a view with a colon in the
Expand Down
2 changes: 1 addition & 1 deletion include/ada/url_base-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace ada {

[[nodiscard]] ada_really_inline bool url_base::is_special() const noexcept {
[[nodiscard]] ada_really_inline constexpr bool url_base::is_special() const noexcept {
return type != ada::scheme::NOT_SPECIAL;
}

Expand Down
2 changes: 1 addition & 1 deletion include/ada/url_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct url_base {
* A URL is special if its scheme is a special scheme. A URL is not special if
* its scheme is not a special scheme.
*/
[[nodiscard]] ada_really_inline bool is_special() const noexcept;
[[nodiscard]] ada_really_inline constexpr bool is_special() const noexcept;

/**
* The origin getter steps are to return the serialization of this's URL's
Expand Down
2 changes: 1 addition & 1 deletion include/ada/url_components.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct url_components {
* @return true if the offset values are
* consistent with a possible URL string
*/
[[nodiscard]] bool check_offset_consistency() const noexcept;
[[nodiscard]] constexpr bool check_offset_consistency() const noexcept;

/**
* Converts a url_components to JSON stringified version.
Expand Down
2 changes: 1 addition & 1 deletion src/url-getters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace ada {
return host.value_or("");
}

[[nodiscard]] std::string_view url::get_pathname() const noexcept {
[[nodiscard]] constexpr std::string_view url::get_pathname() const noexcept {
return path;
}

Expand Down
4 changes: 2 additions & 2 deletions src/url_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ 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]] constexpr 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(),
Expand Down Expand Up @@ -1377,7 +1377,7 @@ bool url_aggregator::parse_opaque_host(std::string_view input) {
return answer;
}

[[nodiscard]] bool url_aggregator::validate() const noexcept {
[[nodiscard]] constexpr bool url_aggregator::validate() const noexcept {
if (!is_valid) {
return true;
}
Expand Down
Loading
Loading