diff options
author | kleines Filmröllchen <filmroellchen@serenityos.org> | 2022-06-24 00:40:21 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-11 16:05:23 +0000 |
commit | 16ca41ec108faeecc1ab96ef94df3c025ad7ac3a (patch) | |
tree | 246821588c29edf22cb3b7c5e3ea2f10f3ea9693 /AK/LexicalPath.cpp | |
parent | 4625f7aab549141d861664be282cc8504f0ccd35 (diff) | |
download | serenity-16ca41ec108faeecc1ab96ef94df3c025ad7ac3a.zip |
AK: Add LexicalPath::is_child_of
This API checks whether this path is a child of (or the same as) another
path.
Diffstat (limited to 'AK/LexicalPath.cpp')
-rw-r--r-- | AK/LexicalPath.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/AK/LexicalPath.cpp b/AK/LexicalPath.cpp index 77e09f7abe..4f18a8f8d5 100644 --- a/AK/LexicalPath.cpp +++ b/AK/LexicalPath.cpp @@ -72,6 +72,22 @@ bool LexicalPath::has_extension(StringView extension) const return m_string.ends_with(extension, CaseSensitivity::CaseInsensitive); } +bool LexicalPath::is_child_of(LexicalPath const& possible_parent) const +{ + // Any relative path is a child of an absolute path. + if (!this->is_absolute() && possible_parent.is_absolute()) + return true; + // An absolute path can't meaningfully be a child of a relative path. + if (this->is_absolute() && !possible_parent.is_absolute()) + return false; + + // Two relative paths and two absolute paths can be meaningfully compared. + if (possible_parent.parts_view().size() > this->parts_view().size()) + return false; + auto common_parts_with_parent = this->parts_view().span().trim(possible_parent.parts_view().size()); + return common_parts_with_parent == possible_parent.parts_view().span(); +} + DeprecatedString LexicalPath::canonicalized_path(DeprecatedString path) { if (path.is_null()) |