Skip to content

Commit 2a0a699

Browse files
authored
Updated some core functions, added extra tests for URL serializer (#69)
1 parent 915dcd2 commit 2a0a699

File tree

14 files changed

+113
-90
lines changed

14 files changed

+113
-90
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ AlwaysBreakBeforeMultilineStrings: true
1111
BreakBeforeBinaryOperators: None
1212
BreakConstructorInitializersBeforeComma: false
1313
BinPackParameters: true
14-
ColumnLimit: 80
14+
ColumnLimit: 120
1515
ConstructorInitializerAllOnOneLineOrOnePerLine: true
1616
ExperimentalAutoDetectBinPacking: false
1717
IndentCaseLabels: true

include/skyr/core/schemes.hpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,38 @@ using default_port_list = std::vector<std::pair<std::string, std::optional<std::
2121

2222
inline auto special_schemes() noexcept -> const default_port_list & {
2323
static const auto schemes = default_port_list{
24-
{"ftp", 21},
2524
{"file", std::nullopt},
25+
{"ftp", 21},
2626
{"http", 80},
2727
{"https", 443},
2828
{"ws", 80},
2929
{"wss", 443},
3030
};
3131
return schemes;
3232
}
33+
34+
inline auto scheme_less(
35+
const default_port_list::value_type &special_scheme,
36+
std::string_view scheme) -> bool {
37+
return special_scheme.first < scheme;
38+
};
3339
} // namespace details
3440

3541
/// \param scheme
3642
/// \returns
3743
inline auto is_special(std::string_view scheme) noexcept -> bool {
3844
const auto &schemes = details::special_schemes();
39-
auto first = begin(schemes), last = end(schemes);
40-
auto it = std::find_if(
41-
first, last,
42-
[&scheme](const auto &special_scheme) -> bool {
43-
return scheme == special_scheme.first;
44-
});
45-
return (it != last);
45+
auto it = std::lower_bound(begin(schemes), end(schemes), scheme, details::scheme_less);
46+
return ((it != end(schemes)) && !(scheme < it->first));
4647
}
4748

4849
/// \param scheme
4950
/// \returns
50-
inline auto default_port(std::string_view scheme) noexcept -> std::optional<std::uint16_t> {
51+
inline auto default_port(std::string_view scheme) noexcept
52+
-> std::optional<std::uint16_t> {
5153
const auto &schemes = details::special_schemes();
52-
auto first = begin(schemes), last = end(schemes);
53-
auto it = std::find_if(
54-
first, last,
55-
[&scheme](const auto &special_scheme) -> bool {
56-
return scheme == special_scheme.first;
57-
});
58-
if (it != last) {
59-
return it->second;
60-
}
61-
return std::nullopt;
54+
auto it = std::lower_bound(begin(schemes), end(schemes), scheme, details::scheme_less);
55+
return ((it != end(schemes)) && !(scheme < it->first)) ? it->second : std::nullopt;
6256
}
6357
} // namespace v1
6458
} // namespace skyr

include/skyr/core/url_record.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <string>
1111
#include <cstdint>
1212
#include <optional>
13+
#include <skyr/network/ipv4_address.hpp>
14+
#include <skyr/network/ipv6_address.hpp>
1315
#include <skyr/core/schemes.hpp>
1416

1517
namespace skyr {

include/skyr/network/ipv4_address.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class ipv4_address {
9494
}
9595

9696
/// \returns The address as a string
97-
[[nodiscard]] auto to_string() const -> std::string;
97+
[[nodiscard]] auto serialize() const -> std::string;
9898

9999
};
100100

include/skyr/network/ipv6_address.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class ipv6_address {
9797
}
9898

9999
/// \returns The IPv6 address as a string
100-
[[nodiscard]] auto to_string() const -> std::string;
100+
[[nodiscard]] auto serialize() const -> std::string;
101101

102102
};
103103

src/core/serialize.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@ auto serialize(
2222
output += "@";
2323
}
2424

25-
// TODO: serialize host
26-
output += url.host.value();
25+
if (auto ipv4_address = parse_ipv4_address(url.host.value())) {
26+
output += ipv4_address.value().serialize();
27+
}
28+
else if (auto ipv6_address = parse_ipv6_address(url.host.value())) {
29+
output += ipv6_address.value().serialize();
30+
}
31+
else {
32+
output += url.host.value();
33+
}
2734

2835
if (url.port) {
2936
output += ":";

src/core/url_parser_context.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ auto parse_host(
120120
view.remove_suffix(1);
121121
auto ipv6_address = parse_ipv6_address(view);
122122
if (ipv6_address) {
123-
return "[" + ipv6_address.value().to_string() + "]";
123+
return "[" + ipv6_address.value().serialize() + "]";
124124
}
125125
else {
126126
return tl::make_unexpected(url_parse_errc::invalid_ipv6_address);
@@ -158,7 +158,7 @@ auto parse_host(
158158
return ascii_domain.value();
159159
}
160160
}
161-
return host.value().to_string();
161+
return host.value().serialize();
162162
}
163163

164164
auto port_number(std::string_view port) noexcept -> tl::expected<std::uint16_t, url_parse_errc> {

src/network/ipv4_address.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ auto parse_ipv4_number(
4545
}
4646
} // namespace
4747

48-
auto ipv4_address::to_string() const -> std::string {
48+
auto ipv4_address::serialize() const -> std::string {
4949
using namespace std::string_literals;
5050

5151
auto output = ""s;

src/network/ipv6_address.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ inline auto hex_to_dec(char byte) noexcept {
3131
}
3232
} // namespace
3333

34-
auto ipv6_address::to_string() const -> std::string {
34+
auto ipv6_address::serialize() const -> std::string {
3535
using namespace std::string_literals;
3636

3737
auto output = ""s;

tests/network/ipv4_address_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ TEST_CASE("ipv4 addresses", "[ipv4]") {
1212

1313
SECTION("zero_test") {
1414
auto instance = skyr::ipv4_address(0);
15-
CHECK("0.0.0.0" == instance.to_string());
15+
CHECK("0.0.0.0" == instance.serialize());
1616
}
1717

1818
SECTION("loopback_test") {
1919
auto instance = skyr::ipv4_address(0x7f000001);
20-
CHECK("127.0.0.1" == instance.to_string());
20+
CHECK("127.0.0.1" == instance.serialize());
2121
}
2222

2323
SECTION("address_test") {
2424
auto instance = skyr::ipv4_address(0x814ff5fc);
25-
CHECK("129.79.245.252" == instance.to_string());
25+
CHECK("129.79.245.252" == instance.serialize());
2626
}
2727

2828
SECTION("parse_zero_test") {

0 commit comments

Comments
 (0)