Adds llvm::sys::path::is_separator() to test whether a char is a path separator
authorZhanyong Wan <wan@google.com>
Fri, 11 Feb 2011 21:24:40 +0000 (21:24 +0000)
committerZhanyong Wan <wan@google.com>
Fri, 11 Feb 2011 21:24:40 +0000 (21:24 +0000)
on the host OS.  Reviewed by dgregor.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125406 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/PathV2.h
lib/Support/PathV2.cpp
unittests/Support/Path.cpp

index 3866e410e62413c79c67b1733aaeefecd23ba85a..251563398fb4f58988b52dc00bb2fce8cdc98a14 100644 (file)
@@ -258,6 +258,12 @@ const StringRef stem(StringRef path);
 /// @result The extension of \a path.
 const StringRef extension(StringRef path);
 
+/// @brief Check whether the given char is a path separator on the host OS.
+///
+/// @param value a character
+/// @result true if \a value is a path separator character on the host OS
+bool is_separator(char value);
+
 /// @brief Has root name?
 ///
 /// root_name != ""
index 346387630465c33ace3a8c65b8bf4527143b7a0f..9f9dea25876cd6a23b84543dbf5edf40c380a6bb 100644 (file)
 
 namespace {
   using llvm::StringRef;
-
-  bool is_separator(const char value) {
-    switch(value) {
-#ifdef LLVM_ON_WIN32
-    case '\\': // fall through
-#endif
-    case '/': return true;
-    default: return false;
-    }
-  }
+  using llvm::sys::path::is_separator;
 
 #ifdef LLVM_ON_WIN32
   const StringRef separators = "\\/";
@@ -154,7 +145,7 @@ namespace {
 
     return end_pos;
   }
-}
+} // end unnamed namespace
 
 namespace llvm {
 namespace sys  {
@@ -483,6 +474,16 @@ const StringRef extension(StringRef path) {
       return fname.substr(pos);
 }
 
+bool is_separator(char value) {
+  switch(value) {
+#ifdef LLVM_ON_WIN32
+    case '\\': // fall through
+#endif
+    case '/': return true;
+    default: return false;
+  }
+}
+
 bool has_root_name(const Twine &path) {
   SmallString<128> path_storage;
   StringRef p = path.toStringRef(path_storage);
@@ -737,7 +738,7 @@ error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
 
   return success;
 }
-}
+} // end unnamed namespace
 
 error_code remove_all(const Twine &path, uint32_t &num_removed) {
   SmallString<128> path_storage;
index 1f67f13e97e45a14342d94f0ae75371c6f84ab52..60d08bc92dbec262a459175f939a1087a77bac40 100644 (file)
@@ -29,6 +29,19 @@ using namespace llvm::sys;
 
 namespace {
 
+TEST(is_separator, Works) {
+  EXPECT_TRUE(path::is_separator('/'));
+  EXPECT_FALSE(path::is_separator('\0'));
+  EXPECT_FALSE(path::is_separator('-'));
+  EXPECT_FALSE(path::is_separator(' '));
+
+#ifdef LLVM_ON_WIN32
+  EXPECT_TRUE(path::is_separator('\\'));
+#else
+  EXPECT_FALSE(path::is_separator('\\'));
+#endif
+}
+
 TEST(Support, Path) {
   SmallVector<StringRef, 40> paths;
   paths.push_back("");