mirror of
https://github.com/textmate/textmate.git
synced 2026-01-22 13:17:55 -05:00
Add path::is_absolute.
This is actually not entirely trivial since we can have a path that starts with a slash but includes more ‘..’ components than there are actual directories in the path.
This commit is contained in:
@@ -178,6 +178,17 @@ namespace path
|
||||
return !path.empty() && path[0] == '/' ? normalize(path) : normalize(base + "/" + path);
|
||||
}
|
||||
|
||||
bool is_absolute (std::string const& path)
|
||||
{
|
||||
if(!path.empty() && path[0] == '/')
|
||||
{
|
||||
std::string p = normalize(path);
|
||||
if(p != "/.." && p.find("/../") != 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string with_tilde (std::string const& p)
|
||||
{
|
||||
std::string const& base = home();
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace path
|
||||
|
||||
PUBLIC std::string join (std::string const& base, std::string const& path); // this will normalize the (resulting) path
|
||||
|
||||
PUBLIC bool is_absolute (std::string const& path);
|
||||
PUBLIC std::string with_tilde (std::string const& path); // /Users/me/foo.html.erb → ~/foo.html.erb
|
||||
PUBLIC std::string relative_to (std::string const& path, std::string const& base); // /Users/me/foo.html.erb (arg: ~/Desktop) → ../foo.html.erb
|
||||
|
||||
|
||||
@@ -93,6 +93,26 @@ public:
|
||||
TS_ASSERT_EQUALS(path::join("foo/bar", "fud"), "foo/bar/fud");
|
||||
}
|
||||
|
||||
void test_is_absolute ()
|
||||
{
|
||||
TS_ASSERT_EQUALS(path::is_absolute("../"), false);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("../foo"), false);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("./"), false);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("/."), true);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("/.."), false);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("/../"), false);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("/../tmp"), false); // this path is actually valid, so might revise path::normalize()
|
||||
TS_ASSERT_EQUALS(path::is_absolute("/./.."), false);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("/./../tmp"), false); // this path is actually valid, so might revise path::normalize()
|
||||
TS_ASSERT_EQUALS(path::is_absolute("/./foo"), true);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("//."), true);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("//../../foo"), false);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("//./foo"), true);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("/foo/.."), true);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("/foo/../.."), false);
|
||||
TS_ASSERT_EQUALS(path::is_absolute("foo"), false);
|
||||
}
|
||||
|
||||
void test_with_tilde ()
|
||||
{
|
||||
using namespace path;
|
||||
|
||||
Reference in New Issue
Block a user