Skip to content

Commit 23988b9

Browse files
authored
Formatting updates and apply ranges in url_search_parameters (#142)
* Use ranges algorithms in search parameters * Ran clang-format on url_parser_context.cpp
1 parent b668d54 commit 23988b9

File tree

3 files changed

+68
-108
lines changed

3 files changed

+68
-108
lines changed

include/skyr/v1/url_search_parameters.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <optional>
1414
#include <algorithm>
1515
#include <cassert>
16+
#include <range/v3/algorithm/sort.hpp>
1617

1718
namespace skyr {
1819
inline namespace v1 {
@@ -121,28 +122,28 @@ class url_search_parameters {
121122
void sort() {
122123
static constexpr auto less_name = [](const auto &lhs, const auto &rhs) { return lhs.first < rhs.first; };
123124

124-
std::sort(std::begin(parameters_), std::end(parameters_), less_name);
125+
ranges::sort(parameters_, less_name);
125126
update();
126127
}
127128

128129
/// \returns An iterator to the first element in the search parameters
129-
[[nodiscard]] auto begin() const noexcept {
130-
return parameters_.begin();
130+
[[nodiscard]] auto cbegin() const noexcept {
131+
return parameters_.cbegin();
131132
}
132133

133134
/// \returns An iterator to the last element in the search parameters
134-
[[nodiscard]] auto end() const noexcept {
135-
return parameters_.end();
135+
[[nodiscard]] auto cend() const noexcept {
136+
return parameters_.cend();
136137
}
137138

138139
/// \returns An iterator to the first element in the search parameters
139-
[[nodiscard]] auto cbegin() const noexcept {
140-
return begin();
140+
[[nodiscard]] auto begin() const noexcept {
141+
return cbegin();
141142
}
142143

143144
/// \returns An iterator to the last element in the search parameters
144-
[[nodiscard]] auto cend() const noexcept {
145-
return end();
145+
[[nodiscard]] auto end() const noexcept {
146+
return cend();
146147
}
147148

148149
/// \returns `true` if the URL search string is empty, `false`

src/v1/core/url_parser_context.cpp

Lines changed: 45 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <skyr/v1/core/schemes.hpp>
1212
#include <skyr/v1/core/host.hpp>
1313
#include <skyr/v1/percent_encoding/percent_encoded_char.hpp>
14-
#include <skyr/v1/percent_encoding/percent_decode_range.hpp>
1514
#include <skyr/v1/string/starts_with.hpp>
1615
#include "url_parser_context.hpp"
1716

@@ -21,14 +20,12 @@ using namespace std::string_literals;
2120
using namespace std::string_view_literals;
2221

2322
namespace {
24-
auto contains(char byte, std::string_view view) noexcept {
25-
auto first = begin(view), last = end(view);
23+
auto contains(std::string_view view, char byte) noexcept {
24+
auto first = std::cbegin(view), last = std::cend(view);
2625
return last != std::find(first, last, byte);
2726
}
2827

29-
auto remaining_starts_with(
30-
std::string_view input,
31-
std::string_view chars) noexcept {
28+
auto remaining_starts_with(std::string_view input, std::string_view chars) noexcept {
3229
return !input.empty() && starts_with(input.substr(1), chars);
3330
}
3431

@@ -37,8 +34,8 @@ auto port_number(std::string_view port) noexcept -> tl::expected<std::uint16_t,
3734
return tl::make_unexpected(url_parse_errc::invalid_port);
3835
}
3936

40-
const char* port_first = port.data();
41-
char* port_last = nullptr;
37+
const char *port_first = port.data();
38+
char *port_last = nullptr;
4239
auto port_value = std::strtoul(port_first, &port_last, 10);
4340

4441
if (port_first == port_last) {
@@ -52,8 +49,7 @@ auto port_number(std::string_view port) noexcept -> tl::expected<std::uint16_t,
5249
}
5350

5451
auto is_url_code_point(char byte) noexcept {
55-
return
56-
std::isalnum(byte, std::locale::classic()) || contains(byte, "!$&'()*+,-./:;=?@_~"sv);
52+
return std::isalnum(byte, std::locale::classic()) || contains("!$&'()*+,-./:;=?@_~"sv, byte);
5753
}
5854

5955
inline auto is_windows_drive_letter(std::string_view segment) noexcept {
@@ -85,37 +81,26 @@ auto is_double_dot_path_segment(std::string_view segment) noexcept {
8581
}
8682

8783
void shorten_path(std::string_view scheme, std::vector<std::string> &path) {
88-
if (path.empty()) {
89-
return;
84+
if (!path.empty() && !((scheme == "file"sv) && (path.size() == 1) && is_windows_drive_letter(path.front()))) {
85+
path.pop_back();
9086
}
91-
92-
if ((scheme == "file"sv) &&
93-
(path.size() == 1) &&
94-
is_windows_drive_letter(path.front())) {
95-
return;
96-
}
97-
98-
path.pop_back();
9987
}
100-
} // namespace
101-
102-
url_parser_context::url_parser_context(
103-
std::string_view input,
104-
bool *validation_error,
105-
const url_record *base,
106-
const url_record *url,
107-
std::optional<url_parse_state> state_override)
108-
: input(input)
109-
, it(begin(input))
110-
, validation_error(validation_error)
111-
, base(base)
112-
, url(http://github.com/url?%20*url%20:%20url_record{})
113-
, state(state_override? state_override.value() : url_parse_state::scheme_start)
114-
, state_override(state_override)
115-
, buffer()
116-
, at_flag(false)
117-
, square_braces_flag(false)
118-
, password_token_seen_flag(false) {}
88+
} // namespace
89+
90+
url_parser_context::url_parser_context(std::string_view input, bool *validation_error, const url_record *base,
91+
const url_record *url, std::optional<url_parse_state> state_override)
92+
: input(input),
93+
it(begin(input)),
94+
validation_error(validation_error),
95+
base(base),
96+
url(http://github.com/url%20?%20*url%20:%20url_record{}),
97+
state(state_override ? state_override.value() : url_parse_state::scheme_start),
98+
state_override(state_override),
99+
buffer(),
100+
at_flag(false),
101+
square_braces_flag(false),
102+
password_token_seen_flag(false) {
103+
}
119104

120105
auto url_parser_context::parse_scheme_start(char byte) -> tl::expected<url_parse_action, url_parse_errc> {
121106
if (std::isalpha(byte, std::locale::classic())) {
@@ -135,7 +120,7 @@ auto url_parser_context::parse_scheme_start(char byte) -> tl::expected<url_parse
135120
}
136121

137122
auto url_parser_context::parse_scheme(char byte) -> tl::expected<url_parse_action, url_parse_errc> {
138-
if (std::isalnum(byte, std::locale::classic()) || contains(byte, "+-."sv)) {
123+
if (std::isalnum(byte, std::locale::classic()) || contains("+-."sv, byte)) {
139124
auto lower = std::tolower(byte, std::locale::classic());
140125
buffer.push_back(lower);
141126
} else if (byte == ':') {
@@ -191,8 +176,7 @@ auto url_parser_context::parse_scheme(char byte) -> tl::expected<url_parse_actio
191176
state = url_parse_state::no_scheme;
192177
reset();
193178
return url_parse_action::continue_;
194-
}
195-
else {
179+
} else {
196180
return tl::make_unexpected(url_parse_errc::invalid_scheme_character);
197181
}
198182

@@ -256,8 +240,7 @@ auto url_parser_context::parse_relative(char byte) -> tl::expected<url_parse_act
256240
url.port = base->port;
257241
url.path = base->path;
258242
url.query = base->query;
259-
}
260-
else if (byte == '/') {
243+
} else if (byte == '/') {
261244
state = url_parse_state::relative_slash;
262245
} else if (byte == '?') {
263246
url.username = base->username;
@@ -308,11 +291,9 @@ auto url_parser_context::parse_relative_slash(char byte) -> tl::expected<url_par
308291
*validation_error |= true;
309292
}
310293
state = url_parse_state::special_authority_ignore_slashes;
311-
}
312-
else if (byte == '/') {
294+
} else if (byte == '/') {
313295
state = url_parse_state::authority;
314-
}
315-
else {
296+
} else {
316297
url.username = base->username;
317298
url.password = base->password;
318299
url.host = base->host;
@@ -338,7 +319,8 @@ auto url_parser_context::parse_special_authority_slashes(char byte) -> tl::expec
338319
return url_parse_action::increment;
339320
}
340321

341-
auto url_parser_context::parse_special_authority_ignore_slashes(char byte) -> tl::expected<url_parse_action, url_parse_errc> {
322+
auto url_parser_context::parse_special_authority_ignore_slashes(char byte)
323+
-> tl::expected<url_parse_action, url_parse_errc> {
342324
if ((byte != '/') && (byte != '\\')) {
343325
decrement();
344326
state = url_parse_state::authority;
@@ -370,9 +352,7 @@ auto url_parser_context::parse_authority(char byte) -> tl::expected<url_parse_ac
370352
}
371353
}
372354
buffer.clear();
373-
} else if (
374-
((is_eof()) || (byte == '/') || (byte == '?') || (byte == '#')) ||
375-
(url.is_special() && (byte == '\\'))) {
355+
} else if (((is_eof()) || (byte == '/') || (byte == '?') || (byte == '#')) || (url.is_special() && (byte == '\\'))) {
376356
if (at_flag && buffer.empty()) {
377357
*validation_error |= true;
378358
return tl::make_unexpected(url_parse_errc::empty_hostname);
@@ -412,21 +392,15 @@ auto url_parser_context::parse_hostname(char byte) -> tl::expected<url_parse_act
412392
if (state_override && (state_override.value() == url_parse_state::hostname)) {
413393
return url_parse_action::success;
414394
}
415-
} else if (
416-
(is_eof() || (byte == '/') || (byte == '?') || (byte == '#')) ||
417-
(url.is_special() && (byte == '\\'))) {
395+
} else if ((is_eof() || (byte == '/') || (byte == '?') || (byte == '#')) || (url.is_special() && (byte == '\\'))) {
418396
if (it != begin(input)) {
419397
decrement();
420398
}
421399

422400
if (url.is_special() && buffer.empty()) {
423401
*validation_error |= true;
424402
return tl::make_unexpected(url_parse_errc::empty_hostname);
425-
}
426-
else if (
427-
state_override &&
428-
buffer.empty() &&
429-
(url.includes_credentials() || url.port)) {
403+
} else if (state_override && buffer.empty() && (url.includes_credentials() || url.port)) {
430404
*validation_error |= true;
431405
return url_parse_action::success;
432406
}
@@ -456,10 +430,8 @@ auto url_parser_context::parse_hostname(char byte) -> tl::expected<url_parse_act
456430
auto url_parser_context::parse_port(char byte) -> tl::expected<url_parse_action, url_parse_errc> {
457431
if (std::isdigit(byte, std::locale::classic())) {
458432
buffer += byte;
459-
} else if (
460-
((is_eof()) || (byte == '/') || (byte == '?') || (byte == '#')) ||
461-
(url.is_special() && (byte == '\\')) ||
462-
state_override) {
433+
} else if (((is_eof()) || (byte == '/') || (byte == '?') || (byte == '#')) || (url.is_special() && (byte == '\\')) ||
434+
state_override) {
463435
if (!buffer.empty()) {
464436
auto port = port_number(buffer);
465437

@@ -471,8 +443,7 @@ auto url_parser_context::parse_port(char byte) -> tl::expected<url_parse_action,
471443
auto dport = default_port(url.scheme);
472444
if (dport && (dport.value() == port.value())) {
473445
url.port = std::nullopt;
474-
}
475-
else {
446+
} else {
476447
url.port = port.value();
477448
}
478449
buffer.clear();
@@ -505,8 +476,7 @@ auto url_parser_context::parse_file(char byte) -> tl::expected<url_parse_action,
505476
url.host = base->host;
506477
url.path = base->path;
507478
url.query = base->query;
508-
}
509-
else if (byte == '?') {
479+
} else if (byte == '?') {
510480
url.host = base->host;
511481
url.path = base->path;
512482
url.query = std::string();
@@ -522,8 +492,7 @@ auto url_parser_context::parse_file(char byte) -> tl::expected<url_parse_action,
522492
url.host = base->host;
523493
url.path = base->path;
524494
shorten_path(url.scheme, url.path);
525-
}
526-
else {
495+
} else {
527496
*validation_error |= true;
528497
}
529498
state = url_parse_state::path;
@@ -551,8 +520,7 @@ auto url_parser_context::parse_file_slash(char byte) -> tl::expected<url_parse_a
551520
state = url_parse_state::file_host;
552521
} else {
553522
auto substr = input.substr(std::distance(begin(input), it));
554-
if (base &&
555-
((base->scheme == "file") && !is_windows_drive_letter(substr))) {
523+
if (base && ((base->scheme == "file") && !is_windows_drive_letter(substr))) {
556524
if (!base->path.empty() && is_windows_drive_letter(base->path[0])) {
557525
url.path.push_back(base->path[0]);
558526
} else {
@@ -643,8 +611,7 @@ auto url_parser_context::parse_path_start(char byte) -> tl::expected<url_parse_a
643611
}
644612

645613
auto url_parser_context::parse_path(char byte) -> tl::expected<url_parse_action, url_parse_errc> {
646-
if (((is_eof()) || (byte == '/')) ||
647-
(url.is_special() && (byte == '\\')) ||
614+
if (((is_eof()) || (byte == '/')) || (url.is_special() && (byte == '\\')) ||
648615
(!state_override && ((byte == '?') || (byte == '#')))) {
649616
if (url.is_special() && (byte == '\\')) {
650617
*validation_error |= true;
@@ -655,13 +622,10 @@ auto url_parser_context::parse_path(char byte) -> tl::expected<url_parse_action,
655622
if (!((byte == '/') || (url.is_special() && (byte == '\\')))) {
656623
url.path.emplace_back();
657624
}
658-
} else if (
659-
is_single_dot_path_segment(buffer) &&
660-
!((byte == '/') || (url.is_special() && (byte == '\\')))) {
625+
} else if (is_single_dot_path_segment(buffer) && !((byte == '/') || (url.is_special() && (byte == '\\')))) {
661626
url.path.emplace_back();
662627
} else if (!is_single_dot_path_segment(buffer)) {
663-
if ((url.scheme == "file") &&
664-
url.path.empty() && is_windows_drive_letter(buffer)) {
628+
if ((url.scheme == "file") && url.path.empty() && is_windows_drive_letter(buffer)) {
665629
if (!url.host || !url.host.value().is_empty()) {
666630
*validation_error |= true;
667631
url.host = skyr::host{skyr::v1::empty_host{}};
@@ -716,8 +680,7 @@ auto url_parser_context::parse_cannot_be_a_base_url(http://github.com/char%20byte) -> tl::expected<u
716680
auto substr = input.substr(std::distance(std::begin(input), it));
717681
if (!is_eof() && (!is_url_code_point(byte) && (byte != '%'))) {
718682
*validation_error |= true;
719-
}
720-
else if ((byte == '%') && !percent_encoding::is_percent_encoded(substr)) {
683+
} else if ((byte == '%') && !percent_encoding::is_percent_encoded(substr)) {
721684
*validation_error |= true;
722685
}
723686
if (!is_eof()) {
@@ -733,10 +696,7 @@ auto url_parser_context::parse_query(char byte) -> tl::expected<url_parse_action
733696
url.fragment = std::string();
734697
state = url_parse_state::fragment;
735698
} else if (!is_eof()) {
736-
if ((byte < '!') ||
737-
(byte > '~') ||
738-
(contains(byte, R"("#<>)"sv)) ||
739-
((byte == '\'') && url.is_special())) {
699+
if ((byte < '!') || (byte > '~') || (contains(R"("#<>)"sv, byte)) || ((byte == '\'') && url.is_special())) {
740700
auto pct_encoded = percent_encode_byte(std::byte(byte), percent_encoding::encode_set::none);
741701
url.query.value() += pct_encoded.to_string();
742702
} else {

0 commit comments

Comments
 (0)