Revamp uuid code

This commit is contained in:
2026-02-15 14:55:01 -06:00
parent 61275c0f5f
commit 9dae77a0b9
8 changed files with 84 additions and 87 deletions

View File

@@ -157,7 +157,7 @@ namespace Tesses::Framework::Serialization
b2[0] = (uint8_t)v;
b2[1] = (uint8_t)(v >> 8);
}
void BitConverter::FromUuidBE(uint8_t& b, const Uuid& uuid)
void BitConverter::FromUuid(uint8_t& b, const Uuid& uuid)
{
uint8_t* b2 = &b;
FromUint32BE(b2[0], uuid.time_low);
@@ -169,35 +169,17 @@ namespace Tesses::Framework::Serialization
b2[i+10] = uuid.node[i];
}
void BitConverter::FromUuidMS(uint8_t& b, const Uuid& uuid)
{
uint8_t* b2 = &b;
FromUint32LE(b2[0], uuid.time_low);
FromUint32LE(b2[4], uuid.time_mid);
FromUint32LE(b2[6], uuid.time_hi_and_version);
b2[8] = uuid.clock_seq_hi_and_reserved;
b2[9] = uuid.clock_seq_low;
for(size_t i = 0; i < 6; i++)
b2[i+10] = uuid.node[i];
}
Uuid BitConverter::ToUuidBE(uint8_t& b)
Uuid BitConverter::ToUuid(uint8_t& b)
{
Uuid uuid;
BitConverter::ToUuidBE(b,uuid);
return uuid;
}
Uuid BitConverter::ToUuidMS(uint8_t& b)
{
Uuid uuid;
BitConverter::ToUuidMS(b,uuid);
BitConverter::ToUuid(b,uuid);
return uuid;
}
void BitConverter::ToUuidBE(uint8_t& b, Uuid& uuid)
void BitConverter::ToUuid(uint8_t& b, Uuid& uuid)
{
uint8_t* b2 = &b;
uuid.time_low = ToUint32BE(b2[0]);
@@ -213,16 +195,5 @@ namespace Tesses::Framework::Serialization
}
void BitConverter::ToUuidMS(uint8_t& b, Uuid& uuid)
{
uint8_t* b2 = &b;
uuid.time_low = ToUint32LE(b2[0]);
uuid.time_mid = ToUint16LE(b2[4]);
uuid.time_hi_and_version = ToUint16LE(b2[6]);
uuid.clock_seq_hi_and_reserved = b2[8];
uuid.clock_seq_low = b2[9];
for(size_t i = 0; i < 6; i++)
uuid.node[i]= b2[i+10];
}
};

View File

@@ -151,32 +151,19 @@ namespace Tesses::Framework::Streams
return *(double*)&v;
}
Uuid ByteReader::ReadUuidBE()
Uuid ByteReader::ReadUuid()
{
uint8_t data[16];
if(this->strm->ReadBlock(data, 16) != 16)
throw std::runtime_error("End of file");
return Serialization::BitConverter::ToUuidBE(data[0]);
return Serialization::BitConverter::ToUuid(data[0]);
}
Uuid ByteReader::ReadUuidMS()
void ByteReader::ReadUuid(Uuid& uuid)
{
uint8_t data[16];
if(this->strm->ReadBlock(data, 16) != 16)
throw std::runtime_error("End of file");
return Serialization::BitConverter::ToUuidMS(data[0]);
}
void ByteReader::ReadUuidBE(Uuid& uuid)
{
uint8_t data[16];
if(this->strm->ReadBlock(data, 16) != 16)
throw std::runtime_error("End of file");
Serialization::BitConverter::ToUuidBE(data[0],uuid);
}
void ByteReader::ReadUuidMS(Uuid& uuid)
{
uint8_t data[16];
if(this->strm->ReadBlock(data, 16) != 16)
throw std::runtime_error("End of file");
Serialization::BitConverter::ToUuidMS(data[0],uuid);
Serialization::BitConverter::ToUuid(data[0],uuid);
}
}

View File

@@ -130,16 +130,11 @@ namespace Tesses::Framework::Streams
uint64_t data = *(uint64_t*)&v;
WriteU64LE(data);
}
void ByteWriter::WriteUuidBE(const Uuid& uuid)
void ByteWriter::WriteUuid(const Uuid& uuid)
{
uint8_t data[16];
Serialization::BitConverter::FromUuidBE(data[0], uuid);
this->strm->WriteBlock(data, 16);
}
void ByteWriter::WriteUuidMS(const Uuid& uuid)
{
uint8_t data[16];
Serialization::BitConverter::FromUuidMS(data[0], uuid);
Serialization::BitConverter::FromUuid(data[0], uuid);
this->strm->WriteBlock(data, 16);
}
}

