libs/base/src/base/guid.cc
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "base/guid.h" | ||
| 2 | |||
| 3 | |||
| 4 | #ifdef _WIN32 | ||
| 5 | #include <objbase.h> | ||
| 6 | #else | ||
| 7 | #include <uuid/uuid.h> | ||
| 8 | #endif | ||
| 9 | |||
| 10 | |||
| 11 | namespace eu | ||
| 12 | { | ||
| 13 | #ifndef _WIN32 | ||
| 14 | 1 | std::optional<Guid> Guid::generate() | |
| 15 | { | ||
| 16 | 1 | Guid guid {}; | |
| 17 | 1 | uuid_generate(guid.data.data()); | |
| 18 | 2 | return guid; | |
| 19 | } | ||
| 20 | #else | ||
| 21 | std::optional<Guid> Guid::generate() | ||
| 22 | { | ||
| 23 | GUID g; | ||
| 24 | |||
| 25 | if (S_OK != CoCreateGuid(&g)) | ||
| 26 | { | ||
| 27 | return std::nullopt; | ||
| 28 | } | ||
| 29 | |||
| 30 | std::array<uint8_t, 16> data | ||
| 31 | { | ||
| 32 | static_cast<std::uint8_t>((g.Data1 >> 24) & 0xff), | ||
| 33 | static_cast<std::uint8_t>((g.Data1 >> 16) & 0xff), | ||
| 34 | static_cast<std::uint8_t>((g.Data1 >> 8) & 0xff), | ||
| 35 | static_cast<std::uint8_t>((g.Data1 ) & 0xff), | ||
| 36 | static_cast<std::uint8_t>((g.Data2 >> 8 ) & 0xff), | ||
| 37 | static_cast<std::uint8_t>((g.Data2 ) & 0xff), | ||
| 38 | static_cast<std::uint8_t>((g.Data3 >> 8 ) & 0xff), | ||
| 39 | static_cast<std::uint8_t>((g.Data3 ) & 0xff), | ||
| 40 | static_cast<std::uint8_t>( g.Data4[0] ), | ||
| 41 | static_cast<std::uint8_t>( g.Data4[1] ), | ||
| 42 | static_cast<std::uint8_t>( g.Data4[2] ), | ||
| 43 | static_cast<std::uint8_t>( g.Data4[3] ), | ||
| 44 | static_cast<std::uint8_t>( g.Data4[4] ), | ||
| 45 | static_cast<std::uint8_t>( g.Data4[5] ), | ||
| 46 | static_cast<std::uint8_t>( g.Data4[6] ), | ||
| 47 | static_cast<std::uint8_t>( g.Data4[7] ) | ||
| 48 | }; | ||
| 49 | |||
| 50 | return Guid{data}; | ||
| 51 | } | ||
| 52 | #endif | ||
| 53 | |||
| 54 | 8 | bool operator==(const Guid& lhs, const Guid& rhs) | |
| 55 | { | ||
| 56 | 120 | for(int data_index=0; data_index<16; data_index+=1) | |
| 57 | { | ||
| 58 | 113 | if (lhs.data[data_index] != rhs.data[data_index]) | |
| 59 | 1 | { return false; } | |
| 60 | } | ||
| 61 | 7 | return true; | |
| 62 | } | ||
| 63 | |||
| 64 | 1 | bool operator!=(const Guid& lhs, const Guid& rhs) | |
| 65 | { | ||
| 66 | 1 | return !(lhs == rhs); | |
| 67 | } | ||
| 68 | |||
| 69 | struct Parser | ||
| 70 | { | ||
| 71 | std::string_view str; | ||
| 72 | |||
| 73 | 8 | explicit Parser(const std::string_view s) : str(s) {} | |
| 74 | |||
| 75 | 912 | [[nodiscard]] bool has_more() const | |
| 76 | { | ||
| 77 | 912 | return str.empty() == false; | |
| 78 | } | ||
| 79 | |||
| 80 | 277 | char read() | |
| 81 | { | ||
| 82 | 277 | if(has_more() == false) | |
| 83 | ✗ | { return '\0'; } | |
| 84 | 277 | const char r = str[0]; | |
| 85 | 277 | str = str.substr(1); | |
| 86 | 277 | return r; | |
| 87 | } | ||
| 88 | |||
| 89 | 522 | [[nodiscard]] char peek() const | |
| 90 | { | ||
| 91 | 522 | if(has_more() == false) | |
| 92 | 12 | { return '\0'; } | |
| 93 | 510 | return str[0]; | |
| 94 | } | ||
| 95 | |||
| 96 | 357 | void skip(char c) | |
| 97 | { | ||
| 98 | 411 | while(peek() == c) | |
| 99 | { | ||
| 100 | 54 | read(); | |
| 101 | } | ||
| 102 | 357 | } | |
| 103 | |||
| 104 | 238 | void skip_spaces() | |
| 105 | { | ||
| 106 | 238 | skip(' '); | |
| 107 | 238 | } | |
| 108 | |||
| 109 | 223 | static std::optional<uint8_t> as_hex(char c) | |
| 110 | { | ||
| 111 | 223 | if ('0' <= c && c <= '9') { return static_cast<uint8_t>(c - '0'); } | |
| 112 | 36 | if ('a' <= c && c <= 'f') { return static_cast<uint8_t>(10 + (c - 'a')); } | |
| 113 | 11 | if ('A' <= c && c <= 'F') { return static_cast<uint8_t>(10 + (c - 'A')); } | |
| 114 | 1 | return std::nullopt; | |
| 115 | } | ||
| 116 | |||
| 117 | 112 | std::optional<uint8_t> read_hex() | |
| 118 | { | ||
| 119 | 112 | const auto first = as_hex(read()); | |
| 120 | 112 | if(first.has_value() == false) { return std::nullopt; } | |
| 121 | |||
| 122 | 111 | const auto second = as_hex(peek()); | |
| 123 | 111 | if(second.has_value()) | |
| 124 | { | ||
| 125 | 111 | read(); | |
| 126 | 111 | return static_cast<uint8_t>((*first << 4) | *second); | |
| 127 | } | ||
| 128 | else | ||
| 129 | { | ||
| 130 | ✗ | return first; | |
| 131 | } | ||
| 132 | } | ||
| 133 | }; | ||
| 134 | |||
| 135 | 8 | std::optional<Guid> guid_from(const std::string& str) | |
| 136 | { | ||
| 137 | 8 | auto parser = Parser{str}; | |
| 138 | 8 | parser.skip_spaces(); | |
| 139 | 8 | parser.skip('{'); | |
| 140 | 8 | parser.skip_spaces(); | |
| 141 | |||
| 142 | 8 | Guid ret {}; | |
| 143 | 119 | for(int index=0; index<16; index+=1) | |
| 144 | { | ||
| 145 | 114 | if(parser.has_more() == false) { return std::nullopt; } | |
| 146 | |||
| 147 | 112 | auto hex = parser.read_hex(); | |
| 148 | 112 | if(hex.has_value() == false) { return std::nullopt; } | |
| 149 | |||
| 150 | 111 | ret.data[index] = *hex; | |
| 151 | 111 | parser.skip_spaces(); | |
| 152 | 111 | parser.skip('-'); | |
| 153 | 111 | parser.skip_spaces(); | |
| 154 | } | ||
| 155 | |||
| 156 | 6 | return ret; | |
| 157 | } | ||
| 158 | |||
| 159 | 13 | std::string string_from(const Guid& x) | |
| 160 | { | ||
| 161 | return fmt::format | ||
| 162 | ( | ||
| 163 | "{:02x}{:02x}{:02x}{:02x}-" | ||
| 164 | "{:02x}{:02x}-" | ||
| 165 | "{:02x}{:02x}-" | ||
| 166 | "{:02x}{:02x}-" | ||
| 167 | "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", | ||
| 168 | 13 | x.data[0], x.data[1], x.data[2], x.data[3], | |
| 169 | 13 | x.data[4], x.data[5], | |
| 170 | 13 | x.data[6], x.data[7], | |
| 171 | 13 | x.data[8], x.data[9], | |
| 172 | 13 | x.data[10], x.data[11], x.data[12], x.data[13], x.data[14], x.data[15] | |
| 173 | 26 | ); | |
| 174 | } | ||
| 175 | |||
| 176 | 2 | ADD_CATCH_FORMATTER_IMPL(Guid) | |
| 177 | } | ||
| 178 |