diff --git a/apps/components_tests/vfs/testpathutil.cpp b/apps/components_tests/vfs/testpathutil.cpp index 41f9a22f78..934cf6e972 100644 --- a/apps/components_tests/vfs/testpathutil.cpp +++ b/apps/components_tests/vfs/testpathutil.cpp @@ -338,5 +338,23 @@ namespace VFS::Path const NormalizedView value("foo"); EXPECT_EQ(value.filename(), "foo"); } + + TEST(VFSPathNormalizedViewTest, stemShouldOmitPathAndExtension) + { + const NormalizedView value("foo/bar.a"); + EXPECT_EQ(value.stem(), "bar"); + } + + TEST(VFSPathNormalizedViewTest, stemShouldReturnSameValueForPathWithSingleComponent) + { + const NormalizedView value("foo"); + EXPECT_EQ(value.stem(), "foo"); + } + + TEST(VFSPathNormalizedViewTest, stemShouldIncludeDotsBeforeTheExtension) + { + const NormalizedView value("last voyage of the u.s.s. constitution.swf"); + EXPECT_EQ(value.stem(), "last voyage of the u.s.s. constitution"); + } } } diff --git a/components/vfs/pathutil.hpp b/components/vfs/pathutil.hpp index ab6281a92b..af470c120a 100644 --- a/components/vfs/pathutil.hpp +++ b/components/vfs/pathutil.hpp @@ -232,18 +232,6 @@ namespace VFS::Path return p; } - std::string_view stem() const - { - std::string_view stem = mValue; - std::size_t pos = stem.find_last_of(separator); - if (pos != std::string_view::npos) - stem = stem.substr(pos + 1); - pos = stem.find_first_of(extensionSeparator); - if (pos != std::string_view::npos) - stem = stem.substr(0, pos); - return stem; - } - NormalizedView filename() const { NormalizedView result(*this); @@ -252,6 +240,14 @@ namespace VFS::Path return result; } + std::string_view stem() const + { + std::string_view stem = filename().value(); + if (const std::size_t pos = stem.find_last_of(extensionSeparator); pos != std::string_view::npos) + stem = stem.substr(0, pos); + return stem; + } + constexpr ExtensionView extension() const { ExtensionView result;