Skip to content

Commit 4c01890

Browse files
MaxWipfliawesomekling
authored andcommitted
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.
1 parent 1e80022 commit 4c01890

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

AK/LexicalPath.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,14 @@ String LexicalPath::relative_path(String absolute_path, String const& prefix)
137137
return absolute_path.substring(prefix_length);
138138
}
139139

140-
void LexicalPath::append(String const& component)
140+
LexicalPath LexicalPath::append(StringView const& value) const
141141
{
142-
StringBuilder builder;
143-
builder.append(m_string);
144-
builder.append('/');
145-
builder.append(component);
142+
return LexicalPath::join(m_string, value);
143+
}
146144

147-
m_string = builder.to_string();
148-
canonicalize();
145+
LexicalPath LexicalPath::parent() const
146+
{
147+
return append("..");
149148
}
150149

151150
}

AK/LexicalPath.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class LexicalPath {
3030

3131
bool has_extension(StringView const&) const;
3232

33-
void append(String const& component);
33+
[[nodiscard]] LexicalPath append(StringView const&) const;
34+
[[nodiscard]] LexicalPath parent() const;
3435

3536
static String canonicalized_path(String);
3637
static String relative_path(String absolute_path, String const& prefix);

Tests/AK/TestLexicalPath.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,38 @@ TEST_CASE(join)
165165
EXPECT_EQ(LexicalPath::join("anon", "foo.txt").string(), "anon/foo.txt");
166166
EXPECT_EQ(LexicalPath::join("/home", "anon/foo.txt").string(), "/home/anon/foo.txt");
167167
EXPECT_EQ(LexicalPath::join("/", "foo.txt").string(), "/foo.txt");
168+
EXPECT_EQ(LexicalPath::join("/home", "anon", "foo.txt").string(), "/home/anon/foo.txt");
168169
}
169170

170171
TEST_CASE(append)
171172
{
172-
LexicalPath path("/home/anon");
173-
path.append("foo.txt");
174-
EXPECT_EQ(path.string(), "/home/anon/foo.txt");
173+
LexicalPath path("/home/anon/");
174+
auto new_path = path.append("foo.txt");
175+
EXPECT_EQ(new_path.string(), "/home/anon/foo.txt");
176+
}
177+
178+
TEST_CASE(parent)
179+
{
180+
{
181+
LexicalPath path("/home/anon/foo.txt");
182+
auto parent = path.parent();
183+
EXPECT_EQ(parent.string(), "/home/anon");
184+
}
185+
{
186+
LexicalPath path("anon/foo.txt");
187+
auto parent = path.parent();
188+
EXPECT_EQ(parent.string(), "anon");
189+
}
190+
{
191+
LexicalPath path("foo.txt");
192+
auto parent = path.parent();
193+
EXPECT_EQ(parent.string(), ".");
194+
auto parent_of_parent = parent.parent();
195+
EXPECT_EQ(parent_of_parent.string(), "..");
196+
}
197+
{
198+
LexicalPath path("/");
199+
auto parent = path.parent();
200+
EXPECT_EQ(parent.string(), "/");
201+
}
175202
}

0 commit comments

Comments
 (0)