Skip to content

Commit

Permalink
libshvcore: Rewrite utils::joinPath
Browse files Browse the repository at this point in the history
  • Loading branch information
syyyr committed Feb 17, 2024
1 parent 2b1fb54 commit 985cba2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
19 changes: 12 additions & 7 deletions libshvcore/include/shv/core/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,22 @@ SHVCORE_DECL_EXPORT StringView getToken(StringView strv, char delim = ' ', char
SHVCORE_DECL_EXPORT StringView slice(StringView s, int start, int end);

SHVCORE_DECL_EXPORT std::string joinPath(const StringView &p1, const StringView &p2);
SHVCORE_DECL_EXPORT std::string joinPath();
template <typename StringType>
StringType joinPath(const StringType& str)

template <typename Type>
const auto always_false = std::false_type::value;

template <typename... Types>
void joinPath(const Types& ...pack)
{
return str;
static_assert(sizeof...(pack) >= 2, "Can't use joinPath with less than two paths.");
}

template <typename HeadStringType, typename... StringTypes>
std::string joinPath(const HeadStringType& head, const StringTypes& ...rest)
template <typename FirstStringType, typename SecondStringType, typename... RestStringTypes>
std::string joinPath(const FirstStringType& head, const SecondStringType& second, const RestStringTypes& ...rest)
{
return joinPath(StringView(head), StringView(joinPath(rest...)));
std::string res = joinPath(std::string_view(head), std::string_view(second));
((res = joinPath(std::string_view(res), std::string_view(rest))), ...);
return res;
}

template <typename Container>
Expand Down
5 changes: 0 additions & 5 deletions libshvcore/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ std::string Utils::joinPath(const StringView &p1, const StringView &p2)
return utils::joinPath(p1, p2);
}

std::string utils::joinPath()
{
return {};
}

std::string utils::joinPath(const StringView &p1, const StringView &p2)
{
StringView sv1(p1);
Expand Down
2 changes: 0 additions & 2 deletions libshvcore/tests/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,12 @@ DOCTEST_TEST_CASE("joinPath")
DOCTEST_TEST_CASE("joinPath - variadic arguments")
{
// The function takes any number of args.
REQUIRE(joinPath(std::string("a")) == "a");
REQUIRE(joinPath(std::string("a"), std::string("b")) == "a/b");
REQUIRE(joinPath(std::string("a"), std::string("b"), std::string("c")) == "a/b/c");
REQUIRE(joinPath(std::string("a"), std::string("b"), std::string("c"), std::string("d")) == "a/b/c/d");
REQUIRE(joinPath(std::string("a"), std::string("b"), std::string("c"), std::string("d"), std::string("e")) == "a/b/c/d/e");

// The function supports character literals.
REQUIRE(joinPath("a") == "a");
REQUIRE(joinPath("a", "b") == "a/b");
REQUIRE(joinPath(std::string("a"), "b") == "a/b");
REQUIRE(joinPath("a", std::string("b")) == "a/b");
Expand Down

0 comments on commit 985cba2

Please sign in to comment.