diff --git a/lib/Support/PathV2.cpp b/lib/Support/PathV2.cpp index e360e9cd1fe..47e59dd1cf1 100644 --- a/lib/Support/PathV2.cpp +++ b/lib/Support/PathV2.cpp @@ -574,6 +574,22 @@ error_code stem(const StringRef &path, StringRef &result) { return make_error_code(errc::success); } +error_code extension(const StringRef &path, StringRef &result) { + StringRef fname; + if (error_code ec = filename(path, fname)) return ec; + size_t pos = fname.find_last_of('.'); + if (pos == StringRef::npos) + result = StringRef(); + else + if ((fname.size() == 1 && fname == ".") || + (fname.size() == 2 && fname == "..")) + result = StringRef(); + else + result = StringRef(fname.begin() + pos, fname.size() - pos); + + return make_error_code(errc::success); +} + } } } diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index 70cf213f191..2114a181fa1 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -100,6 +100,9 @@ TEST(Support, Path) { if (error_code ec = sys::path::stem(*i, res)) ASSERT_FALSE(ec.message().c_str()); outs() << " stem: " << res << '\n'; + if (error_code ec = sys::path::extension(*i, res)) + ASSERT_FALSE(ec.message().c_str()); + outs() << " stem: " << res << '\n'; temp_store = *i; if (error_code ec = sys::path::make_absolute(temp_store)) @@ -113,10 +116,17 @@ TEST(Support, Path) { if (error_code ec = sys::path::replace_extension(temp_store, "ext")) ASSERT_FALSE(ec.message().c_str()); outs() << " replace_extension: " << temp_store << '\n'; + StringRef stem, ext; if (error_code ec = sys::path::stem( - StringRef(temp_store.begin(), temp_store.size()), res)) + StringRef(temp_store.begin(), temp_store.size()), stem)) ASSERT_FALSE(ec.message().c_str()); - outs() << " stem: " << res << '\n'; + outs() << " stem: " << stem << '\n'; + if (error_code ec = sys::path::extension( + StringRef(temp_store.begin(), temp_store.size()), ext)) + ASSERT_FALSE(ec.message().c_str()); + outs() << " extension: " << ext << '\n'; + EXPECT_EQ(*(--sys::path::end( + StringRef(temp_store.begin(), temp_store.size()))), (stem + ext).str()); if (error_code ec = sys::path::native(*i, temp_store)) ASSERT_FALSE(ec.message().c_str()); outs() << " native: " << temp_store << '\n';