View File

@@ -61,39 +61,39 @@ namespace Tesses::Framework {
}
uint8_t b = hex_digits[0] >> 4 | hex_digits[1];
uint8_t b = hex_digits[0] << 4 | hex_digits[1];
uuid.time_low = (uint32_t)b;
b = hex_digits[2] >> 4 | hex_digits[3];
b = hex_digits[2] << 4 | hex_digits[3];
uuid.time_low |= (uint32_t)b << 8;
b = hex_digits[4] >> 4 | hex_digits[5];
b = hex_digits[4] << 4 | hex_digits[5];
uuid.time_low |= (uint32_t)b << 16;
b = hex_digits[6] >> 4 | hex_digits[7];
b = hex_digits[6] << 4 | hex_digits[7];
uuid.time_low |= (uint32_t)b << 24;
b = hex_digits[8] >> 4 | hex_digits[9];
b = hex_digits[8] << 4 | hex_digits[9];
uuid.time_mid = (uint16_t)b;
b = hex_digits[10] >> 4 | hex_digits[11];
b = hex_digits[10] << 4 | hex_digits[11];
uuid.time_mid |= (uint16_t)b << 8;
b = hex_digits[12] >> 4 | hex_digits[13];
b = hex_digits[12] << 4 | hex_digits[13];
uuid.time_hi_and_version = (uint16_t)b;
b = hex_digits[14] >> 4 | hex_digits[15];
b = hex_digits[14] << 4 | hex_digits[15];
uuid.time_hi_and_version |= (uint16_t)b << 8;
uuid.clock_seq_hi_and_reserved = hex_digits[16] >> 4 | hex_digits[17];
uuid.clock_seq_hi_and_reserved = hex_digits[16] << 4 | hex_digits[17];
uuid.clock_seq_low = hex_digits[18] >> 4 | hex_digits[19];
uuid.clock_seq_low = hex_digits[18] << 4 | hex_digits[19];
for(size_t i = 0; i < 6; i++)
{
uuid.node[i] = hex_digits[20+(i*2)] >> 4 | hex_digits[21+(i*2)];
uuid.node[i] = hex_digits[20+(i*2)] << 4 | hex_digits[21+(i*2)];
}
return true;
}
//9c4994e7-3c82-4c30-a459-8fdcd960b4ac
std::string Uuid::ToString(UuidStringifyConfig cfg)
std::string Uuid::ToString(UuidStringifyConfig cfg) const
{
bool hasCurly = ((int)cfg & (int)UuidStringifyConfig::HasCurly) != 0;
bool isUppercase = ((int)cfg & (int)UuidStringifyConfig::IsUppercase) != 0;
@@ -166,4 +166,49 @@ namespace Tesses::Framework {
}
bool Uuid::IsEmpty() const
{
return this->time_low == 0 &&
this->time_mid == 0 &&
this->time_hi_and_version == 0 &&
this->clock_seq_hi_and_reserved == 0 &&
this->clock_seq_low == 0 &&
this->node[0] == 0 &&
this->node[1] == 0 &&
this->node[2] == 0 &&
this->node[3] == 0 &&
this->node[4] == 0 &&
this->node[5] == 0;
}
bool operator==(const Uuid& left, const Uuid& right)
{
return left.time_low == right.time_low &&
left.time_mid == right.time_mid &&
left.time_hi_and_version == right.time_hi_and_version &&
left.clock_seq_hi_and_reserved == right.clock_seq_hi_and_reserved &&
left.clock_seq_low == right.clock_seq_low &&
left.node[0] == right.node[0] &&
left.node[1] == right.node[1] &&
left.node[2] == right.node[2] &&
left.node[3] == right.node[3] &&
left.node[4] == right.node[4] &&
left.node[5] == right.node[5];
}
bool operator!=(const Uuid& left, const Uuid& right)
{
return left.time_low != right.time_low &&
left.time_mid != right.time_mid &&
left.time_hi_and_version != right.time_hi_and_version &&
left.clock_seq_hi_and_reserved != right.clock_seq_hi_and_reserved &&
left.clock_seq_low != right.clock_seq_low &&
left.node[0] != right.node[0] &&
left.node[1] != right.node[1] &&
left.node[2] != right.node[2] &&
left.node[3] != right.node[3] &&
left.node[4] != right.node[4] &&
left.node[5] != right.node[5];
}
}