Skip to content

Commit eb1697d

Browse files
committed
more c++20 changes
1 parent c0ed932 commit eb1697d

File tree

15 files changed

+54
-80
lines changed

15 files changed

+54
-80
lines changed

include/ada/helpers.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include <string_view>
1313
#include <optional>
1414

15+
#if ADA_DEVELOPMENT_CHECKS
16+
#include <iostream>
17+
#endif // ADA_DEVELOPMENT_CHECKS
18+
1519
/**
1620
* These functions are not part of our public API and may
1721
* change at any time.
@@ -128,7 +132,7 @@ ada_really_inline void resize(std::string_view& input, size_t pos) noexcept;
128132
* and whether a colon was found outside brackets. Used by the host parser.
129133
*/
130134
ada_really_inline std::pair<size_t, bool> get_host_delimiter_location(
131-
const bool is_special, std::string_view& view) noexcept;
135+
bool is_special, std::string_view& view) noexcept;
132136

133137
/**
134138
* @private

include/ada/log.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
#define ADA_LOG_H
88
#include "ada/common_defs.h"
99

10-
#include <iostream>
1110
// To enable logging, set ADA_LOGGING to 1:
1211
#ifndef ADA_LOGGING
1312
#define ADA_LOGGING 0
1413
#endif
1514

15+
#if ADA_LOGGING
16+
#include <iostream>
17+
#endif // ADA_LOGGING
18+
1619
namespace ada {
1720

1821
/**

include/ada/scheme.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "ada/common_defs.h"
99

1010
#include <array>
11-
#include <optional>
1211
#include <string>
1312

1413
/**

include/ada/serializers.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "ada/common_defs.h"
99

1010
#include <array>
11-
#include <optional>
1211
#include <string>
1312

1413
/**

include/ada/unicode-inl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ namespace ada::unicode {
1919
ada_really_inline size_t percent_encode_index(const std::string_view input,
2020
const uint8_t character_set[]) {
2121
return std::distance(
22-
input.begin(),
23-
std::find_if(input.begin(), input.end(), [character_set](const char c) {
22+
input.begin(), std::ranges::find_if(input, [character_set](const char c) {
2423
return character_sets::bit_at(character_set, c);
2524
}));
2625
}

include/ada/url-inl.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ inline void url::update_base_search(std::string_view input,
133133
}
134134

135135
inline void url::update_base_search(std::optional<std::string> input) {
136-
query = input;
136+
query = std::move(input);
137137
}
138138

139139
inline void url::update_base_pathname(const std::string_view input) {
@@ -232,7 +232,7 @@ ada_really_inline size_t url::parse_port(std::string_view view,
232232
return 0;
233233
}
234234
ada_log("parse_port: ", parsed_port);
235-
const size_t consumed = size_t(r.ptr - view.data());
235+
const auto consumed = size_t(r.ptr - view.data());
236236
ada_log("parse_port: consumed ", consumed);
237237
if (check_trailing_content) {
238238
is_valid &=
@@ -245,9 +245,8 @@ ada_really_inline size_t url::parse_port(std::string_view view,
245245
auto default_port = scheme_default_port();
246246
bool is_port_valid = (default_port == 0 && parsed_port == 0) ||
247247
(default_port != parsed_port);
248-
port = (r.ec == std::errc() && is_port_valid)
249-
? std::optional<uint16_t>(parsed_port)
250-
: std::nullopt;
248+
port = (r.ec == std::errc() && is_port_valid) ? std::optional(parsed_port)
249+
: std::nullopt;
251250
}
252251
return consumed;
253252
}

include/ada/url_search_params-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ inline url_search_params_entries_iter url_search_params::get_entries() {
193193
}
194194

195195
template <typename T, url_search_params_iter_type Type>
196-
inline bool url_search_params_iter<T, Type>::has_next() {
196+
inline bool url_search_params_iter<T, Type>::has_next() const {
197197
return pos < params.params.size();
198198
}
199199

include/ada/url_search_params.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ struct url_search_params {
4242
* @see
4343
* https://github.com/web-platform-tests/wpt/blob/master/url/urlsearchparams-constructor.any.js
4444
*/
45-
url_search_params(const std::string_view input) { initialize(input); }
45+
explicit url_search_params(const std::string_view input) {
46+
initialize(input);
47+
}
4648

