allow Path::join() to take an arbitrary number of arguments

This commit is contained in:
Josh Holtrop 2016-07-06 21:32:47 -04:00
parent 2012591b27
commit 725a0f22a1
3 changed files with 22 additions and 2 deletions

View File

@ -17,7 +17,7 @@ std::string Path::dirname(const std::string & s)
return std::string(s, 0, pos);
}
std::string Path::join(const std::string & first, const std::string & second)
std::string Path::_join(const std::string & first, const std::string & second)
{
if (first == "")
{

View File

@ -7,9 +7,24 @@ class Path
{
public:
static std::string dirname(const std::string & s);
static std::string join(const std::string & first, const std::string & second);
template <typename... Parts>
static std::string join(Parts... parts)
{
return _join(parts...);
}
static bool is_file(const std::string & s);
static bool is_dir(const std::string & s);
protected:
static std::string _join(const std::string & first, const std::string & second);
template <typename... Parts>
static std::string _join(const std::string & first, const std::string & second, Parts... more)
{
return _join(_join(first, second), more...);
}
};
#endif

View File

@ -62,6 +62,11 @@ TEST(Path_join, does_not_add_extra_slash_when_first_path_already_ends_with_one)
EXPECT_EQ("/var/run", Path::join("/var/", "run"));
}
TEST(Path_join, allows_arbitrary_number_of_arguments)
{
EXPECT_EQ("/usr/local/share/jes/runtime", Path::join("/usr/local/", "share", "jes/", "runtime"));
}
TEST(Path_is_file, returns_true_for_file)
{