Utils: Add endsWith to SmallStringView

Change-Id: Iea8286b46c28a68b26904e256b9f084c32c79909
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2022-07-05 14:45:16 +02:00
parent 659dabd1a6
commit 8fbd703974
2 changed files with 14 additions and 0 deletions

View File

@@ -106,6 +106,11 @@ public:
{
return *begin() == characterToSearch;
}
constexpr bool endsWith(SmallStringView ending) const noexcept
{
return size() >= ending.size() && std::equal(ending.rbegin(), ending.rend(), rbegin());
}
};
constexpr bool operator==(SmallStringView first, SmallStringView second) noexcept

View File

@@ -1296,6 +1296,15 @@ TEST(SmallString, EndsWith)
ASSERT_FALSE(text.endsWith('x'));
}
TEST(SmallString, EndsWithStringView)
{
SmallStringView text("/my/path");
ASSERT_TRUE(text.endsWith("/my/path"));
ASSERT_TRUE(text.endsWith("path"));
ASSERT_FALSE(text.endsWith("paths"));
}
TEST(SmallString, EndsWithSmallString)
{
SmallString text("/my/path");