diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2022-02-15 19:35:32 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-16 22:21:37 +0100 |
commit | 6098ffa1200e4b6c6802d35961db0ce0e69e0cc5 (patch) | |
tree | 075c37c42631150103483d3e777cbec0fcb55467 /AK/IPv4Address.h | |
parent | bc98ad9cc1452a3db574eca44b029c2dfca43605 (diff) | |
download | serenity-6098ffa1200e4b6c6802d35961db0ce0e69e0cc5.zip |
AK+Kernel: Return KString from IPv4Address::to_string() in the Kernel
This lets us safely handle allocation failure.
Diffstat (limited to 'AK/IPv4Address.h')
-rw-r--r-- | AK/IPv4Address.h | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/AK/IPv4Address.h b/AK/IPv4Address.h index 3df22ea34e..03a6f61cab 100644 --- a/AK/IPv4Address.h +++ b/AK/IPv4Address.h @@ -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 } |