-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AK: Introduce IPAddressCidr datatype #25006
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mainly Nits on my end, that would likely need support from other places in the code base to resolve
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer CIDR
AK/IpAddressCidr.h
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally this would just be a
#ifdef KERNEL
return KSting::formatted("{}/{}", m_address, m_length());
#else
return Sting::try_formatted("{}/{}", m_address, m_length());
#endif
but 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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);
}
#endif
works 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the OG commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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())); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See OG commit
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 commit introduces a CIDR datatype which will become handy especially for IPv6 to not have to deal with netmasks.