diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/uuid.cpp | 54 | ||||
| -rw-r--r-- | src/common/uuid.h | 19 |
2 files changed, 73 insertions, 0 deletions
diff --git a/src/common/uuid.cpp b/src/common/uuid.cpp index 18303a1e3..d7435a6e9 100644 --- a/src/common/uuid.cpp +++ b/src/common/uuid.cpp | |||
| @@ -6,10 +6,64 @@ | |||
| 6 | 6 | ||
| 7 | #include <fmt/format.h> | 7 | #include <fmt/format.h> |
| 8 | 8 | ||
| 9 | #include "common/assert.h" | ||
| 9 | #include "common/uuid.h" | 10 | #include "common/uuid.h" |
| 10 | 11 | ||
| 11 | namespace Common { | 12 | namespace Common { |
| 12 | 13 | ||
| 14 | namespace { | ||
| 15 | |||
| 16 | bool IsHexDigit(char c) { | ||
| 17 | return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); | ||
| 18 | } | ||
| 19 | |||
| 20 | u8 HexCharToByte(char c) { | ||
| 21 | if (c >= '0' && c <= '9') { | ||
| 22 | return static_cast<u8>(c - '0'); | ||
| 23 | } | ||
| 24 | if (c >= 'a' && c <= 'f') { | ||
| 25 | return static_cast<u8>(c - 'a' + 10); | ||
| 26 | } | ||
| 27 | if (c >= 'A' && c <= 'F') { | ||
| 28 | return static_cast<u8>(c - 'A' + 10); | ||
| 29 | } | ||
| 30 | ASSERT_MSG(false, "{} is not a hexadecimal digit!", c); | ||
| 31 | return u8{0}; | ||
| 32 | } | ||
| 33 | |||
| 34 | } // Anonymous namespace | ||
| 35 | |||
| 36 | u128 HexStringToU128(std::string_view hex_string) { | ||
| 37 | const size_t length = hex_string.length(); | ||
| 38 | |||
| 39 | // Detect "0x" prefix. | ||
| 40 | const bool has_0x_prefix = length > 2 && hex_string[0] == '0' && hex_string[1] == 'x'; | ||
| 41 | const size_t offset = has_0x_prefix ? 2 : 0; | ||
| 42 | |||
| 43 | // Check length. | ||
| 44 | if (length > 32 + offset) { | ||
| 45 | ASSERT_MSG(false, "hex_string has more than 32 hexadecimal characters!"); | ||
| 46 | return INVALID_UUID; | ||
| 47 | } | ||
| 48 | |||
| 49 | u64 lo = 0; | ||
| 50 | u64 hi = 0; | ||
| 51 | for (size_t i = 0; i < length - offset; ++i) { | ||
| 52 | const char c = hex_string[length - 1 - i]; | ||
| 53 | if (!IsHexDigit(c)) { | ||
| 54 | ASSERT_MSG(false, "{} is not a hexadecimal digit!", c); | ||
| 55 | return INVALID_UUID; | ||
| 56 | } | ||
| 57 | if (i < 16) { | ||
| 58 | lo |= u64{HexCharToByte(c)} << (i * 4); | ||
| 59 | } | ||
| 60 | if (i >= 16) { | ||
| 61 | hi |= u64{HexCharToByte(c)} << ((i - 16) * 4); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | return u128{lo, hi}; | ||
| 65 | } | ||
| 66 | |||
| 13 | UUID UUID::Generate() { | 67 | UUID UUID::Generate() { |
| 14 | std::random_device device; | 68 | std::random_device device; |
| 15 | std::mt19937 gen(device()); | 69 | std::mt19937 gen(device()); |
diff --git a/src/common/uuid.h b/src/common/uuid.h index 0ffa37e7c..aeb36939a 100644 --- a/src/common/uuid.h +++ b/src/common/uuid.h | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <string> | 7 | #include <string> |
| 8 | #include <string_view> | ||
| 8 | 9 | ||
| 9 | #include "common/common_types.h" | 10 | #include "common/common_types.h" |
| 10 | 11 | ||
| @@ -12,12 +13,30 @@ namespace Common { | |||
| 12 | 13 | ||
| 13 | constexpr u128 INVALID_UUID{{0, 0}}; | 14 | constexpr u128 INVALID_UUID{{0, 0}}; |
| 14 | 15 | ||
| 16 | /** | ||
| 17 | * Converts a hex string to a 128-bit unsigned integer. | ||
| 18 | * | ||
| 19 | * The hex string can be formatted in lowercase or uppercase, with or without the "0x" prefix. | ||
| 20 | * | ||
| 21 | * This function will assert and return INVALID_UUID under the following conditions: | ||
| 22 | * - If the hex string is more than 32 characters long | ||
| 23 | * - If the hex string contains non-hexadecimal characters | ||
| 24 | * | ||
| 25 | * @param hex_string Hexadecimal string | ||
| 26 | * | ||
| 27 | * @returns A 128-bit unsigned integer if successfully converted, INVALID_UUID otherwise. | ||
| 28 | */ | ||
| 29 | [[nodiscard]] u128 HexStringToU128(std::string_view hex_string); | ||
| 30 | |||
| 15 | struct UUID { | 31 | struct UUID { |
| 16 | // UUIDs which are 0 are considered invalid! | 32 | // UUIDs which are 0 are considered invalid! |
| 17 | u128 uuid; | 33 | u128 uuid; |
| 18 | UUID() = default; | 34 | UUID() = default; |
| 19 | constexpr explicit UUID(const u128& id) : uuid{id} {} | 35 | constexpr explicit UUID(const u128& id) : uuid{id} {} |
| 20 | constexpr explicit UUID(const u64 lo, const u64 hi) : uuid{{lo, hi}} {} | 36 | constexpr explicit UUID(const u64 lo, const u64 hi) : uuid{{lo, hi}} {} |
| 37 | explicit UUID(std::string_view hex_string) { | ||
| 38 | uuid = HexStringToU128(hex_string); | ||
| 39 | } | ||
| 21 | 40 | ||
| 22 | [[nodiscard]] constexpr explicit operator bool() const { | 41 | [[nodiscard]] constexpr explicit operator bool() const { |
| 23 | return uuid != INVALID_UUID; | 42 | return uuid != INVALID_UUID; |