Skip to content

Commit

Permalink
more c++20 changes (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig authored Oct 10, 2024
1 parent 14703dd commit 37c4997
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 83 deletions.
6 changes: 5 additions & 1 deletion include/ada/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include <string_view>
#include <optional>

#if ADA_DEVELOPMENT_CHECKS
#include <iostream>
#endif // ADA_DEVELOPMENT_CHECKS

/**
* These functions are not part of our public API and may
* change at any time.
Expand Down Expand Up @@ -128,7 +132,7 @@ ada_really_inline void resize(std::string_view& input, size_t pos) noexcept;
* and whether a colon was found outside brackets. Used by the host parser.
*/
ada_really_inline std::pair<size_t, bool> get_host_delimiter_location(
const bool is_special, std::string_view& view) noexcept;
bool is_special, std::string_view& view) noexcept;

/**
* @private
Expand Down
5 changes: 4 additions & 1 deletion include/ada/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#define ADA_LOG_H
#include "ada/common_defs.h"

#include <iostream>
// To enable logging, set ADA_LOGGING to 1:
#ifndef ADA_LOGGING
#define ADA_LOGGING 0
#endif

#if ADA_LOGGING
#include <iostream>
#endif // ADA_LOGGING

namespace ada {

/**
Expand Down
1 change: 0 additions & 1 deletion include/ada/scheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "ada/common_defs.h"

#include <array>
#include <optional>
#include <string>

/**
Expand Down
1 change: 0 additions & 1 deletion include/ada/serializers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "ada/common_defs.h"

#include <array>
#include <optional>
#include <string>

/**
Expand Down
3 changes: 1 addition & 2 deletions include/ada/unicode-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ namespace ada::unicode {
ada_really_inline size_t percent_encode_index(const std::string_view input,
const uint8_t character_set[]) {
return std::distance(
input.begin(),
std::find_if(input.begin(), input.end(), [character_set](const char c) {
input.begin(), std::ranges::find_if(input, [character_set](const char c) {
return character_sets::bit_at(character_set, c);
}));
}
Expand Down
11 changes: 5 additions & 6 deletions include/ada/url-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ inline void url::update_base_search(std::string_view input,
query = ada::unicode::percent_encode(input, query_percent_encode_set);
}

inline void url::update_base_search(std::optional<std::string> input) {
query = input;
inline void url::update_base_search(std::optional<std::string> &&input) {
query = std::move(input);
}

inline void url::update_base_pathname(const std::string_view input) {
Expand Down Expand Up @@ -232,7 +232,7 @@ ada_really_inline size_t url::parse_port(std::string_view view,
return 0;
}
ada_log("parse_port: ", parsed_port);
const size_t consumed = size_t(r.ptr - view.data());
const auto consumed = size_t(r.ptr - view.data());
ada_log("parse_port: consumed ", consumed);
if (check_trailing_content) {
is_valid &=
Expand All @@ -245,9 +245,8 @@ ada_really_inline size_t url::parse_port(std::string_view view,
auto default_port = scheme_default_port();
bool is_port_valid = (default_port == 0 && parsed_port == 0) ||
(default_port != parsed_port);
port = (r.ec == std::errc() && is_port_valid)
? std::optional<uint16_t>(parsed_port)
: std::nullopt;
port = (r.ec == std::errc() && is_port_valid) ? std::optional(parsed_port)
: std::nullopt;
}
return consumed;
}
Expand Down
3 changes: 1 addition & 2 deletions include/ada/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,9 @@ struct url : url_base {

inline void update_unencoded_base_hash(std::string_view input);
inline void update_base_hostname(std::string_view input);
inline void update_base_search(std::string_view input);
inline void update_base_search(std::string_view input,
const uint8_t query_percent_encode_set[]);
inline void update_base_search(std::optional<std::string> input);
inline void update_base_search(std::optional<std::string> &&input);
inline void update_base_pathname(std::string_view input);
inline void update_base_username(std::string_view input);
inline void update_base_password(std::string_view input);
Expand Down
2 changes: 1 addition & 1 deletion include/ada/url_search_params-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ inline url_search_params_entries_iter url_search_params::get_entries() {
}

template <typename T, url_search_params_iter_type Type>
inline bool url_search_params_iter<T, Type>::has_next() {
inline bool url_search_params_iter<T, Type>::has_next() const {
return pos < params.params.size();
}

Expand Down
6 changes: 4 additions & 2 deletions include/ada/url_search_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ struct url_search_params {
* @see
* https://github.com/web-platform-tests/wpt/blob/master/url/urlsearchparams-constructor.any.js
*/
url_search_params(const std::string_view input) { initialize(input); }
explicit url_search_params(const std::string_view input) {
initialize(input);
}

