diff --git a/apps/openmw_test_suite/esm/testrefid.cpp b/apps/openmw_test_suite/esm/testrefid.cpp index 08f4b4256b..4083046d15 100644 --- a/apps/openmw_test_suite/esm/testrefid.cpp +++ b/apps/openmw_test_suite/esm/testrefid.cpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace ESM @@ -122,5 +123,26 @@ namespace ESM const std::string_view b = "B"; EXPECT_LT(a, b); } + + TEST(ESMRefIdTest, hasCaseInsensitiveStrongOrderWithStringView) + { + const RefId a = RefId::stringRefId("a"); + const std::string_view b = "B"; + const RefId c = RefId::stringRefId("c"); + EXPECT_LT(a, b); + EXPECT_LT(b, c); + } + + TEST(ESMRefIdTest, canBeUsedAsMapKeyWithLookupByStringView) + { + const std::map> map({ { ESM::RefId::stringRefId("a"), 42 } }); + EXPECT_EQ(map.count("A"), 1); + } + + TEST(ESMRefIdTest, canBeUsedAsLookupKeyForMapWithStringKey) + { + const std::map> map({ { "a", 42 } }); + EXPECT_EQ(map.count(ESM::RefId::stringRefId("A")), 1); + } } } diff --git a/components/esm/refid.cpp b/components/esm/refid.cpp index 27d3bb6d9d..a866ecb86e 100644 --- a/components/esm/refid.cpp +++ b/components/esm/refid.cpp @@ -16,9 +16,14 @@ namespace ESM return Misc::StringUtils::ciLess(mId, rhs.mId); } - bool RefId::operator<(std::string_view rhs) const + bool operator<(const RefId& lhs, std::string_view rhs) { - return Misc::StringUtils::ciLess(mId, rhs); + return Misc::StringUtils::ciLess(lhs.mId, rhs); + } + + bool operator<(std::string_view lhs, const RefId& rhs) + { + return Misc::StringUtils::ciLess(lhs, rhs.mId); } std::ostream& operator<<(std::ostream& os, const RefId& refId) diff --git a/components/esm/refid.hpp b/components/esm/refid.hpp index 0d297cfb0f..e7385e9c1b 100644 --- a/components/esm/refid.hpp +++ b/components/esm/refid.hpp @@ -39,7 +39,9 @@ namespace ESM bool operator<(const RefId& rhs) const; - bool operator<(std::string_view rhs) const; + friend bool operator<(const RefId& lhs, std::string_view rhs); + + friend bool operator<(std::string_view lhs, const RefId& rhs); friend std::ostream& operator<<(std::ostream& os, const RefId& dt);