1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:54:58 +00:00

AK+Kernel: Return KString from UUID::to_string() in the Kernel

This lets us safely handle allocation failure.
This commit is contained in:
Idan Horowitz 2022-02-15 20:55:53 +02:00 committed by Andreas Kling
parent 5a5766be2c
commit 7f44e54ad6
2 changed files with 27 additions and 0 deletions

View file

@ -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
{