Skip to content

Commit fae5182

Browse files
authored
Merge pull request swiftlang#15127 from compnerd/uuid
UUID
2 parents 3550fb2 + 5db0c06 commit fae5182

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

lib/Basic/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
55
set(UUID_LIBRARIES "")
66
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
77
set(UUID_INCLUDE "")
8-
set(UUID_LIBRARIES "ole32.lib")
8+
set(UUID_LIBRARIES "rpcrt4.lib")
99
else()
1010
find_package(UUID REQUIRED)
1111
set(UUID_INCLUDE "-I${UUID_INCLUDE_DIRS}")

lib/Basic/UUID.cpp

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ swift::UUID::UUID(FromRandom_t) {
4242

4343
swift::UUID::UUID(FromTime_t) {
4444
#if defined(_WIN32)
45-
::GUID uuid;
45+
::UUID uuid;
4646
::CoCreateGuid(&uuid);
4747

4848
memcpy(Value, &uuid, Size);
@@ -53,7 +53,8 @@ swift::UUID::UUID(FromTime_t) {
5353

5454
swift::UUID::UUID() {
5555
#if defined(_WIN32)
56-
::GUID uuid = GUID();
56+
::UUID uuid = *((::UUID *)&Value);
57+
UuidCreateNil(&uuid);
5758

5859
memcpy(Value, &uuid, Size);
5960
#else
@@ -63,18 +64,11 @@ swift::UUID::UUID() {
6364

6465
Optional<swift::UUID> swift::UUID::fromString(const char *s) {
6566
#if defined(_WIN32)
66-
int length = strlen(s) + 1;
67-
wchar_t *unicodeString = new wchar_t[length];
68-
69-
size_t convertedChars = 0;
70-
errno_t conversionResult =
71-
mbstowcs_s(&convertedChars, unicodeString, length, s, length);
72-
assert(conversionResult == 0 &&
73-
"expected successful conversion of char* to wchar_t*");
74-
75-
::GUID uuid;
76-
HRESULT parseResult = CLSIDFromString(unicodeString, &uuid);
77-
if (parseResult != 0) {
67+
RPC_CSTR t = const_cast<RPC_CSTR>(reinterpret_cast<const unsigned char*>(s));
68+
69+
::UUID uuid;
70+
RPC_STATUS status = UuidFromStringA(t, &uuid);
71+
if (status == RPC_S_INVALID_STRING_UUID) {
7872
return None;
7973
}
8074

@@ -92,19 +86,14 @@ Optional<swift::UUID> swift::UUID::fromString(const char *s) {
9286
void swift::UUID::toString(llvm::SmallVectorImpl<char> &out) const {
9387
out.resize(UUID::StringBufferSize);
9488
#if defined(_WIN32)
95-
::GUID uuid;
89+
::UUID uuid;
9690
memcpy(&uuid, Value, Size);
9791

98-
LPOLESTR unicodeStr;
99-
StringFromCLSID(uuid, &unicodeStr);
100-
101-
char str[StringBufferSize];
102-
int strLen = wcstombs(str, unicodeStr, sizeof(str));
103-
104-
assert(strLen == 37 && "expected ascii convertible output from StringFromCLSID.");
105-
(void)strLen;
92+
RPC_CSTR str;
93+
UuidToStringA(&uuid, &str);
10694

107-
memcpy(out.data(), str, StringBufferSize);
95+
char* signedStr = reinterpret_cast<char*>(str);
96+
memcpy(out.data(), signedStr, StringBufferSize);
10897
#else
10998
uuid_unparse_upper(Value, out.data());
11099
#endif
@@ -115,13 +104,14 @@ void swift::UUID::toString(llvm::SmallVectorImpl<char> &out) const {
115104

116105
int swift::UUID::compare(UUID y) const {
117106
#if defined(_WIN32)
118-
::GUID uuid1;
107+
RPC_STATUS s;
108+
::UUID uuid1;
119109
memcpy(&uuid1, Value, Size);
120110

121-
::GUID uuid2;
111+
::UUID uuid2;
122112
memcpy(&uuid2, y.Value, Size);
123113

124-
return memcmp(Value, y.Value, Size);
114+
return UuidCompare(&uuid1, &uuid2, &s);
125115
#else
126116
return uuid_compare(Value, y.Value);
127117
#endif

0 commit comments

Comments
 (0)