Skip to content

Commit

Permalink
more constexpr (#738)
Browse files Browse the repository at this point in the history
* convert more functions to constexpr

* moving functions around

* tweak

* lint

* simplify

---------

Co-authored-by: Yagiz Nizipli <yagiz@cloudflare.com>
  • Loading branch information
lemire and anonrig committed Sep 19, 2024
1 parent e730702 commit 9bb54ec
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 336 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/aarch64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,11 @@ concurrency:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
shared: [OFF]
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: uraimo/run-on-arch-action@b0ffb25eb00af00468375982384441f063da1741 # v2.7.2
name: Build and Test
id: runcmd
env:
CXX: g++-12
with:
arch: aarch64
githubToken: ${{ github.token }}
Expand All @@ -41,6 +36,6 @@ jobs:
apt-get update -q -y
apt-get install -y cmake make g++ ninja-build git
run: |
cmake -DADA_SANITIZE_BOUNDS_STRICT=ON -DBUILD_SHARED_LIBS=${{matrix.shared}} -B build
cmake -B build
cmake --build build
ctest --test-dir build
1 change: 1 addition & 0 deletions include/ada.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "ada/url_base-inl.h"
#include "ada/url-inl.h"
#include "ada/url_components.h"
#include "ada/url_components-inl.h"
#include "ada/url_aggregator.h"
#include "ada/url_aggregator-inl.h"
#include "ada/url_search_params.h"
Expand Down
6 changes: 3 additions & 3 deletions include/ada/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ bool overlaps(std::string_view input1, const std::string& input2) noexcept;
* Return the substring from input going from index pos1 to the pos2 (non
* included). The length of the substring is pos2 - pos1.
*/
ada_really_inline std::string_view substring(const std::string& input,
size_t pos1,
size_t pos2) noexcept {
ada_really_inline constexpr std::string_view substring(const std::string& input,
size_t pos1,
size_t pos2) noexcept {
#if ADA_DEVELOPMENT_CHECKS
if (pos2 < pos1) {
std::cerr << "Negative-length substring: [" << pos1 << " to " << pos2 << ")"
Expand Down
18 changes: 11 additions & 7 deletions include/ada/url-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ inline std::ostream &operator<<(std::ostream &out, const ada::url &u) {
return path.size();
}

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

[[nodiscard]] ada_really_inline ada::url_components url::get_components()
const noexcept {
url_components out{};
Expand Down Expand Up @@ -148,19 +152,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 +174,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
Loading

0 comments on commit 9bb54ec

Please sign in to comment.