AK: Add LexicalPath::append and LexicalPath::join

This patch adds two new methods to LexicalPath.  LexicalPath::append
appends a new path component to a LexicalPath, and LexicalPath::join
constructs a new LexicalPath from one or more components.

Co-authored-by: Gunnar Beutner <gunnar@beutner.name>
This commit is contained in:
sin-ack 2021-05-12 19:17:39 +00:00 committed by Andreas Kling
parent 3f9927b0c3
commit 2de11b0dc8
Notes: sideshowbarker 2024-07-18 18:17:14 +09:00
2 changed files with 23 additions and 0 deletions

View file

@ -116,4 +116,15 @@ String LexicalPath::relative_path(String absolute_path, const String& prefix)
return absolute_path.substring(prefix_length);
}
void LexicalPath::append(String const& component)
{
StringBuilder builder;
builder.append(m_string);
builder.append('/');
builder.append(component);
m_string = builder.to_string();
canonicalize();
}
}

View file

@ -29,9 +29,21 @@ public:
bool has_extension(const StringView&) const;
void append(String const& component);
static String canonicalized_path(String);
static String relative_path(String absolute_path, String const& prefix);
template<typename... S>
static LexicalPath join(String const& first, S&&... rest)
{
StringBuilder builder;
builder.append(first);
((builder.append('/'), builder.append(forward<S>(rest))), ...);
return LexicalPath { builder.to_string() };
}
private:
void canonicalize();