Skip to content

Commit

Permalink
Lowercase UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
lhecker committed Jan 25, 2024
1 parent f4fc024 commit 2df1ae8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
18 changes: 13 additions & 5 deletions src/types/ut_types/UtilsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,23 @@ void UtilsTests::TestClampToShortMax()

void UtilsTests::TestGuidToString()
{
constexpr GUID constantGuid{
static constexpr GUID constantGuid{
0x01020304, 0x0506, 0x0708, { 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 }
};
constexpr std::wstring_view constantGuidString{ L"{01020304-0506-0708-090a-0b0c0d0e0f10}" };

auto generatedGuid{ GuidToString(constantGuid) };
{
const auto str = GuidToString(constantGuid);
const auto guid = GuidFromString(str.c_str());
VERIFY_ARE_EQUAL(L"{01020304-0506-0708-090a-0b0c0d0e0f10}", str);
VERIFY_ARE_EQUAL(constantGuid, guid);
}

VERIFY_ARE_EQUAL(constantGuidString.size(), generatedGuid.size());
VERIFY_ARE_EQUAL(constantGuidString, generatedGuid);
{
const auto str = GuidToPlainString(constantGuid);
const auto guid = GuidFromPlainString(str.c_str());
VERIFY_ARE_EQUAL(L"01020304-0506-0708-090a-0b0c0d0e0f10", str);
VERIFY_ARE_EQUAL(constantGuid, guid);
}
}

void UtilsTests::TestSplitString()
Expand Down
13 changes: 7 additions & 6 deletions src/types/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,27 @@ static constexpr bool _isNumber(const wchar_t wch) noexcept
return wch >= L'0' && wch <= L'9'; // 0x30 - 0x39
}

[[gsl::suppress(bounds)]] static std::wstring guidToStringCommon(const GUID& guid, size_t offset, size_t length)
static std::wstring guidToStringCommon(const GUID& guid, size_t offset, size_t length)
{
// This is just like StringFromGUID2 but with lowercase hexadecimal.
wchar_t buffer[39];
StringFromGUID2(guid, &buffer[0], 39);
swprintf_s(&buffer[0], 39, L"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
return { &buffer[offset], length };
}

// Creates a string from the given GUID in the format "{12345678-ABCD-EF12-3456-7890ABCDEF12}".
// Creates a string from the given GUID in the format "{12345678-abcd-ef12-3456-7890abcdef12}".
std::wstring Utils::GuidToString(const GUID& guid)
{
return guidToStringCommon(guid, 0, 38);
}

// Creates a string from the given GUID in the format "12345678-ABCD-EF12-3456-7890ABCDEF12".
// Creates a string from the given GUID in the format "12345678-abcd-ef12-3456-7890abcdef12".
std::wstring Utils::GuidToPlainString(const GUID& guid)
{
return guidToStringCommon(guid, 1, 36);
}

// Creates a GUID from a string in the format "{12345678-ABCD-EF12-3456-7890ABCDEF12}".
// Creates a GUID from a string in the format "{12345678-abcd-ef12-3456-7890abcdef12}".
// Throws if the conversion failed.
GUID Utils::GuidFromString(_Null_terminated_ const wchar_t* str)
{
Expand All @@ -52,7 +53,7 @@ GUID Utils::GuidFromString(_Null_terminated_ const wchar_t* str)
return result;
}

// Creates a GUID from a string in the format "12345678-ABCD-EF12-3456-7890ABCDEF12".
// Creates a GUID from a string in the format "12345678-abcd-ef12-3456-7890abcdef12".
// Throws if the conversion failed.
//
// Side-note: An interesting quirk of this method is that the given string doesn't need to be null-terminated.
Expand Down

0 comments on commit 2df1ae8

Please sign in to comment.