diff options
| author | 2021-08-04 03:55:48 -0400 | |
|---|---|---|
| committer | 2021-08-04 10:45:41 -0400 | |
| commit | 705f1110587502a49dda51790fa14ac9843e135e (patch) | |
| tree | e7ff76e7ae29ee77bc734fe63d5cbd510dcd69f9 /src/common/uuid.cpp | |
| parent | nvdec: Implement VA-API hardware video acceleration (#6713) (diff) | |
| download | yuzu-705f1110587502a49dda51790fa14ac9843e135e.tar.gz yuzu-705f1110587502a49dda51790fa14ac9843e135e.tar.xz yuzu-705f1110587502a49dda51790fa14ac9843e135e.zip | |
common: uuid: Add hex string to UUID constructor
This allows for easily converting a hex string into a Common::UUID, which is backed by a 128 bit unsigned integer.
Diffstat (limited to '')
| -rw-r--r-- | src/common/uuid.cpp | 54 |
1 files changed, 54 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()); |