4749
url_search_params(const url_search_params &u) = default;
4850
url_search_params(url_search_params &&u) noexcept = default;
@@ -172,7 +174,7 @@ struct url_search_params_iter {
172174
*/
173175
inline std::optional<T> next();
174176

175-
inline bool has_next();
177+
inline bool has_next() const;
176178

177179
private:
178180
static url_search_params EMPTY;

src/checkers.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ ada_really_inline ada_constexpr bool is_ipv4(std::string_view view) noexcept {
1111
// with 'x' or a lowercase hex character.
1212
// Most of the time, this will be false so this simple check will save a lot
1313
// of effort.
14-
char last_char = view.back();
1514
// If the address ends with a dot, we need to prune it (special case).
16-
if (last_char == '.') {
15+
if (view.ends_with('.')) {
1716
view.remove_suffix(1);
1817
if (view.empty()) {
1918
return false;
2019
}
21-
last_char = view.back();
2220
}
21+
char last_char = view.back();
2322
bool possible_ipv4 = (last_char >= '0' && last_char <= '9') ||
2423
(last_char >= 'a' && last_char <= 'f') ||
2524
last_char == 'x';
@@ -35,7 +34,7 @@ ada_really_inline ada_constexpr bool is_ipv4(std::string_view view) noexcept {
3534
/** Optimization opportunity: we have basically identified the last number of
3635
the ipv4 if we return true here. We might as well parse it and have at
3736
least one number parsed when we get to parse_ipv4. */
38-
if (std::all_of(view.begin(), view.end(), ada::checkers::is_digit)) {
37+
if (std::ranges::all_of(view, ada::checkers::is_digit)) {
3938
return true;
4039
}
4140
// It could be hex (0x), but not if there is a single character.

src/helpers.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,11 @@ ada_really_inline std::optional<std::string_view> prune_hash(
9696

9797
ada_really_inline bool shorten_path(std::string& path,
9898
ada::scheme::type type) noexcept {
99-
size_t first_delimiter = path.find_first_of('/', 1);
100-
10199
// Let path be url's path.
102100
// If url's scheme is "file", path's size is 1, and path[0] is a normalized
103101
// Windows drive letter, then return.
104102
if (type == ada::scheme::type::FILE &&
105-
first_delimiter == std::string_view::npos && !path.empty()) {
103+
path.find('/', 1) == std::string_view::npos && !path.empty()) {
106104
if (checkers::is_normalized_windows_drive_letter(
107105
helpers::substring(path, 1))) {
108106
return false;
@@ -121,13 +119,11 @@ ada_really_inline bool shorten_path(std::string& path,
121119

122120
ada_really_inline bool shorten_path(std::string_view& path,
123121
ada::scheme::type type) noexcept {
124-
size_t first_delimiter = path.find_first_of('/', 1);
125-
126122
// Let path be url's path.
127123
// If url's scheme is "file", path's size is 1, and path[0] is a normalized
128124
// Windows drive letter, then return.
129125
if (type == ada::scheme::type::FILE &&
130-
first_delimiter == std::string_view::npos && !path.empty()) {
126+
path.find('/', 1) == std::string_view::npos && !path.empty()) {
131127
if (checkers::is_normalized_windows_drive_letter(
132128
helpers::substring(path, 1))) {
133129
return false;
@@ -150,11 +146,7 @@ ada_really_inline void remove_ascii_tab_or_newline(
150146
std::string& input) noexcept {
151147
// if this ever becomes a performance issue, we could use an approach similar
152148
// to has_tabs_or_newline
153-
input.erase(std::remove_if(input.begin(), input.end(),
154-
[](char c) {
155-
return ada::unicode::is_ascii_tab_or_newline(c);
156-
}),
157-
input.end());
149+
std::erase_if(input, ada::unicode::is_ascii_tab_or_newline);
158150
}
159151

160152
ada_really_inline constexpr std::string_view substring(std::string_view input,

0 commit comments

Comments
 (0)