From 2de11b0dc8bbaa0c264a7e6dbb32b5481a337fb8 Mon Sep 17 00:00:00 2001 From: sin-ack Date: Wed, 12 May 2021 19:17:39 +0000 Subject: [PATCH] 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 --- AK/LexicalPath.cpp | 11 +++++++++++ AK/LexicalPath.h | 12 ++++++++++++ 2 files changed, 23 insertions(+) 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 + static LexicalPath join(String const& first, S&&... rest) + { + StringBuilder builder; + builder.append(first); + ((builder.append('/'), builder.append(forward(rest))), ...); + + return LexicalPath { builder.to_string() }; + } + private: void canonicalize();