Skip to content

Commit

Permalink
more c++20 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Oct 7, 2024
1 parent c0ed932 commit 79d1342
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 118 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
8 changes: 4 additions & 4 deletions include/ada/scheme-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ ada_really_inline constexpr bool is_special(std::string_view scheme) {
if (scheme.empty()) {
return false;
}
int hash_value = (2 * scheme.size() + (unsigned)(scheme[0])) & 7;
int hash_value = (2 * scheme.size() + static_cast<unsigned>(scheme[0])) & 7;
const std::string_view target = details::is_special_list[hash_value];
return (target[0] == scheme[0]) && (target.substr(1) == scheme.substr(1));
}
constexpr uint16_t get_special_port(std::string_view scheme) noexcept {
if (scheme.empty()) {
return 0;
}
int hash_value = (2 * scheme.size() + (unsigned)(scheme[0])) & 7;
int hash_value = (2 * scheme.size() + static_cast<unsigned>(scheme[0])) & 7;
const std::string_view target = details::is_special_list[hash_value];
if ((target[0] == scheme[0]) && (target.substr(1) == scheme.substr(1))) {
return details::special_ports[hash_value];
Expand All @@ -67,13 +67,13 @@ constexpr uint16_t get_special_port(std::string_view scheme) noexcept {
}
}
constexpr uint16_t get_special_port(ada::scheme::type type) noexcept {
return details::special_ports[int(type)];
return details::special_ports[static_cast<int>(type)];
}
constexpr ada::scheme::type get_scheme_type(std::string_view scheme) noexcept {
if (scheme.empty()) {
return ada::scheme::NOT_SPECIAL;
}
int hash_value = (2 * scheme.size() + (unsigned)(scheme[0])) & 7;
int hash_value = (2 * scheme.size() + static_cast<unsigned>(scheme[0])) & 7;
const std::string_view target = details::is_special_list[hash_value];
if ((target[0] == scheme[0]) && (target.substr(1) == scheme.substr(1))) {
return ada::scheme::type(hash_value);
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
9 changes: 4 additions & 5 deletions include/ada/url-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ inline void url::update_base_search(std::string_view input,
}

inline void url::update_base_search(std::optional<std::string> input) {
query = 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
18 changes: 9 additions & 9 deletions include/ada/url_aggregator-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ inline void url_aggregator::update_unencoded_base_hash(std::string_view input) {
ada_really_inline uint32_t url_aggregator::replace_and_resize(
uint32_t start, uint32_t end, std::string_view input) {
uint32_t current_length = end - start;
uint32_t input_size = uint32_t(input.size());
auto input_size = static_cast<uint32_t>(input.size());
uint32_t new_difference = input_size - current_length;

if (current_length == 0) {
Expand Down Expand Up @@ -155,7 +155,7 @@ inline void url_aggregator::update_base_hostname(const std::string_view input) {
[[nodiscard]] ada_really_inline uint32_t
url_aggregator::get_pathname_length() const noexcept {
ada_log("url_aggregator::get_pathname_length");
uint32_t ending_index = uint32_t(buffer.size());
auto ending_index = static_cast<uint32_t>(buffer.size());
if (components.search_start != url_components::omitted) {
ending_index = components.search_start;
} else if (components.hash_start != url_components::omitted) {
Expand Down Expand Up @@ -307,7 +307,7 @@ inline void url_aggregator::append_base_pathname(const std::string_view input) {
std::string path_expected(get_pathname());
path_expected.append(input);
#endif // ADA_DEVELOPMENT_CHECKS
uint32_t ending_index = uint32_t(buffer.size());
auto ending_index = static_cast<uint32_t>(buffer.size());
if (components.search_start != url_components::omitted) {
ending_index = components.search_start;
} else if (components.hash_start != url_components::omitted) {
Expand Down Expand Up @@ -384,7 +384,7 @@ inline void url_aggregator::append_base_username(const std::string_view input) {
return;
}

uint32_t difference = uint32_t(input.size());
auto difference = static_cast<uint32_t>(input.size());
buffer.insert(components.username_end, input);
components.username_end += difference;
components.host_start += difference;
Expand Down Expand Up @@ -452,7 +452,7 @@ inline void url_aggregator::update_base_password(const std::string_view input) {
}

bool password_exists = has_password();
uint32_t difference = uint32_t(input.size());
auto difference = static_cast<uint32_t>(input.size());

if (password_exists) {
uint32_t current_length =
Expand Down Expand Up @@ -503,7 +503,7 @@ inline void url_aggregator::append_base_password(const std::string_view input) {
return;
}

uint32_t difference = uint32_t(input.size());
auto difference = static_cast<uint32_t>(input.size());
if (has_password()) {
buffer.insert(components.host_start, input);
} else {
Expand Down Expand Up @@ -548,7 +548,7 @@ inline void url_aggregator::update_base_port(uint32_t input) {
// calling std::to_string(input.value()) is unfortunate given that the port
// value is probably already available as a string.
std::string value = helpers::concat(":", std::to_string(input));
uint32_t difference = uint32_t(value.size());
auto difference = static_cast<uint32_t>(value.size());

if (components.port != url_components::omitted) {
difference -= components.pathname_start - components.host_end;
Expand Down Expand Up @@ -637,7 +637,7 @@ inline void url_aggregator::clear_hash() {
constexpr void url_aggregator::clear_pathname() {
ada_log("url_aggregator::clear_pathname");
ADA_ASSERT_TRUE(validate());
uint32_t ending_index = uint32_t(buffer.size());
auto ending_index = static_cast<uint32_t>(buffer.size());
if (components.search_start != url_components::omitted) {
ending_index = components.search_start;
} else if (components.hash_start != url_components::omitted) {
Expand Down Expand Up @@ -867,7 +867,7 @@ ada_really_inline size_t url_aggregator::parse_port(
return 0;
}
ada_log("parse_port: ", parsed_port);
const size_t consumed = size_t(r.ptr - view.data());
const auto consumed = static_cast<size_t>(r.ptr - view.data());
ada_log("parse_port: consumed ", consumed);
if (check_trailing_content) {
is_valid &=
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
29 changes: 15 additions & 14 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 Expand Up @@ -88,24 +87,26 @@ ada_really_inline constexpr uint8_t path_signature(
size_t i = 0;
uint8_t accumulator{};
for (; i + 7 < input.size(); i += 8) {
accumulator |= uint8_t(path_signature_table[uint8_t(input[i])] |
path_signature_table[uint8_t(input[i + 1])] |
path_signature_table[uint8_t(input[i + 2])] |
path_signature_table[uint8_t(input[i + 3])] |
path_signature_table[uint8_t(input[i + 4])] |
path_signature_table[uint8_t(input[i + 5])] |
path_signature_table[uint8_t(input[i + 6])] |
path_signature_table[uint8_t(input[i + 7])]);
accumulator |=
static_cast<uint8_t>(path_signature_table[uint8_t(input[i])] |
path_signature_table[uint8_t(input[i + 1])] |
path_signature_table[uint8_t(input[i + 2])] |
path_signature_table[uint8_t(input[i + 3])] |
path_signature_table[uint8_t(input[i + 4])] |
path_signature_table[uint8_t(input[i + 5])] |
path_signature_table[uint8_t(input[i + 6])] |
path_signature_table[uint8_t(input[i + 7])]);
}
for (; i < input.size(); i++) {
accumulator |= uint8_t(path_signature_table[uint8_t(input[i])]);
accumulator |=
static_cast<uint8_t>(path_signature_table[uint8_t(input[i])]);
}
return accumulator;
}

ada_really_inline constexpr bool verify_dns_length(
std::string_view input) noexcept {
if (input.back() == '.') {
if (input.ends_with('.')) {
if (input.size() > 254) return false;
} else if (input.size() > 253)
return false;
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
Loading

0 comments on commit 79d1342

Please sign in to comment.