mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 08:17:35 +00:00
AK+Kernel: Return KString from IPv4Address::to_string() in the Kernel
This lets us safely handle allocation failure.
This commit is contained in:
parent
bc98ad9cc1
commit
6098ffa120
2 changed files with 47 additions and 10 deletions
|
@ -7,11 +7,18 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Endian.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
#ifdef KERNEL
|
||||
# include <AK/Error.h>
|
||||
# include <Kernel/KString.h>
|
||||
#else
|
||||
# include <AK/String.h>
|
||||
#endif
|
||||
|
||||
namespace AK {
|
||||
|
||||
class [[gnu::packed]] IPv4Address {
|
||||
|
@ -48,6 +55,16 @@ public:
|
|||
return octet(SubnetClass(i));
|
||||
}
|
||||
|
||||
#ifdef KERNEL
|
||||
ErrorOr<NonnullOwnPtr<Kernel::KString>> to_string() const
|
||||
{
|
||||
return Kernel::KString::formatted("{}.{}.{}.{}",
|
||||
octet(SubnetClass::A),
|
||||
octet(SubnetClass::B),
|
||||
octet(SubnetClass::C),
|
||||
octet(SubnetClass::D));
|
||||
}
|
||||
#else
|
||||
String to_string() const
|
||||
{
|
||||
return String::formatted("{}.{}.{}.{}",
|
||||
|
@ -65,6 +82,7 @@ public:
|
|||
octet(SubnetClass::B),
|
||||
octet(SubnetClass::A));
|
||||
}
|
||||
#endif
|
||||
|
||||
static Optional<IPv4Address> from_string(StringView string)
|
||||
{
|
||||
|
@ -131,6 +149,15 @@ struct Traits<IPv4Address> : public GenericTraits<IPv4Address> {
|
|||
static constexpr unsigned hash(const IPv4Address& address) { return int_hash(address.to_u32()); }
|
||||
};
|
||||
|
||||
#ifdef KERNEL
|
||||
template<>
|
||||
struct Formatter<IPv4Address> : Formatter<ErrorOr<NonnullOwnPtr<Kernel::KString>>> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, IPv4Address value)
|
||||
{
|
||||
return Formatter<ErrorOr<NonnullOwnPtr<Kernel::KString>>>::format(builder, value.to_string());
|
||||
}
|
||||
};
|
||||
#else
|
||||
template<>
|
||||
struct Formatter<IPv4Address> : Formatter<String> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, IPv4Address value)
|
||||
|
@ -138,6 +165,7 @@ struct Formatter<IPv4Address> : Formatter<String> {
|
|||
return Formatter<String>::format(builder, value.to_string());
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue