|
15 | 15 | //
|
16 | 16 | //===----------------------------------------------------------------------===//
|
17 | 17 |
|
18 |
| -#include <uuid/uuid.h> |
19 | 18 | #include "swift/Basic/UUID.h"
|
20 | 19 |
|
| 20 | +// WIN32 doesn't natively support <uuid/uuid.h>. Instead, we use Win32 APIs. |
| 21 | +#if defined(_WIN32) |
| 22 | +#define WIN32_LEAN_AND_MEAN |
| 23 | +#include <rpc.h> |
| 24 | +#include <string> |
| 25 | +#else |
| 26 | +#include <uuid/uuid.h> |
| 27 | +#endif |
| 28 | + |
21 | 29 | using namespace swift;
|
22 | 30 |
|
23 |
| -UUID::UUID(FromRandom_t) { |
| 31 | +swift::UUID::UUID(FromRandom_t) { |
| 32 | +#if defined(_WIN32) |
| 33 | + ::UUID uuid; |
| 34 | + UuidCreate(&uuid); |
| 35 | + |
| 36 | + memcpy(Value, &uuid, Size); |
| 37 | +#else |
24 | 38 | uuid_generate_random(Value);
|
| 39 | +#endif |
25 | 40 | }
|
26 | 41 |
|
27 |
| -UUID::UUID(FromTime_t) { |
| 42 | +swift::UUID::UUID(FromTime_t) { |
| 43 | +#if defined(_WIN32) |
| 44 | + ::UUID uuid; |
| 45 | + UuidCreate(&uuid); |
| 46 | + |
| 47 | + memcpy(Value, &uuid, Size); |
| 48 | +#else |
28 | 49 | uuid_generate_time(Value);
|
| 50 | +#endif |
29 | 51 | }
|
30 | 52 |
|
31 |
| -UUID::UUID() { |
| 53 | +swift::UUID::UUID() { |
| 54 | +#if defined(_WIN32) |
| 55 | + ::UUID uuid = *((::UUID *)&Value); |
| 56 | + UuidCreateNil(&uuid); |
| 57 | + |
| 58 | + memcpy(Value, &uuid, Size); |
| 59 | +#else |
32 | 60 | uuid_clear(Value);
|
| 61 | +#endif |
33 | 62 | }
|
34 | 63 |
|
35 |
| -Optional<UUID> UUID::fromString(const char *s) { |
36 |
| - UUID result; |
| 64 | +Optional<swift::UUID> swift::UUID::fromString(const char *s) { |
| 65 | +#if defined(_WIN32) |
| 66 | + RPC_CSTR t = const_cast<RPC_CSTR>(reinterpret_cast<const unsigned char*>(s)); |
| 67 | + |
| 68 | + ::UUID uuid; |
| 69 | + RPC_STATUS status = UuidFromStringA(t, &uuid); |
| 70 | + if (status == RPC_S_INVALID_STRING_UUID) { |
| 71 | + return None; |
| 72 | + } |
| 73 | + |
| 74 | + swift::UUID result = UUID(); |
| 75 | + memcpy(result.Value, &uuid, Size); |
| 76 | + return result; |
| 77 | +#else |
| 78 | + swift::UUID result; |
37 | 79 | if (uuid_parse(s, result.Value))
|
38 | 80 | return None;
|
39 | 81 | return result;
|
| 82 | +#endif |
40 | 83 | }
|
41 | 84 |
|
42 |
| -void UUID::toString(llvm::SmallVectorImpl<char> &out) const { |
| 85 | +void swift::UUID::toString(llvm::SmallVectorImpl<char> &out) const { |
43 | 86 | out.resize(UUID::StringBufferSize);
|
| 87 | +#if defined(_WIN32) |
| 88 | + ::UUID uuid; |
| 89 | + memcpy(&uuid, Value, Size); |
| 90 | + |
| 91 | + RPC_CSTR str; |
| 92 | + UuidToStringA(&uuid, &str); |
| 93 | + |
| 94 | + char* signedStr = reinterpret_cast<char*>(str); |
| 95 | + memcpy(out.data(), signedStr, StringBufferSize); |
| 96 | +#else |
44 | 97 | uuid_unparse_upper(Value, out.data());
|
| 98 | +#endif |
45 | 99 | // Pop off the null terminator.
|
46 | 100 | assert(out.back() == '\0' && "did not null-terminate?!");
|
47 | 101 | out.pop_back();
|
48 | 102 | }
|
49 | 103 |
|
50 |
| -int UUID::compare(UUID y) const { |
| 104 | +int swift::UUID::compare(UUID y) const { |
| 105 | +#if defined(_WIN32) |
| 106 | + RPC_STATUS s; |
| 107 | + ::UUID uuid1; |
| 108 | + memcpy(&uuid1, Value, Size); |
| 109 | + |
| 110 | + ::UUID uuid2; |
| 111 | + memcpy(&uuid2, y.Value, Size); |
| 112 | + |
| 113 | + return UuidCompare(&uuid1, &uuid2, &s); |
| 114 | +#else |
51 | 115 | return uuid_compare(Value, y.Value);
|
| 116 | +#endif |
52 | 117 | }
|
53 | 118 |
|
54 | 119 | llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os, UUID uuid) {
|
|
0 commit comments