Skip to content

Commit

Permalink
Add guid brace parsing (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaanstra authored Feb 24, 2022
1 parent 0c8f4e1 commit 0e33676
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 8 additions & 1 deletion strings/base_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ WINRT_EXPORT namespace winrt
private:

template <typename TStringView>
static constexpr guid parse(TStringView const value)
static constexpr guid parse(TStringView value)
{
// Handle {} and ()
if (value.size() == 38 && ((value[0] == '{' && value[37] == '}') || (value[0] == '(' && value[37] == ')')))
{
value.remove_prefix(1);
value.remove_suffix(1);
}

if (value.size() != 36 || value[8] != '-' || value[13] != '-' || value[18] != '-' || value[23] != '-')
{
throw std::invalid_argument("value is not a valid GUID string");
Expand Down
3 changes: 2 additions & 1 deletion test/test/guid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ TEST_CASE("guid")

REQUIRE(winrt::guid("00112233-4455-6677-8899-aabbccddeeff") == expected);
REQUIRE(winrt::guid({ "{00112233-4455-6677-8899-aabbccddeeff}" + 1, 36 }) == expected);
REQUIRE(winrt::guid("{00112233-4455-6677-8899-aabbccddeeff}") == expected);
REQUIRE(winrt::guid("(00112233-4455-6677-8899-aabbccddeeff)") == expected);

REQUIRE_THROWS_AS(winrt::guid(""), std::invalid_argument);
REQUIRE_THROWS_AS(winrt::guid("not a guid"), std::invalid_argument);
REQUIRE_THROWS_AS(winrt::guid("same length string that's not a guid"), std::invalid_argument);
REQUIRE_THROWS_AS(winrt::guid("too long string that's also not a guid"), std::invalid_argument);
REQUIRE_THROWS_AS(winrt::guid("00112233-4455-6677-8899-aabbccddeeff with extra"), std::invalid_argument);
REQUIRE_THROWS_AS(winrt::guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), std::invalid_argument);
REQUIRE_THROWS_AS(winrt::guid("{00112233-4455-6677-8899-aabbccddeeff}"), std::invalid_argument);

// Verify that you can constexpr-construct a guid from a GUID.
constexpr winrt::guid from_abi_guid = GUID{ 0x00112233, 0x4455, 0x6677, { 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff } };
Expand Down

0 comments on commit 0e33676

Please sign in to comment.