mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Add LexicalPath::relative_path
This commit is contained in:
parent
50f887c9d5
commit
7ecb21afa7
Notes:
sideshowbarker
2024-07-18 22:06:48 +09:00
Author: https://github.com/itamar8910 Commit: https://github.com/SerenityOS/serenity/commit/7ecb21afa72 Pull-request: https://github.com/SerenityOS/serenity/pull/5424 Reviewed-by: https://github.com/awesomekling
2 changed files with 16 additions and 0 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -50,6 +50,7 @@ public:
|
|||
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();
|
||||
|
|
Loading…
Reference in a new issue