url_search_params(const url_search_params &u) = default;
url_search_params(url_search_params &&u) noexcept = default;
Expand Down Expand Up @@ -172,7 +174,7 @@ struct url_search_params_iter {
*/
inline std::optional<T> next();

inline bool has_next();
inline bool has_next() const;

private:
static url_search_params EMPTY;
Expand Down
7 changes: 3 additions & 4 deletions src/checkers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ ada_really_inline ada_constexpr bool is_ipv4(std::string_view view) noexcept {
// with 'x' or a lowercase hex character.
// Most of the time, this will be false so this simple check will save a lot
// of effort.
char last_char = view.back();
// If the address ends with a dot, we need to prune it (special case).
if (last_char == '.') {
if (view.ends_with('.')) {
view.remove_suffix(1);
if (view.empty()) {
return false;
}
last_char = view.back();
}
char last_char = view.back();
bool possible_ipv4 = (last_char >= '0' && last_char <= '9') ||
(last_char >= 'a' && last_char <= 'f') ||
last_char == 'x';
Expand All @@ -35,7 +34,7 @@ ada_really_inline ada_constexpr bool is_ipv4(std::string_view view) noexcept {
/** Optimization opportunity: we have basically identified the last number of
the ipv4 if we return true here. We might as well parse it and have at
least one number parsed when we get to parse_ipv4. */
if (std::all_of(view.begin(), view.end(), ada::checkers::is_digit)) {
if (std::ranges::all_of(view, ada::checkers::is_digit)) {
return true;
}
// It could be hex (0x), but not if there is a single character.
Expand Down
14 changes: 3 additions & 11 deletions src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,11 @@ ada_really_inline std::optional<std::string_view> prune_hash(

ada_really_inline bool shorten_path(std::string& path,
ada::scheme::type type) noexcept {
size_t first_delimiter = path.find_first_of('/', 1);

// Let path be url's path.
// If url's scheme is "file", path's size is 1, and path[0] is a normalized
// Windows drive letter, then return.
if (type == ada::scheme::type::FILE &&
first_delimiter == std::string_view::npos && !path.empty()) {
path.find('/', 1) == std::string_view::npos && !path.empty()) {
if (checkers::is_normalized_windows_drive_letter(
helpers::substring(path, 1))) {
return false;
Expand All @@ -121,13 +119,11 @@ ada_really_inline bool shorten_path(std::string& path,

ada_really_inline bool shorten_path(std::string_view& path,
ada::scheme::type type) noexcept {
size_t first_delimiter = path.find_first_of('/', 1);

// Let path be url's path.
// If url's scheme is "file", path's size is 1, and path[0] is a normalized
// Windows drive letter, then return.
if (type == ada::scheme::type::FILE &&
first_delimiter == std::string_view::npos && !path.empty()) {
path.find('/', 1) == std::string_view::npos && !path.empty()) {
if (checkers::is_normalized_windows_drive_letter(
helpers::substring(path, 1))) {
return false;
Expand All @@ -150,11 +146,7 @@ ada_really_inline void remove_ascii_tab_or_newline(
std::string& input) noexcept {
// if this ever becomes a performance issue, we could use an approach similar
// to has_tabs_or_newline
input.erase(std::remove_if(input.begin(), input.end(),
[](char c) {
return ada::unicode::is_ascii_tab_or_newline(c);
}),
input.end());
std::erase_if(input, ada::unicode::is_ascii_tab_or_newline);
}

ada_really_inline constexpr std::string_view substring(std::string_view input,
Expand Down
43 changes: 18 additions & 25 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,9 @@ result_type parse_url_impl(std::string_view user_input,
// TODO: We could do various processing early on, using a single pass
// over the string to collect information about it, e.g., telling us
// whether there is a @ and if so, where (or how many).
const bool contains_ampersand =
(url_data.find('@', input_position) != std::string_view::npos);

if (!contains_ampersand) {
// Check if url data contains an @.
if (url_data.find('@', input_position) == std::string_view::npos) {
state = ada::state::HOST;
break;
}
Expand All @@ -252,12 +251,12 @@ result_type parse_url_impl(std::string_view user_input,
* --------^
*/
do {
std::string_view view = helpers::substring(url_data, input_position);
std::string_view view = url_data.substr(input_position);
// The delimiters are @, /, ? \\.
size_t location =
url.is_special() ? helpers::find_authority_delimiter_special(view)
: helpers::find_authority_delimiter(view);
std::string_view authority_view(view.data(), location);
std::string_view authority_view = view.substr(0, location);
size_t end_of_authority = input_position + authority_view.size();
// If c is U+0040 (@), then:
if ((end_of_authority != input_size) &&
Expand Down Expand Up @@ -361,8 +360,7 @@ result_type parse_url_impl(std::string_view user_input,
// If c is U+002F (/) and remaining starts with U+002F (/),
// then set state to special authority ignore slashes state and increase
// pointer by 1.
std::string_view view = helpers::substring(url_data, input_position);
if (view.starts_with("//")) {
if (url_data.substr(input_position, 2) == "//") {
state = ada::state::SPECIAL_AUTHORITY_IGNORE_SLASHES;
input_position += 2;
} else {
Expand Down Expand Up @@ -457,7 +455,7 @@ result_type parse_url_impl(std::string_view user_input,
} else {
std::string_view path = url.get_pathname();
if (helpers::shorten_path(path, url.type)) {
url.update_base_pathname(std::string(path));
url.update_base_pathname(std::move(std::string(path)));
}
}
// Set state to path state and decrease pointer by 1.
Expand Down Expand Up @@ -518,8 +516,7 @@ result_type parse_url_impl(std::string_view user_input,
// If c is U+002F (/) and remaining starts with U+002F (/),
// then set state to special authority ignore slashes state and increase
// pointer by 1.
std::string_view view = helpers::substring(url_data, input_position);
if (view.starts_with("//")) {
if (url_data.substr(input_position, 2) == "//") {
input_position += 2;
}

Expand Down Expand Up @@ -552,7 +549,7 @@ result_type parse_url_impl(std::string_view user_input,

// Percent-encode after encoding, with encoding, buffer, and
// queryPercentEncodeSet, and append the result to url's query.
url.update_base_search(helpers::substring(url_data, input_position),
url.update_base_search(url_data.substr(input_position),
query_percent_encode_set);
ada_log("QUERY update_base_search completed ");
if (fragment.has_value()) {
Expand All @@ -564,8 +561,7 @@ result_type parse_url_impl(std::string_view user_input,
case ada::state::HOST: {
ada_log("HOST ", helpers::substring(url_data, input_position));

std::string_view host_view =
helpers::substring(url_data, input_position);
std::string_view host_view = url_data.substr(input_position);
auto [location, found_colon] =
helpers::get_host_delimiter_location(url.is_special(), host_view);
input_position = (location != std::string_view::npos)
Expand Down Expand Up @@ -596,7 +592,7 @@ result_type parse_url_impl(std::string_view user_input,
else {
// If url is special and host_view is the empty string, validation
// error, return failure.
if (url.is_special() && host_view.empty()) {
if (host_view.empty() && url.is_special()) {
url.is_valid = false;
return url;
}
Expand All @@ -619,7 +615,7 @@ result_type parse_url_impl(std::string_view user_input,
}
case ada::state::OPAQUE_PATH: {
ada_log("OPAQUE_PATH ", helpers::substring(url_data, input_position));
std::string_view view = helpers::substring(url_data, input_position);
std::string_view view = url_data.substr(input_position);
// If c is U+003F (?), then set url's query to the empty string and
// state to query state.
size_t location = view.find('?');
Expand All @@ -639,10 +635,8 @@ result_type parse_url_impl(std::string_view user_input,
}
case ada::state::PORT: {
ada_log("PORT ", helpers::substring(url_data, input_position));
std::string_view port_view =
helpers::substring(url_data, input_position);
size_t consumed_bytes = url.parse_port(port_view, true);
input_position += consumed_bytes;
std::string_view port_view = url_data.substr(input_position);
input_position += url.parse_port(port_view, true);
if (!url.is_valid) {
return url;
}
Expand Down Expand Up @@ -697,8 +691,8 @@ result_type parse_url_impl(std::string_view user_input,
break;
}
case ada::state::PATH: {
std::string_view view = helpers::substring(url_data, input_position);
ada_log("PATH ", helpers::substring(url_data, input_position));
std::string_view view = url_data.substr(input_position);

// Most time, we do not need percent encoding.
// Furthermore, we can immediately locate the '?'.
Expand Down Expand Up @@ -751,7 +745,7 @@ result_type parse_url_impl(std::string_view user_input,
// url's path.
if (!base_url->get_pathname().empty()) {
if (!checkers::is_windows_drive_letter(
helpers::substring(url_data, input_position))) {
url_data.substr(input_position))) {
std::string_view first_base_url_path =
base_url->get_pathname().substr(1);
size_t loc = first_base_url_path.find('/');
Expand Down Expand Up @@ -779,8 +773,8 @@ result_type parse_url_impl(std::string_view user_input,
break;
}
case ada::state::FILE_HOST: {
std::string_view view = helpers::substring(url_data, input_position);
ada_log("FILE_HOST ", helpers::substring(url_data, input_position));
std::string_view view = url_data.substr(input_position);

size_t location = view.find_first_of("/\\?");
std::string_view file_host_buffer(
Expand Down Expand Up @@ -826,8 +820,7 @@ result_type parse_url_impl(std::string_view user_input,
}
case ada::state::FILE: {
ada_log("FILE ", helpers::substring(url_data, input_position));
std::string_view file_view =
helpers::substring(url_data, input_position);
std::string_view file_view = url_data.substr(input_position);

url.set_protocol_as_file();
if constexpr (result_type_is_ada_url) {
Expand Down Expand Up @@ -880,7 +873,7 @@ result_type parse_url_impl(std::string_view user_input,
} else {
std::string_view path = url.get_pathname();
if (helpers::shorten_path(path, url.type)) {
url.update_base_pathname(std::string(path));
url.update_base_pathname(std::move(std::string(path)));
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ std::string percent_decode(const std::string_view input, size_t first_percent) {
!is_ascii_hex_digit(pointer[2])))) {
dest += ch;
pointer++;
continue;
} else {
unsigned a = convert_hex_to_binary(pointer[1]);
unsigned b = convert_hex_to_binary(pointer[2]);
Expand All @@ -386,10 +385,9 @@ std::string percent_decode(const std::string_view input, size_t first_percent) {

std::string percent_encode(const std::string_view input,
const uint8_t character_set[]) {
auto pointer =
std::find_if(input.begin(), input.end(), [character_set](const char c) {
return character_sets::bit_at(character_set, c);
});
auto pointer = std::ranges::find_if(input, [character_set](const char c) {
return character_sets::bit_at(character_set, c);
});
// Optimization: Don't iterate if percent encode is not required
if (pointer == input.end()) {
return std::string(input);
Expand Down
Loading

0 comments on commit 37c4997

Please sign in to comment.