Skip to content

Commit 1f6a902

Browse files
authored
using ranges (#942)
1 parent 1b02e31 commit 1f6a902

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

src/checkers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ ada_really_inline constexpr bool is_ipv4(std::string_view view) noexcept {
5656
}
5757
// We have 0x followed by some characters, we need to check that they are
5858
// hexadecimals.
59-
return std::all_of(view.begin() + 2, view.end(),
60-
ada::unicode::is_lowercase_hex);
59+
view.remove_prefix(2);
60+
return std::ranges::all_of(view, ada::unicode::is_lowercase_hex);
6161
}
6262

6363
// for use with path_signature, we include all characters that need percent

src/parser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ada/parser-inl.h"
22

33
#include <limits>
4+
#include <ranges>
45

56
#include "ada/character_sets-inl.h"
67
#include "ada/common_defs.h"
@@ -624,7 +625,7 @@ result_type parse_url_impl(std::string_view user_input,
624625
// to optimize it.
625626
if (view.ends_with(' ')) {
626627
std::string modified_view =
627-
std::string(view.begin(), view.end() - 1) + "%20";
628+
std::string(view.substr(0, view.size() - 1)) + "%20";
628629
url.update_base_pathname(unicode::percent_encode(
629630
modified_view, character_sets::C0_CONTROL_PERCENT_ENCODE));
630631
} else {

src/unicode.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ ADA_POP_DISABLE_WARNINGS
1515
#include <emmintrin.h>
1616
#endif
1717

18+
#include <ranges>
19+
1820
namespace ada::unicode {
1921

2022
constexpr bool is_tabs_or_newline(char c) noexcept {
@@ -55,8 +57,7 @@ ada_really_inline bool has_tabs_or_newline(
5557
std::string_view user_input) noexcept {
5658
// first check for short strings in which case we do it naively.
5759
if (user_input.size() < 16) { // slow path
58-
return std::any_of(user_input.begin(), user_input.end(),
59-
is_tabs_or_newline);
60+
return std::ranges::any_of(user_input, is_tabs_or_newline);
6061
}
6162
// fast path for long strings (expected to be common)
6263
size_t i = 0;
@@ -94,8 +95,7 @@ ada_really_inline bool has_tabs_or_newline(
9495
std::string_view user_input) noexcept {
9596
// first check for short strings in which case we do it naively.
9697
if (user_input.size() < 16) { // slow path
97-
return std::any_of(user_input.begin(), user_input.end(),
98-
is_tabs_or_newline);
98+
return std::ranges::any_of(user_input, is_tabs_or_newline);
9999
}
100100
// fast path for long strings (expected to be common)
101101
size_t i = 0;
@@ -426,10 +426,9 @@ bool percent_encode(const std::string_view input, const uint8_t character_set[],
426426
std::string& out) {
427427
ada_log("percent_encode ", input, " to output string while ",
428428
append ? "appending" : "overwriting");
429-
auto pointer =
430-
std::find_if(input.begin(), input.end(), [character_set](const char c) {
431-
return character_sets::bit_at(character_set, c);
432-
});
429+
auto pointer = std::ranges::find_if(input, [character_set](const char c) {
430+
return character_sets::bit_at(character_set, c);
431+
});
433432
ada_log("percent_encode done checking, moved to ",
434433
std::distance(input.begin(), pointer));
435434

src/url.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
#include <numeric>
66
#include <algorithm>
7+
#include <ranges>
78
#include <string>
89
#include <string_view>
910

1011
namespace ada {
1112

1213
bool url::parse_opaque_host(std::string_view input) {
1314
ada_log("parse_opaque_host ", input, " [", input.size(), " bytes]");
14-
if (std::ranges::any_of(input.begin(), input.end(),
15-
ada::unicode::is_forbidden_host_code_point)) {
15+
if (std::ranges::any_of(input, ada::unicode::is_forbidden_host_code_point)) {
1616
return is_valid = false;
1717
}
1818

src/url_aggregator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "ada/url_aggregator.h"
99
#include "ada/url_aggregator-inl.h"
1010

11+
#include <ranges>
1112
#include <string>
1213
#include <string_view>
1314

@@ -227,7 +228,7 @@ bool url_aggregator::set_protocol(const std::string_view input) {
227228

228229
if (pointer != view.end() && *pointer == ':') {
229230
return parse_scheme_with_colon<true>(
230-
std::string_view(view.data(), pointer - view.begin() + 1));
231+
view.substr(0, pointer - view.begin() + 1));
231232
}
232233
return false;
233234
}
@@ -489,8 +490,8 @@ ada_really_inline bool url_aggregator::parse_host(std::string_view input) {
489490
ada_log("parse_host to_ascii succeeded ", *host, " [", host->size(),
490491
" bytes]");
491492

492-
if (std::any_of(host.value().begin(), host.value().end(),
493-
ada::unicode::is_forbidden_domain_code_point)) {
493+
if (std::ranges::any_of(host.value(),
494+
ada::unicode::is_forbidden_domain_code_point)) {
494495
return is_valid = false;
495496
}
496497

@@ -1182,8 +1183,7 @@ bool url_aggregator::parse_opaque_host(std::string_view input) {
11821183
ada_log("parse_opaque_host ", input, " [", input.size(), " bytes]");
11831184
ADA_ASSERT_TRUE(validate());
11841185
ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
1185-
if (std::any_of(input.begin(), input.end(),
1186-
ada::unicode::is_forbidden_host_code_point)) {
1186+
if (std::ranges::any_of(input, ada::unicode::is_forbidden_host_code_point)) {
11871187
return is_valid = false;
11881188
}
11891189

0 commit comments

Comments
 (0)