Skip to content

Commit 142c536

Browse files
committed
Implemented the path::join() function which concatenates two Paths.
1 parent 889a059 commit 142c536

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

‎src/h/path.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ namespace ctgl {
3131
template <typename T>
3232
constexpr auto dropPrefix(T, Path<>) noexcept;
3333

34+
// Concatenates the given Paths using the following tabular expression:
35+
// +-----------------------+ +---------+
36+
// | p1 == DNE | --> | DNE |
37+
// +-----------+-----------+ +---------+
38+
// | p1 != DNE | p2 == DNE | --> | DNE |
39+
// | +-----------+ +---------+
40+
// | | p2 != DNE | --> | p1 + p2 |
41+
// +-----------+-----------+ +---------+
42+
template <typename... Ts, typename... Us>
43+
constexpr auto join(Path<Ts...> p1, Path<Us...> p2) noexcept;
44+
3445
// Chooses the "shorter" of the given Paths using the following tabular expression:
3546
// +--------------------------------------------------+ +----+
3647
// | p1 == DNE | --> | p2 |
@@ -84,6 +95,15 @@ namespace ctgl {
8495
return Path<>{};
8596
}
8697

98+
template <typename... Ts, typename... Us>
99+
constexpr auto join(Path<Ts...> p1, Path<Us...> p2) noexcept {
100+
if constexpr (p1 == DNE || p2 == DNE) {
101+
return DNE;
102+
} else {
103+
return p1 + p2;
104+
}
105+
}
106+
87107
template <typename... Ts, typename... Us>
88108
constexpr auto shortest(Path<Ts...> p1, Path<Us...> p2) noexcept {
89109
if constexpr (p1 == DNE) {

‎src/test/path_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ TEST(PathTest, DropPrefix) {
5555
EXPECT_EQ(path::dropPrefix(N4{}, Path<E34, E41, E23>{}), (Path<E41, E23>{}));
5656
}
5757

58+
// Unit tests for the ctgl::path::join() function.
59+
TEST(PathTest, Join) {
60+
// DNE
61+
EXPECT_EQ(path::join(path::DNE, path::DNE), path::DNE);
62+
EXPECT_EQ(path::join(Path<E11>{}, path::DNE), path::DNE);
63+
EXPECT_EQ(path::join(path::DNE, Path<E11>{}), path::DNE);
64+
65+
// Exists
66+
EXPECT_EQ(path::join(Path<E12>{}, Path<E23>{}), (Path<E12, E23>{}));
67+
EXPECT_EQ(path::join(Path<E12>{}, Path<E12>{}), (Path<E12, E12>{}));
68+
EXPECT_EQ(path::join(Path<E12, E23>{}, Path<E32, E21>{}), (Path<E12, E23, E32, E21>{}));
69+
}
70+
5871
// Unit tests for the ctgl::path::shortest() function.
5972
TEST(PathTest, Shortest) {
6073
// DNE

0 commit comments

Comments
 (0)