AK: Introduce IPAddressCidr datatype#25006
Conversation
kleinesfilmroellchen
left a comment
There was a problem hiding this comment.
(note to other reviewers: this is part of SIP6TF, so I was consulted on quite a bit of this code and may be biased)
Very nice work, and extensive tests, I like it. I'm not happy with the name though, so some bikeshedding may be required.
Hendiadyoin1
left a comment
There was a problem hiding this comment.
Mainly Nits on my end, that would likely need support from other places in the code base to resolve
| auto address_string = TRY(m_address.to_string()); | ||
|
|
||
| #ifdef KERNEL | ||
| TRY(builder.try_append(address_string->view())); | ||
| #else | ||
| TRY(builder.try_append(address_string)); | ||
| #endif | ||
|
|
||
| TRY(builder.try_append('/')); | ||
| TRY(builder.try_appendff("{}", m_length)); |
There was a problem hiding this comment.
Ideally this would just be a
#ifdef KERNEL
return KSting::formatted("{}/{}", m_address, m_length());
#else
return Sting::try_formatted("{}/{}", m_address, m_length());
#endifbut we're likely missing a custom formatter, are we?
Note that fallibility in the Userland case is not enforced (anymore)
There was a problem hiding this comment.
Just doing
#ifdef KERNEL
ErrorOr<NonnullOwnPtr<Kernel::KString>> to_string() const
{
return KString::formatted("{}/{}", m_address, m_length);
}
#else
ErrorOr<String> to_string() const
{
return String::formatted("{}/{}", m_address, m_length);
}
#endifworks is this case both for IPv4 and IPv6 in hosts and serenity tests. I'll adopt this for formatting.
| template<> | ||
| struct Formatter<IPv6AddressCidr> : Formatter<StringView> { | ||
| ErrorOr<void> format(FormatBuilder& builder, IPv6AddressCidr value) | ||
| { | ||
| return Formatter<StringView>::format(builder, TRY(value.to_string())->view()); | ||
| } | ||
| }; | ||
| #else | ||
| template<> | ||
| struct Formatter<IPv6AddressCidr> : Formatter<StringView> { | ||
| ErrorOr<void> format(FormatBuilder& builder, IPv6AddressCidr value) | ||
| { | ||
| return Formatter<StringView>::format(builder, TRY(value.to_string())); | ||
| } | ||
| }; |
There was a problem hiding this comment.
What I meant with this, is that this could mirror the actual implementation of the CIDR to_string method,
maybe even replace it.
With some template shenanigans (Ie DerivedFrom<IpAddressCIDR>) all formatters could use the same code
| template<> | ||
| struct Formatter<IPv4AddressCidr> : Formatter<StringView> { | ||
| ErrorOr<void> format(FormatBuilder& builder, IPv4AddressCidr value) | ||
| { | ||
| return Formatter<StringView>::format(builder, TRY(value.to_string())->view()); | ||
| } | ||
| }; | ||
| #else | ||
| template<> | ||
| struct Formatter<IPv4AddressCidr> : Formatter<StringView> { | ||
| ErrorOr<void> format(FormatBuilder& builder, IPv4AddressCidr value) | ||
| { | ||
| return Formatter<StringView>::format(builder, TRY(value.to_string())); | ||
| } | ||
| }; |
|
Hello! One or more of the commit messages in this PR do not match the SerenityOS code submission policy, please check the |
|
I will squash this again later when the code itself is fine |
28ead4b to
d1b9e2c
Compare
kleinesfilmroellchen
left a comment
There was a problem hiding this comment.
Only Leon's comment about string formatting needs to be resolved, I think we want to switch the implementation place and move the implementation into the Formatter, while to_string only calls String::formatted.
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions! |
|
I will pick this up again eventually, currently a bit short on time unfortunately. |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions! |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions! |
|
This pull request has been closed because it has not had recent activity. Feel free to re-open if you wish to still contribute these changes. Thank you for your contributions! |
This commit introduces a CIDR datatype which will become handy especially for IPv6 to not have to deal with netmasks.