summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorsin-ack <sin-ack@users.noreply.github.com>2021-05-12 19:17:39 +0000
committerAndreas Kling <kling@serenityos.org>2021-05-12 22:38:20 +0200
commit2de11b0dc8bbaa0c264a7e6dbb32b5481a337fb8 (patch)
tree54ebe5f2b5b9cf5106c830516654f7e53d9218ef /AK
parent3f9927b0c36d31270781ded6fccf97a7f1beb82e (diff)
downloadserenity-2de11b0dc8bbaa0c264a7e6dbb32b5481a337fb8.zip
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>
Diffstat (limited to 'AK')
-rw-r--r--AK/LexicalPath.cpp11
-rw-r--r--AK/LexicalPath.h12
2 files changed, 23 insertions, 0 deletions
diff --git a/AK/LexicalPath.cpp b/AK/LexicalPath.cpp
index e4dbf706ff..0e18d00365 100644
--- a/AK/LexicalPath.cpp
+++ b/AK/LexicalPath.cpp
@@ -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();
+}
+
}
diff --git a/AK/LexicalPath.h b/AK/LexicalPath.h
index 20ceadaa0a..e5c3b2d733 100644
--- a/AK/LexicalPath.h
+++ b/AK/LexicalPath.h
@@ -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();