Skip to content

Commit

Permalink
AK: Add LexicalPath::relative_path
Browse files Browse the repository at this point in the history
  • Loading branch information
itamar8910 authored and awesomekling committed Feb 20, 2021
1 parent 50f887c commit 7ecb21a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions AK/LexicalPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,19 @@ String LexicalPath::canonicalized_path(const StringView& path)
return LexicalPath(path).string();
}

String LexicalPath::relative_path(const String absolute_path, const String& prefix)
{
if (!LexicalPath { absolute_path }.is_absolute() || !LexicalPath { prefix }.is_absolute())
return {};

if (!absolute_path.starts_with(prefix))
return absolute_path;

size_t prefix_length = LexicalPath { prefix }.string().length() + 1;
if (prefix_length >= absolute_path.length())
return {};

return absolute_path.substring(prefix_length);
}

}
1 change: 1 addition & 0 deletions AK/LexicalPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class LexicalPath {
bool has_extension(const StringView&) const;

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

private:
void canonicalize();
Expand Down

0 comments on commit 7ecb21a

Please sign in to comment.