Support/PathV2: Add extension implementation.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Wed, 1 Dec 2010 03:37:41 +0000 (03:37 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Wed, 1 Dec 2010 03:37:41 +0000 (03:37 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120550 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/PathV2.cpp
unittests/Support/Path.cpp

index e360e9cd1fef0d0c584ecb68d1bcf1be7a825597..47e59dd1cf146b6ab86cec86ad36ad4edb49d6d2 100644 (file)
@@ -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);
+}
+
 }
 }
 }
index 70cf213f1917a69de59a9fcc85dc60874bb14dec..2114a181fa1a7c6214abc491dd4d1bf64b65d0ec 100644 (file)
@@ -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';