diff --git a/include/ada/helpers.h b/include/ada/helpers.h index b9a553c02..d20473f0c 100644 --- a/include/ada/helpers.h +++ b/include/ada/helpers.h @@ -12,6 +12,10 @@ #include #include +#if ADA_DEVELOPMENT_CHECKS +#include +#endif // ADA_DEVELOPMENT_CHECKS + /** * These functions are not part of our public API and may * change at any time. @@ -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 get_host_delimiter_location( - const bool is_special, std::string_view& view) noexcept; + bool is_special, std::string_view& view) noexcept; /** * @private diff --git a/include/ada/log.h b/include/ada/log.h index 5b00d360c..7f5948346 100644 --- a/include/ada/log.h +++ b/include/ada/log.h @@ -7,12 +7,15 @@ #define ADA_LOG_H #include "ada/common_defs.h" -#include // To enable logging, set ADA_LOGGING to 1: #ifndef ADA_LOGGING #define ADA_LOGGING 0 #endif +#if ADA_LOGGING +#include +#endif // ADA_LOGGING + namespace ada { /** diff --git a/include/ada/scheme-inl.h b/include/ada/scheme-inl.h index a327dd049..46891c7b0 100644 --- a/include/ada/scheme-inl.h +++ b/include/ada/scheme-inl.h @@ -50,7 +50,7 @@ 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(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)); } @@ -58,7 +58,7 @@ 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(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]; @@ -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(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(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); diff --git a/include/ada/scheme.h b/include/ada/scheme.h index f14a85e6d..b83dccb48 100644 --- a/include/ada/scheme.h +++ b/include/ada/scheme.h @@ -8,7 +8,6 @@ #include "ada/common_defs.h" #include -#include #include /** diff --git a/include/ada/serializers.h b/include/ada/serializers.h index d8e0d3d6f..260dcf3a1 100644 --- a/include/ada/serializers.h +++ b/include/ada/serializers.h @@ -8,7 +8,6 @@ #include "ada/common_defs.h" #include -#include #include /** diff --git a/include/ada/unicode-inl.h b/include/ada/unicode-inl.h index 7bbbd8fe3..772af6cf9 100644 --- a/include/ada/unicode-inl.h +++ b/include/ada/unicode-inl.h @@ -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); })); } diff --git a/include/ada/url-inl.h b/include/ada/url-inl.h index 863e6c5e7..6976eadea 100644 --- a/include/ada/url-inl.h +++ b/include/ada/url-inl.h @@ -133,7 +133,7 @@ inline void url::update_base_search(std::string_view input, } inline void url::update_base_search(std::optional input) { - query = input; + query = std::move(input); } 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, 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 &= @@ -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(parsed_port) - : std::nullopt; + port = (r.ec == std::errc() && is_port_valid) ? std::optional(parsed_port) + : std::nullopt; } return consumed; } diff --git a/include/ada/url_aggregator-inl.h b/include/ada/url_aggregator-inl.h index 483214a75..dc727cf9f 100644 --- a/include/ada/url_aggregator-inl.h +++ b/include/ada/url_aggregator-inl.h @@ -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(input.size()); uint32_t new_difference = input_size - current_length; if (current_length == 0) { @@ -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(buffer.size()); if (components.search_start != url_components::omitted) { ending_index = components.search_start; } else if (components.hash_start != url_components::omitted) { @@ -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(buffer.size()); if (components.search_start != url_components::omitted) { ending_index = components.search_start; } else if (components.hash_start != url_components::omitted) { @@ -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(input.size()); buffer.insert(components.username_end, input); components.username_end += difference; components.host_start += difference; @@ -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(input.size()); if (password_exists) { uint32_t current_length = @@ -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(input.size()); if (has_password()) { buffer.insert(components.host_start, input); } else { @@ -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(value.size()); if (components.port != url_components::omitted) { difference -= components.pathname_start - components.host_end; @@ -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(buffer.size()); if (components.search_start != url_components::omitted) { ending_index = components.search_start; } else if (components.hash_start != url_components::omitted) { @@ -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(r.ptr - view.data()); ada_log("parse_port: consumed ", consumed); if (check_trailing_content) { is_valid &= diff --git a/include/ada/url_search_params-inl.h b/include/ada/url_search_params-inl.h index 6edb37c52..0ea2017ae 100644 --- a/include/ada/url_search_params-inl.h +++ b/include/ada/url_search_params-inl.h @@ -193,7 +193,7 @@ inline url_search_params_entries_iter url_search_params::get_entries() { } template -inline bool url_search_params_iter::has_next() { +inline bool url_search_params_iter::has_next() const { return pos < params.params.size(); } diff --git a/include/ada/url_search_params.h b/include/ada/url_search_params.h index 00052c786..84018087a 100644 --- a/include/ada/url_search_params.h +++ b/include/ada/url_search_params.h @@ -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; @@ -172,7 +174,7 @@ struct url_search_params_iter { */ inline std::optional next(); - inline bool has_next(); + inline bool has_next() const; private: static url_search_params EMPTY; diff --git a/src/checkers.cpp b/src/checkers.cpp index ffca6ddb3..fc3b08ab0 100644 --- a/src/checkers.cpp +++ b/src/checkers.cpp @@ -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'; @@ -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. @@ -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(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(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; diff --git a/src/helpers.cpp b/src/helpers.cpp index c5926cb65..b84b533ec 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -96,13 +96,11 @@ ada_really_inline std::optional 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; @@ -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; @@ -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, diff --git a/src/parser.cpp b/src/parser.cpp index cbc375675..b07906733 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -238,10 +238,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; } @@ -253,12 +252,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) && @@ -362,8 +361,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 { @@ -458,7 +456,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. @@ -519,8 +517,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; } @@ -553,7 +550,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()) { @@ -565,8 +562,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) @@ -597,7 +593,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; } @@ -620,7 +616,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('?'); @@ -640,10 +636,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; } @@ -698,8 +692,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 '?'. @@ -752,7 +746,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('/'); @@ -780,8 +774,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( @@ -827,8 +821,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) { @@ -881,7 +874,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))); } } } diff --git a/src/serializers.cpp b/src/serializers.cpp index 91be39ce1..b744e510a 100644 --- a/src/serializers.cpp +++ b/src/serializers.cpp @@ -68,10 +68,13 @@ std::string ipv4(const uint64_t address) noexcept { std::string output(15, '\0'); char* point = output.data(); char* point_end = output.data() + output.size(); - point = std::to_chars(point, point_end, uint8_t(address >> 24)).ptr; + point = + std::to_chars(point, point_end, static_cast(address >> 24)).ptr; for (int i = 2; i >= 0; i--) { *point++ = '.'; - point = std::to_chars(point, point_end, uint8_t(address >> (i * 8))).ptr; + point = std::to_chars(point, point_end, + static_cast(address >> (i * 8))) + .ptr; } output.resize(point - output.data()); return output; diff --git a/src/unicode.cpp b/src/unicode.cpp index c979c0250..aaccc925d 100644 --- a/src/unicode.cpp +++ b/src/unicode.cpp @@ -198,13 +198,18 @@ ada_really_inline constexpr bool contains_forbidden_domain_code_point( size_t i = 0; uint8_t accumulator{}; for (; i + 4 <= length; i += 4) { - accumulator |= is_forbidden_domain_code_point_table[uint8_t(input[i])]; - accumulator |= is_forbidden_domain_code_point_table[uint8_t(input[i + 1])]; - accumulator |= is_forbidden_domain_code_point_table[uint8_t(input[i + 2])]; - accumulator |= is_forbidden_domain_code_point_table[uint8_t(input[i + 3])]; + accumulator |= + is_forbidden_domain_code_point_table[static_cast(input[i])]; + accumulator |= is_forbidden_domain_code_point_table[static_cast( + input[i + 1])]; + accumulator |= is_forbidden_domain_code_point_table[static_cast( + input[i + 2])]; + accumulator |= is_forbidden_domain_code_point_table[static_cast( + input[i + 3])]; } for (; i < length; i++) { - accumulator |= is_forbidden_domain_code_point_table[uint8_t(input[i])]; + accumulator |= + is_forbidden_domain_code_point_table[static_cast(input[i])]; } return accumulator; } @@ -235,17 +240,22 @@ contains_forbidden_domain_code_point_or_upper(const char* input, uint8_t accumulator{}; for (; i + 4 <= length; i += 4) { accumulator |= - is_forbidden_domain_code_point_table_or_upper[uint8_t(input[i])]; + is_forbidden_domain_code_point_table_or_upper[static_cast( + input[i])]; accumulator |= - is_forbidden_domain_code_point_table_or_upper[uint8_t(input[i + 1])]; + is_forbidden_domain_code_point_table_or_upper[static_cast( + input[i + 1])]; accumulator |= - is_forbidden_domain_code_point_table_or_upper[uint8_t(input[i + 2])]; + is_forbidden_domain_code_point_table_or_upper[static_cast( + input[i + 2])]; accumulator |= - is_forbidden_domain_code_point_table_or_upper[uint8_t(input[i + 3])]; + is_forbidden_domain_code_point_table_or_upper[static_cast( + input[i + 3])]; } for (; i < length; i++) { accumulator |= - is_forbidden_domain_code_point_table_or_upper[uint8_t(input[i])]; + is_forbidden_domain_code_point_table_or_upper[static_cast( + input[i])]; } return accumulator; } @@ -273,7 +283,7 @@ ada_really_inline constexpr bool is_ascii_hex_digit(const char c) noexcept { } ada_really_inline constexpr bool is_c0_control_or_space(const char c) noexcept { - return (unsigned char)c <= ' '; + return static_cast(c) <= ' '; } ada_really_inline constexpr bool is_ascii_tab_or_newline( @@ -300,7 +310,7 @@ ada_really_inline ada_constexpr bool is_double_dot_path_segment( return false; } // We are unlikely the get beyond this point. - int hash_value = (input.size() + (unsigned)(input[0])) & 3; + int hash_value = (input.size() + static_cast(input[0])) & 3; const std::string_view target = table_is_double_dot_path_segment[hash_value]; if (target.size() != input.size()) { return false; @@ -318,7 +328,8 @@ ada_really_inline ada_constexpr bool is_double_dot_path_segment( } for (size_t i = 2; i < input.size(); i++) { char c = input[i]; - if ((uint8_t((c | 0x20) - 0x61) <= 25 ? (c | 0x20) : c) != target[i]) { + if ((static_cast((c | 0x20) - 0x61) <= 25 ? (c | 0x20) : c) != + target[i]) { return false; } } @@ -372,7 +383,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]); @@ -386,10 +396,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); diff --git a/src/url-setters.cpp b/src/url-setters.cpp index a368ef3a3..62b717335 100644 --- a/src/url-setters.cpp +++ b/src/url-setters.cpp @@ -177,8 +177,7 @@ void url::set_search(const std::string_view input) { is_special() ? ada::character_sets::SPECIAL_QUERY_PERCENT_ENCODE : ada::character_sets::QUERY_PERCENT_ENCODE; - query = ada::unicode::percent_encode(std::string_view(new_value), - query_percent_encode_set); + query = ada::unicode::percent_encode(new_value, query_percent_encode_set); } bool url::set_pathname(const std::string_view input) { @@ -205,7 +204,7 @@ bool url::set_protocol(const std::string_view input) { view.append(":"); std::string::iterator pointer = - std::find_if_not(view.begin(), view.end(), unicode::is_alnum_plus); + std::ranges::find_if_not(view, unicode::is_alnum_plus); if (pointer != view.end() && *pointer == ':') { return parse_scheme( @@ -218,16 +217,7 @@ bool url::set_href(const std::string_view input) { ada::result out = ada::parse(input); if (out) { - username = out->username; - password = out->password; - host = out->host; - port = out->port; - path = out->path; - query = out->query; - hash = out->hash; - type = out->type; - non_special_scheme = out->non_special_scheme; - has_opaque_path = out->has_opaque_path; + *this = *out; } return out.has_value(); diff --git a/src/url.cpp b/src/url.cpp index 657364381..04d7580cd 100644 --- a/src/url.cpp +++ b/src/url.cpp @@ -5,13 +5,14 @@ #include #include #include +#include namespace ada { bool url::parse_opaque_host(std::string_view input) { ada_log("parse_opaque_host ", input, " [", input.size(), " bytes]"); - if (std::any_of(input.begin(), input.end(), - ada::unicode::is_forbidden_host_code_point)) { + if (std::ranges::any_of(input.begin(), input.end(), + ada::unicode::is_forbidden_host_code_point)) { return is_valid = false; } @@ -513,18 +514,14 @@ ada_really_inline void url::parse_path(std::string_view input) { path = "/"; } else if ((internal_input[0] == '/') || (internal_input[0] == '\\')) { helpers::parse_prepared_path(internal_input.substr(1), type, path); - return; } else { helpers::parse_prepared_path(internal_input, type, path); - return; } } else if (!internal_input.empty()) { if (internal_input[0] == '/') { helpers::parse_prepared_path(internal_input.substr(1), type, path); - return; } else { helpers::parse_prepared_path(internal_input, type, path); - return; } } else { if (!host.has_value()) { diff --git a/src/url_aggregator.cpp b/src/url_aggregator.cpp index 48ed79b8e..2c431cef1 100644 --- a/src/url_aggregator.cpp +++ b/src/url_aggregator.cpp @@ -22,7 +22,7 @@ template std::string_view input{input_with_colon}; input.remove_suffix(1); auto parsed_type = ada::scheme::get_scheme_type(input); - bool is_input_special = (parsed_type != ada::scheme::NOT_SPECIAL); + const bool is_input_special = (parsed_type != ada::scheme::NOT_SPECIAL); /** * In the common case, we will immediately recognize a special scheme (e.g., *http, https), in which case, we can go really fast. @@ -224,7 +224,7 @@ bool url_aggregator::set_protocol(const std::string_view input) { view.append(":"); std::string::iterator pointer = - std::find_if_not(view.begin(), view.end(), unicode::is_alnum_plus); + std::ranges::find_if_not(view, unicode::is_alnum_plus); if (pointer != view.end() && *pointer == ':') { return parse_scheme_with_colon(