Adds llvm::sys::path::is_separator() to test whether a char is a path separator

on the host OS.  Reviewed by dgregor.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125406 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Zhanyong Wan 2011-02-11 21:24:40 +00:00
parent 7973f350b7
commit 63cc3a85cc
3 changed files with 32 additions and 12 deletions

View File

@ -258,6 +258,12 @@ const StringRef stem(StringRef path);
/// @result The extension of \a path. /// @result The extension of \a path.
const StringRef extension(StringRef 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? /// @brief Has root name?
/// ///
/// root_name != "" /// root_name != ""

View File

@ -20,16 +20,7 @@
namespace { namespace {
using llvm::StringRef; using llvm::StringRef;
using llvm::sys::path::is_separator;
bool is_separator(const char value) {
switch(value) {
#ifdef LLVM_ON_WIN32
case '\\': // fall through
#endif
case '/': return true;
default: return false;
}
}
#ifdef LLVM_ON_WIN32 #ifdef LLVM_ON_WIN32
const StringRef separators = "\\/"; const StringRef separators = "\\/";
@ -154,7 +145,7 @@ namespace {
return end_pos; return end_pos;
} }
} } // end unnamed namespace
namespace llvm { namespace llvm {
namespace sys { namespace sys {
@ -483,6 +474,16 @@ const StringRef extension(StringRef path) {
return fname.substr(pos); 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) { bool has_root_name(const Twine &path) {
SmallString<128> path_storage; SmallString<128> path_storage;
StringRef p = path.toStringRef(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; return success;
} }
} } // end unnamed namespace
error_code remove_all(const Twine &path, uint32_t &num_removed) { error_code remove_all(const Twine &path, uint32_t &num_removed) {
SmallString<128> path_storage; SmallString<128> path_storage;

View File

@ -29,6 +29,19 @@ using namespace llvm::sys;
namespace { 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) { TEST(Support, Path) {
SmallVector<StringRef, 40> paths; SmallVector<StringRef, 40> paths;
paths.push_back(""); paths.push_back("");