Skip to content

Commit

Permalink
AK: Return KString instead of String from encode_hex in the Kernel
Browse files Browse the repository at this point in the history
This let's us propagate allocation errors from this API.
  • Loading branch information
IdanHo authored and awesomekling committed Feb 16, 2022
1 parent d296001 commit 3219ce3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
12 changes: 12 additions & 0 deletions AK/Hex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -44,5 +55,6 @@ String encode_hex(const ReadonlyBytes input)

return output.build();
}
#endif

}
11 changes: 10 additions & 1 deletion AK/Hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

}

Expand Down
15 changes: 10 additions & 5 deletions AK/UUID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3219ce3

Please sign in to comment.