mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-26 20:55:35 +03:00
AK: Migrate IPv6Address::to_deprecated_string()
to ::to_string()
Change the name and return type of `IPv6Address::to_deprecated_string()` to `IPv6Address::to_string()` with return type `ErrorOr<String>`. It will now propagate errors that occur when writing to the StringBuilder. There are two users of `to_deprecated_string()` that now use `to_string()`: 1. `Formatted<IPv6Address>`: it now propagates errors. 2. `inet_ntop`: it now sets errno to ENOMEM and returns.
This commit is contained in:
parent
fdfffe2d8c
commit
49b29332f2
Notes:
sideshowbarker
2024-07-17 23:07:41 +09:00
Author: https://github.com/peterbb Commit: https://github.com/SerenityOS/serenity/commit/49b29332f2 Pull-request: https://github.com/SerenityOS/serenity/pull/19139 Reviewed-by: https://github.com/Dexesttp Reviewed-by: https://github.com/gmta
@ -16,7 +16,7 @@
|
||||
# include <AK/Error.h>
|
||||
# include <Kernel/Library/KString.h>
|
||||
#else
|
||||
# include <AK/DeprecatedString.h>
|
||||
# include <AK/String.h>
|
||||
#endif
|
||||
#include <AK/IPv4Address.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
@ -51,25 +51,24 @@ public:
|
||||
#ifdef KERNEL
|
||||
ErrorOr<NonnullOwnPtr<Kernel::KString>> to_string() const
|
||||
#else
|
||||
DeprecatedString to_deprecated_string() const
|
||||
ErrorOr<String> to_string() const
|
||||
#endif
|
||||
{
|
||||
if (is_zero()) {
|
||||
#ifdef KERNEL
|
||||
return Kernel::KString::try_create("::"sv);
|
||||
#else
|
||||
return "::"sv;
|
||||
return "::"_short_string;
|
||||
#endif
|
||||
}
|
||||
|
||||
// TODO: Error propagation
|
||||
StringBuilder builder;
|
||||
|
||||
if (is_ipv4_mapped()) {
|
||||
#ifdef KERNEL
|
||||
return Kernel::KString::formatted("::ffff:{}.{}.{}.{}", m_data[12], m_data[13], m_data[14], m_data[15]);
|
||||
#else
|
||||
return DeprecatedString::formatted("::ffff:{}.{}.{}.{}", m_data[12], m_data[13], m_data[14], m_data[15]);
|
||||
return String::formatted("::ffff:{}.{}.{}.{}", m_data[12], m_data[13], m_data[14], m_data[15]);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -99,24 +98,24 @@ public:
|
||||
for (int i = 0; i < 8;) {
|
||||
if (longest_zero_span_start.has_value() && longest_zero_span_start.value() == i) {
|
||||
if (longest_zero_span_start.value() + zero_span_length >= 8)
|
||||
builder.append("::"sv);
|
||||
TRY(builder.try_append("::"sv));
|
||||
else
|
||||
builder.append(':');
|
||||
TRY(builder.try_append(':'));
|
||||
i += zero_span_length;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i == 0)
|
||||
builder.appendff("{:x}", group(i));
|
||||
TRY(builder.try_appendff("{:x}", group(i)));
|
||||
else
|
||||
builder.appendff(":{:x}", group(i));
|
||||
TRY(builder.try_appendff(":{:x}", group(i)));
|
||||
|
||||
i++;
|
||||
}
|
||||
#ifdef KERNEL
|
||||
return Kernel::KString::try_create(builder.string_view());
|
||||
#else
|
||||
return builder.string_view();
|
||||
return builder.to_string();
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -298,7 +297,7 @@ template<>
|
||||
struct Formatter<IPv6Address> : Formatter<StringView> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, IPv6Address const& value)
|
||||
{
|
||||
return Formatter<StringView>::format(builder, value.to_deprecated_string());
|
||||
return Formatter<StringView>::format(builder, TRY(value.to_string()));
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
@ -55,14 +55,18 @@ TEST_CASE(should_get_groups_by_index)
|
||||
|
||||
TEST_CASE(should_convert_to_string)
|
||||
{
|
||||
EXPECT_EQ("102:304:506:708:90a:b0c:d0e:f10"sv, IPv6Address({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }).to_deprecated_string());
|
||||
EXPECT_EQ("::"sv, IPv6Address().to_deprecated_string());
|
||||
EXPECT_EQ("::1"sv, IPv6Address({ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }).to_deprecated_string());
|
||||
EXPECT_EQ("1::"sv, IPv6Address({ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }).to_deprecated_string());
|
||||
EXPECT_EQ("102:0:506:708:900::10"sv, IPv6Address({ 1, 2, 0, 0, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 16 }).to_deprecated_string());
|
||||
EXPECT_EQ("102:0:506:708:900::"sv, IPv6Address({ 1, 2, 0, 0, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 }).to_deprecated_string());
|
||||
EXPECT_EQ("::304:506:708:90a:b0c:d0e:f10"sv, IPv6Address({ 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }).to_deprecated_string());
|
||||
EXPECT_EQ("102:304::708:90a:b0c:d0e:f10"sv, IPv6Address({ 1, 2, 3, 4, 0, 0, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }).to_deprecated_string());
|
||||
auto to_string = [&](IPv6Address::in6_addr_t const& data) -> String {
|
||||
return MUST(IPv6Address(data).to_string());
|
||||
};
|
||||
|
||||
EXPECT_EQ("102:304:506:708:90a:b0c:d0e:f10"sv, to_string({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }));
|
||||
EXPECT_EQ("::"sv, MUST(IPv6Address().to_string()));
|
||||
EXPECT_EQ("::1"sv, to_string({ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }));
|
||||
EXPECT_EQ("1::"sv, to_string({ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }));
|
||||
EXPECT_EQ("102:0:506:708:900::10"sv, to_string({ 1, 2, 0, 0, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 16 }));
|
||||
EXPECT_EQ("102:0:506:708:900::"sv, to_string({ 1, 2, 0, 0, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 }));
|
||||
EXPECT_EQ("::304:506:708:90a:b0c:d0e:f10"sv, to_string({ 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }));
|
||||
EXPECT_EQ("102:304::708:90a:b0c:d0e:f10"sv, to_string({ 1, 2, 3, 4, 0, 0, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }));
|
||||
}
|
||||
|
||||
TEST_CASE(should_make_ipv6_address_from_string)
|
||||
@ -86,7 +90,7 @@ TEST_CASE(ipv4_mapped_ipv6)
|
||||
IPv6Address mapped_address(ipv4_address_to_map);
|
||||
EXPECT(mapped_address.is_ipv4_mapped());
|
||||
EXPECT_EQ(ipv4_address_to_map, mapped_address.ipv4_mapped_address().value());
|
||||
EXPECT_EQ("::ffff:192.168.0.1"sv, mapped_address.to_deprecated_string());
|
||||
EXPECT_EQ("::ffff:192.168.0.1"sv, MUST(mapped_address.to_string()));
|
||||
EXPECT_EQ(IPv4Address(192, 168, 1, 9), IPv6Address::from_string("::FFFF:192.168.1.9"sv).value().ipv4_mapped_address().value());
|
||||
EXPECT(!IPv6Address::from_string("::abcd:192.168.1.9"sv).has_value());
|
||||
}
|
||||
|
@ -27,8 +27,13 @@ char const* inet_ntop(int af, void const* src, char* dst, socklen_t len)
|
||||
errno = ENOSPC;
|
||||
return nullptr;
|
||||
}
|
||||
auto str = IPv6Address(((in6_addr const*)src)->s6_addr).to_deprecated_string();
|
||||
if (!str.copy_characters_to_buffer(dst, len)) {
|
||||
auto str_or_error = IPv6Address(((in6_addr const*)src)->s6_addr).to_string();
|
||||
if (str_or_error.is_error()) {
|
||||
errno = ENOMEM;
|
||||
return nullptr;
|
||||
}
|
||||
auto str = str_or_error.release_value();
|
||||
if (!str.bytes_as_string_view().copy_characters_to_buffer(dst, len)) {
|
||||
errno = ENOSPC;
|
||||
return nullptr;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user