summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2022-02-15 20:55:53 +0200
committerAndreas Kling <kling@serenityos.org>2022-02-16 22:21:37 +0100
commit7f44e54ad6b898923f3b602e311c3dac404533ad (patch)
treea432d150925e7538e82220cde59a8ad3de3a4a0b
parent5a5766be2c2edb7d130d650a0c3e8707b9d57410 (diff)
downloadserenity-7f44e54ad6b898923f3b602e311c3dac404533ad.zip
AK+Kernel: Return KString from UUID::to_string() in the Kernel
This lets us safely handle allocation failure.
-rw-r--r--AK/UUID.cpp17
-rw-r--r--AK/UUID.h10
2 files changed, 27 insertions, 0 deletions
diff --git a/AK/UUID.cpp b/AK/UUID.cpp
index ac76da1464..8353c49c1e 100644
--- a/AK/UUID.cpp
+++ b/AK/UUID.cpp
@@ -76,6 +76,22 @@ UUID::UUID(StringView uuid_string_view, Endianness endianness)
VERIFY_NOT_REACHED();
}
+#ifdef KERNEL
+ErrorOr<NonnullOwnPtr<Kernel::KString>> UUID::to_string() const
+{
+ StringBuilder builder(36);
+ TRY(builder.try_append(encode_hex(m_uuid_buffer.span().trim(4)).view()));
+ TRY(builder.try_append('-'));
+ TRY(builder.try_append(encode_hex(m_uuid_buffer.span().slice(4).trim(2)).view()));
+ TRY(builder.try_append('-'));
+ TRY(builder.try_append(encode_hex(m_uuid_buffer.span().slice(6).trim(2)).view()));
+ TRY(builder.try_append('-'));
+ TRY(builder.try_append(encode_hex(m_uuid_buffer.span().slice(8).trim(2)).view()));
+ TRY(builder.try_append('-'));
+ TRY(builder.try_append(encode_hex(m_uuid_buffer.span().slice(10).trim(6)).view()));
+ return Kernel::KString::try_create(builder.string_view());
+}
+#else
String UUID::to_string() const
{
StringBuilder builder(36);
@@ -90,6 +106,7 @@ String UUID::to_string() const
builder.append(encode_hex(m_uuid_buffer.span().slice(10).trim(6)).view());
return builder.to_string();
}
+#endif
bool UUID::operator==(const UUID& other) const
{
diff --git a/AK/UUID.h b/AK/UUID.h
index 066d301091..c859cf3729 100644
--- a/AK/UUID.h
+++ b/AK/UUID.h
@@ -11,6 +11,12 @@
#include <AK/StringView.h>
#include <AK/Types.h>
+#ifdef KERNEL
+# include <Kernel/KString.h>
+#else
+# include <AK/String.h>
+#endif
+
namespace AK {
class UUID {
@@ -32,7 +38,11 @@ public:
bool operator<(const UUID&) const = delete;
bool operator>(const UUID&) const = delete;
+#ifdef KERNEL
+ ErrorOr<NonnullOwnPtr<Kernel::KString>> to_string() const;
+#else
String to_string() const;
+#endif
bool is_zero() const;
private: