Skip to content

Commit 281501b

Browse files
authored
Added more constexpr where they made sense (#157)
* Added constexpr everywhere that there wasn't a compiler error
1 parent 40f7a25 commit 281501b

File tree

8 files changed

+27
-21
lines changed

8 files changed

+27
-21
lines changed

.github/workflows/skyr-url-ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,6 @@ jobs:
476476
set(BUILD_V2 OFF)
477477
if ("${{ matrix.config.name }}" STREQUAL "Windows MSVC 2019 Debug (C++20)" OR
478478
"${{ matrix.config.name }}" STREQUAL "Windows MSVC 2019 Release (C++20)" OR
479-
"${{ matrix.config.name }}" STREQUAL "Linux GCC 10 Debug (C++20)" OR
480-
"${{ matrix.config.name }}" STREQUAL "Linux GCC 10 Release (C++20)" OR
481479
"${{ matrix.config.name }}" STREQUAL "Linux GCC 11 Debug (C++20)" OR
482480
"${{ matrix.config.name }}" STREQUAL "Linux GCC 11 Release (C++20)" OR
483481
"${{ matrix.config.name }}" STREQUAL "Linux Clang 11 Debug (C++20)" OR

include/skyr/v2/containers/static_vector.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <array>
1111
#include <type_traits>
1212
#include <optional>
13+
#include <cassert>
1314

1415
namespace skyr::inline v2 {
1516
///
@@ -51,35 +52,39 @@ class static_vector {
5152
constexpr auto operator=(const static_vector &) -> static_vector & = default;
5253
constexpr auto operator=(static_vector &&) noexcept -> static_vector & = default;
5354

54-
~static_vector() {
55+
constexpr ~static_vector() {
5556
clear();
5657
}
5758

5859
/// Gets the first const element in the vector
5960
/// \return a const T &
60-
/// \pre `size() > 0`
61+
/// \pre `size_ > 0`
6162
constexpr auto front() const noexcept -> const_reference {
63+
assert(size() > 0);
6264
return impl_[0];
6365
}
6466

6567
/// Gets the first element in the vector
6668
/// \return a T &
6769
/// \pre `size() > 0`
6870
constexpr auto front() noexcept -> reference {
71+
assert(size_ > 0);
6972
return impl_[0];
7073
}
7174

7275
///
7376
/// \return
7477
/// \pre `size() > 0`
7578
constexpr auto back() const noexcept -> const_reference {
79+
assert(size_ > 0);
7680
return impl_[size_ - 1];
7781
}
7882

7983
///
8084
/// \return
8185
/// \pre `size() > 0`
8286
constexpr auto back() noexcept -> reference {
87+
assert(size_ > 0);
8388
return impl_[size_ - 1];
8489
}
8590

@@ -89,6 +94,7 @@ class static_vector {
8994
/// \pre `size() < capacity()`
9095
/// \post `size() > 0 && size() <= capacity()`
9196
constexpr auto push_back(const_reference value) noexcept -> reference {
97+
assert(size_ < Capacity);
9298
impl_[size_++] = value;
9399
return impl_[size_ - 1];
94100
}
@@ -101,13 +107,15 @@ class static_vector {
101107
/// \post `size() > 0 && size() <= capacity()`
102108
template <class... Args>
103109
constexpr auto emplace_back(Args &&... args) noexcept(std::is_trivially_move_assignable_v<T>) -> reference {
110+
assert(size_ < Capacity);
104111
impl_[size_++] = value_type{std::forward<Args>(args)...};
105112
return back();
106113
}
107114

108115
///
109116
/// \pre `size() > 0`
110117
constexpr void pop_back() noexcept {
118+
assert(size_ > 0);
111119
back().~value_type();
112120
--size_;
113121
}

include/skyr/v2/core/host.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class host {
8686

8787
///
8888
/// \return \c true if the host is a domain, \c false otherwise
89-
[[nodiscard]] auto is_domain_name() const noexcept {
89+
[[nodiscard]] constexpr auto is_domain_name() const noexcept {
9090
return std::holds_alternative<skyr::v2::domain_name>(host_);
9191
}
9292

@@ -98,31 +98,31 @@ class host {
9898

9999
///
100100
/// \return \c true if the host is an IPv4 address, \c false otherwise
101-
[[nodiscard]] auto is_ipv4_address() const noexcept {
101+
[[nodiscard]] constexpr auto is_ipv4_address() const noexcept {
102102
return std::holds_alternative<skyr::v2::ipv4_address>(host_);
103103
}
104104

105105
///
106106
/// \return
107-
[[nodiscard]] auto to_ipv4_address() const noexcept {
107+
[[nodiscard]] constexpr auto to_ipv4_address() const noexcept {
108108
return is_ipv4_address() ? std::make_optional(std::get<skyr::v2::ipv4_address>(host_)) : std::nullopt;
109109
}
110110

111111
///
112112
/// \return \c true if the host is an IPv6 address, \c false otherwise
113-
[[nodiscard]] auto is_ipv6_address() const noexcept {
113+
[[nodiscard]] constexpr auto is_ipv6_address() const noexcept {
114114
return std::holds_alternative<skyr::v2::ipv6_address>(host_);
115115
}
116116

117117
///
118118
/// \return
119-
[[nodiscard]] auto to_ipv6_address() const noexcept {
119+
[[nodiscard]] constexpr auto to_ipv6_address() const noexcept {
120120
return is_ipv6_address() ? std::make_optional(std::get<skyr::v2::ipv6_address>(host_)) : std::nullopt;
121121
}
122122

123123
///
124124
/// \return \c true if the host is an opaque host, \c false otherwise
125-
[[nodiscard]] auto is_opaque_host() const noexcept {
125+
[[nodiscard]] constexpr auto is_opaque_host() const noexcept {
126126
return std::holds_alternative<skyr::v2::opaque_host>(host_);
127127
}
128128

@@ -134,7 +134,7 @@ class host {
134134

135135
///
136136
/// \return
137-
[[nodiscard]] auto is_empty() const noexcept {
137+
[[nodiscard]] constexpr auto is_empty() const noexcept {
138138
return std::holds_alternative<empty_host>(host_);
139139
}
140140

include/skyr/v2/core/url_parser_context.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ using namespace std::string_literals;
2727
using namespace std::string_view_literals;
2828

2929
namespace details {
30-
inline auto contains(std::string_view view, char element) noexcept {
30+
constexpr inline auto contains(std::string_view view, char element) noexcept {
3131
auto first = std::cbegin(view), last = std::cend(view);
3232
return last != std::find(first, last, element);
3333
}
@@ -38,7 +38,7 @@ inline auto port_number(std::string_view port) noexcept -> tl::expected<std::uin
3838
}
3939

4040
const char *port_first = port.data();
41-
char *port_last = nullptr;
41+
char *port_last = nullptr; // NOLINT
4242
auto port_value = std::strtoul(port_first, &port_last, 10);
4343

4444
if (port_first == port_last) {
@@ -55,7 +55,7 @@ inline auto is_url_code_point(char byte) noexcept {
5555
return std::isalnum(byte, std::locale::classic()) || contains("!$&'()*+,-./:;=?@_~"sv, byte);
5656
}
5757

58-
inline auto is_windows_drive_letter(std::string_view segment) noexcept {
58+
constexpr inline auto is_windows_drive_letter(std::string_view segment) noexcept {
5959
if (segment.size() < 2) {
6060
return false;
6161
}
@@ -73,11 +73,11 @@ inline auto is_windows_drive_letter(std::string_view segment) noexcept {
7373
return result;
7474
}
7575

76-
inline auto is_single_dot_path_segment(std::string_view segment) noexcept {
76+
constexpr inline auto is_single_dot_path_segment(std::string_view segment) noexcept {
7777
return (segment == ".") || (segment == "%2e") || (segment == "%2E");
7878
}
7979

80-
inline auto is_double_dot_path_segment(std::string_view segment) noexcept {
80+
constexpr inline auto is_double_dot_path_segment(std::string_view segment) noexcept {
8181
return (segment == "..") || (segment == "%2e.") || (segment == ".%2e") || (segment == "%2e%2e") ||
8282
(segment == "%2E.") || (segment == ".%2E") || (segment == "%2E%2E") || (segment == "%2E%2e") ||
8383
(segment == "%2e%2E");

include/skyr/v2/domain/punycode.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ inline auto punycode_encode(std::u32string_view input, std::string *output) -> t
138138
/// \param input An ASCII encoded domain to be decoded
139139
/// \returns The decoded UTF-8 domain, or an error
140140
template <class StringView>
141-
inline auto punycode_decode(StringView input, std::u32string *output) -> tl::expected<void, domain_errc> {
141+
constexpr inline auto punycode_decode(StringView input, std::u32string *output) -> tl::expected<void, domain_errc> {
142142
using namespace punycode::constants;
143143

144144
// decode_digit(cp) returns the numeric value of a basic code

include/skyr/v2/network/ipv4_address.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ constexpr inline auto parse_ipv4_number(std::string_view input, bool *validation
108108
/// Parses an IPv4 address
109109
/// \param input An input string
110110
/// \returns An `ipv4_address` object or an error
111-
inline auto parse_ipv4_address(std::string_view input, bool *validation_error)
111+
constexpr inline auto parse_ipv4_address(std::string_view input, bool *validation_error)
112112
-> tl::expected<ipv4_address, ipv4_address_errc> {
113113
using namespace std::string_view_literals;
114114

include/skyr/v2/percent_encoding/percent_encoded_char.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ struct percent_encoded_char {
208208

209209
///
210210
/// \return
211-
[[nodiscard]] auto to_string() const & -> std::string {
211+
[[nodiscard]] constexpr auto to_string() const & -> const std::string & {
212212
return impl_;
213213
}
214214

215215
///
216216
/// \return
217-
[[nodiscard]] auto to_string() && noexcept -> std::string && {
217+
[[nodiscard]] constexpr auto to_string() && noexcept -> std::string && {
218218
return std::move(impl_);
219219
}
220220

src/v1/url/url.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void url::initialize(string_view input, const url_record *base) {
1717

1818
bool validation_error = false;
1919
details::parse(input, &validation_error, base)
20-
.and_then([=](auto &&url) -> result_type {
20+
.and_then([this](auto &&url) -> result_type {
2121
update_record(std::forward<url_record>(url));
2222
return {};
2323
})

0 commit comments

Comments
 (0)