Core: Make Uuid constexpr

This commit is contained in:
SirLynix
2022-12-29 12:13:18 +01:00
parent 9a553e5e9d
commit 763bef3fdd
3 changed files with 103 additions and 93 deletions

View File

@@ -15,28 +15,6 @@
namespace Nz
{
namespace
{
bool ParseHexadecimalPair(Pointer<const char>& str, UInt8& number)
{
number = 0;
for (UInt8 mul : { UInt8(0x10), UInt8(1) })
{
if (*str >= '0' && *str <= '9')
number += (*str - '0') * mul;
else if (((*str & 0x5F) >= 'A' && (*str & 0x5F) <= 'F'))
number += ((*str & 0x5F) - 'A' + 10) * mul;
else
return false;
str++;
}
return true;
}
}
std::array<char, 37> Uuid::ToStringArray() const
{
std::array<char, 37> uuidStr; //< Including \0
@@ -47,34 +25,6 @@ namespace Nz
return uuidStr;
}
Uuid Uuid::FromString(std::string_view str)
{
if (str.size() != 36)
return {};
const char* ptr = str.data();
std::array<UInt8, 16> uuid;
UInt8* uuidPart = &uuid[0];
bool first = true;
for (std::size_t groupSize : { 4, 2, 2, 2, 6 })
{
if (!first && *ptr++ != '-')
return {};
first = false;
for (std::size_t i = 0; i < groupSize; ++i)
{
if (!ParseHexadecimalPair(ptr, *uuidPart++))
return {};
}
}
return Uuid{ uuid };
}
Uuid Uuid::Generate()
{
std::array<UInt8, 16> uuid;
@@ -105,11 +55,4 @@ namespace Nz
return uuid;
}
std::ostream& operator<<(std::ostream& out, const Uuid& guid)
{
std::array<char, 37> uuidStr = guid.ToStringArray();
return out << uuidStr.data();
}
}