summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2022-02-15 23:25:25 +0200
committerAndreas Kling <kling@serenityos.org>2022-02-16 22:21:37 +0100
commit3219ce3d61f42dfcbeb0483a24539cd7b8f4723b (patch)
treedd7f9f213b42da8480630ea41f3125aae85dc6ef
parentd296001f3fea970956ad253a4db26ef3f2ac5210 (diff)
downloadserenity-3219ce3d61f42dfcbeb0483a24539cd7b8f4723b.zip
AK: Return KString instead of String from encode_hex in the Kernel
This let's us propagate allocation errors from this API.
-rw-r--r--AK/Hex.cpp12
-rw-r--r--AK/Hex.h11
-rw-r--r--AK/UUID.cpp15
3 files changed, 32 insertions, 6 deletions
diff --git a/AK/Hex.cpp b/AK/Hex.cpp
index ca35477968..dfd0d410ef 100644
--- a/AK/Hex.cpp
+++ b/AK/Hex.cpp
@@ -35,6 +35,17 @@ ErrorOr<ByteBuffer> decode_hex(StringView input)
return { move(output) };
}
+#ifdef KERNEL
+ErrorOr<NonnullOwnPtr<Kernel::KString>> encode_hex(const ReadonlyBytes input)
+{
+ StringBuilder output(input.size() * 2);
+
+ for (auto ch : input)
+ TRY(output.try_appendff("{:02x}", ch));
+
+ return Kernel::KString::try_create(output.string_view());
+}
+#else
String encode_hex(const ReadonlyBytes input)
{
StringBuilder output(input.size() * 2);
@@ -44,5 +55,6 @@ String encode_hex(const ReadonlyBytes input)
return output.build();
}
+#endif
}
diff --git a/AK/Hex.h b/AK/Hex.h
index ba21c3a761..b421e9ac25 100644
--- a/AK/Hex.h
+++ b/AK/Hex.h
@@ -8,9 +8,14 @@
#include <AK/ByteBuffer.h>
#include <AK/Error.h>
-#include <AK/String.h>
#include <AK/StringView.h>
+#ifdef KERNEL
+# include <Kernel/KString.h>
+#else
+# include <AK/String.h>
+#endif
+
namespace AK {
constexpr u8 decode_hex_digit(char digit)
@@ -26,7 +31,11 @@ constexpr u8 decode_hex_digit(char digit)
ErrorOr<ByteBuffer> decode_hex(StringView);
+#ifdef KERNEL
+ErrorOr<NonnullOwnPtr<Kernel::KString>> encode_hex(ReadonlyBytes);
+#else
String encode_hex(ReadonlyBytes);
+#endif
}
diff --git a/AK/UUID.cpp b/AK/UUID.cpp
index 8353c49c1e..c04f10cd88 100644
--- a/AK/UUID.cpp
+++ b/AK/UUID.cpp
@@ -80,15 +80,20 @@ UUID::UUID(StringView uuid_string_view, Endianness endianness)
ErrorOr<NonnullOwnPtr<Kernel::KString>> UUID::to_string() const
{
StringBuilder builder(36);
- TRY(builder.try_append(encode_hex(m_uuid_buffer.span().trim(4)).view()));
+ auto nibble0 = TRY(encode_hex(m_uuid_buffer.span().trim(4)));
+ TRY(builder.try_append(nibble0->view()));
TRY(builder.try_append('-'));
- TRY(builder.try_append(encode_hex(m_uuid_buffer.span().slice(4).trim(2)).view()));
+ auto nibble1 = TRY(encode_hex(m_uuid_buffer.span().slice(4).trim(2)));
+ TRY(builder.try_append(nibble1->view()));
TRY(builder.try_append('-'));
- TRY(builder.try_append(encode_hex(m_uuid_buffer.span().slice(6).trim(2)).view()));
+ auto nibble2 = TRY(encode_hex(m_uuid_buffer.span().slice(6).trim(2)));
+ TRY(builder.try_append(nibble2->view()));
TRY(builder.try_append('-'));
- TRY(builder.try_append(encode_hex(m_uuid_buffer.span().slice(8).trim(2)).view()));
+ auto nibble3 = TRY(encode_hex(m_uuid_buffer.span().slice(8).trim(2)));
+ TRY(builder.try_append(nibble3->view()));
TRY(builder.try_append('-'));
- TRY(builder.try_append(encode_hex(m_uuid_buffer.span().slice(10).trim(6)).view()));
+ auto nibble4 = TRY(encode_hex(m_uuid_buffer.span().slice(10).trim(6)));
+ TRY(builder.try_append(nibble4->view()));
return Kernel::KString::try_create(builder.string_view());
}
#else