From 4c018909f72719b8916cd1480216e9ecb7a16d7a Mon Sep 17 00:00:00 2001 From: Max Wipfli Date: Tue, 29 Jun 2021 17:55:12 +0200 Subject: [PATCH] AK: Make LexicalPath immutable This replaces the current LexicalPath::append() API with a new method that returns a new LexicalPath object and doesn't touch the this-object. With this, LexicalPath is now immutable. It also adds a LexicalPath::parent() method and the relevant test cases. --- AK/LexicalPath.cpp | 13 ++++++------- AK/LexicalPath.h | 3 ++- Tests/AK/TestLexicalPath.cpp | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/AK/LexicalPath.cpp b/AK/LexicalPath.cpp index 8a9ae25155..2108ed940e 100644 --- a/AK/LexicalPath.cpp +++ b/AK/LexicalPath.cpp @@ -137,15 +137,14 @@ String LexicalPath::relative_path(String absolute_path, String const& prefix) return absolute_path.substring(prefix_length); } -void LexicalPath::append(String const& component) +LexicalPath LexicalPath::append(StringView const& value) const { - StringBuilder builder; - builder.append(m_string); - builder.append('/'); - builder.append(component); + return LexicalPath::join(m_string, value); +} - m_string = builder.to_string(); - canonicalize(); +LexicalPath LexicalPath::parent() const +{ + return append(".."); } } diff --git a/AK/LexicalPath.h b/AK/LexicalPath.h index 4c425c4ed3..8c4d71f409 100644 --- a/AK/LexicalPath.h +++ b/AK/LexicalPath.h @@ -30,7 +30,8 @@ public: bool has_extension(StringView const&) const; - void append(String const& component); + [[nodiscard]] LexicalPath append(StringView const&) const; + [[nodiscard]] LexicalPath parent() const; static String canonicalized_path(String); static String relative_path(String absolute_path, String const& prefix); diff --git a/Tests/AK/TestLexicalPath.cpp b/Tests/AK/TestLexicalPath.cpp index 7b6046580d..61832724fb 100644 --- a/Tests/AK/TestLexicalPath.cpp +++ b/Tests/AK/TestLexicalPath.cpp @@ -165,11 +165,38 @@ TEST_CASE(join) EXPECT_EQ(LexicalPath::join("anon", "foo.txt").string(), "anon/foo.txt"); EXPECT_EQ(LexicalPath::join("/home", "anon/foo.txt").string(), "/home/anon/foo.txt"); EXPECT_EQ(LexicalPath::join("/", "foo.txt").string(), "/foo.txt"); + EXPECT_EQ(LexicalPath::join("/home", "anon", "foo.txt").string(), "/home/anon/foo.txt"); } TEST_CASE(append) { - LexicalPath path("/home/anon"); - path.append("foo.txt"); - EXPECT_EQ(path.string(), "/home/anon/foo.txt"); + LexicalPath path("/home/anon/"); + auto new_path = path.append("foo.txt"); + EXPECT_EQ(new_path.string(), "/home/anon/foo.txt"); +} + +TEST_CASE(parent) +{ + { + LexicalPath path("/home/anon/foo.txt"); + auto parent = path.parent(); + EXPECT_EQ(parent.string(), "/home/anon"); + } + { + LexicalPath path("anon/foo.txt"); + auto parent = path.parent(); + EXPECT_EQ(parent.string(), "anon"); + } + { + LexicalPath path("foo.txt"); + auto parent = path.parent(); + EXPECT_EQ(parent.string(), "."); + auto parent_of_parent = parent.parent(); + EXPECT_EQ(parent_of_parent.string(), ".."); + } + { + LexicalPath path("/"); + auto parent = path.parent(); + EXPECT_EQ(parent.string(), "/"); + } }