AK: Return KString instead of String from encode_hex in the Kernel
This let's us propagate allocation errors from this API.
This commit is contained in:
parent
d296001f3f
commit
3219ce3d61
Notes:
sideshowbarker
2024-07-17 18:40:29 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/3219ce3d61 Pull-request: https://github.com/SerenityOS/serenity/pull/12564 Reviewed-by: https://github.com/MaxWipfli
3 changed files with 32 additions and 6 deletions
12
AK/Hex.cpp
12
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
|
||||
|
||||
}
|
||||
|
|
11
AK/Hex.h
11
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
|
||||
|
||||
}
|
||||
|
||||
|
|
15
AK/UUID.cpp
15
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
|
||||
|
|
Loading…
Add table
Reference in a new issue