mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:24:57 +00:00
AK: Update LexicalPath::relative_path to work for '/' prefix
If the prefix path is just a slash the LexicalPath was removing too many characters. Now only remove an extra character if the prefix is not just the root path.
This commit is contained in:
parent
83d2c3f2f5
commit
4d81d868c7
2 changed files with 14 additions and 1 deletions
|
@ -127,7 +127,9 @@ String LexicalPath::relative_path(const String absolute_path, const String& pref
|
|||
if (!absolute_path.starts_with(prefix))
|
||||
return absolute_path;
|
||||
|
||||
size_t prefix_length = LexicalPath { prefix }.string().length() + 1;
|
||||
size_t prefix_length = LexicalPath { prefix }.string().length();
|
||||
if (prefix != "/")
|
||||
prefix_length++;
|
||||
if (prefix_length >= absolute_path.length())
|
||||
return {};
|
||||
|
||||
|
|
|
@ -84,4 +84,15 @@ TEST_CASE(has_extension)
|
|||
}
|
||||
}
|
||||
|
||||
TEST_CASE(relative_path)
|
||||
{
|
||||
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt", "/tmp"), "abc.txt");
|
||||
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt", "/tmp/"), "abc.txt");
|
||||
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt", "/"), "tmp/abc.txt");
|
||||
EXPECT_EQ(LexicalPath::relative_path("/tmp/abc.txt", "/usr"), "/tmp/abc.txt");
|
||||
|
||||
EXPECT_EQ(LexicalPath::relative_path("/tmp/foo.txt", "tmp"), String {});
|
||||
EXPECT_EQ(LexicalPath::relative_path("tmp/foo.txt", "/tmp"), String {});
|
||||
}
|
||||
|
||||
TEST_MAIN(LexicalPath)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue