mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
AK: Make FileSystemPath better at handling relative paths
Relative paths now canonicalize into a string starting with "./" Previously, "foo" would be canonicalized as "/foo" which was clearly not right.
This commit is contained in:
parent
b1bc7a1b5d
commit
56eaf9b033
Notes:
sideshowbarker
2024-07-19 12:33:34 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/56eaf9b0335
2 changed files with 54 additions and 6 deletions
|
@ -14,13 +14,26 @@ FileSystemPath::FileSystemPath(const StringView& s)
|
||||||
|
|
||||||
void FileSystemPath::canonicalize()
|
void FileSystemPath::canonicalize()
|
||||||
{
|
{
|
||||||
|
if (m_string.is_empty()) {
|
||||||
|
m_parts.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_absolute_path = m_string[0] == '/';
|
||||||
auto parts = m_string.split_view('/');
|
auto parts = m_string.split_view('/');
|
||||||
|
|
||||||
|
if (!is_absolute_path)
|
||||||
|
parts.prepend(".");
|
||||||
|
|
||||||
int approximate_canonical_length = 0;
|
int approximate_canonical_length = 0;
|
||||||
Vector<String> canonical_parts;
|
Vector<String> canonical_parts;
|
||||||
|
|
||||||
for (auto& part : parts) {
|
for (int i = 0; i < parts.size(); ++i) {
|
||||||
|
auto& part = parts[i];
|
||||||
|
if (is_absolute_path || i != 0) {
|
||||||
if (part == ".")
|
if (part == ".")
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
if (part == "..") {
|
if (part == "..") {
|
||||||
if (!canonical_parts.is_empty())
|
if (!canonical_parts.is_empty())
|
||||||
canonical_parts.take_last();
|
canonical_parts.take_last();
|
||||||
|
@ -43,9 +56,11 @@ void FileSystemPath::canonicalize()
|
||||||
m_extension = name_parts[1];
|
m_extension = name_parts[1];
|
||||||
|
|
||||||
StringBuilder builder(approximate_canonical_length);
|
StringBuilder builder(approximate_canonical_length);
|
||||||
for (auto& cpart : canonical_parts) {
|
for (int i = 0; i < canonical_parts.size(); ++i) {
|
||||||
|
auto& canonical_part = canonical_parts[i];
|
||||||
|
if (is_absolute_path || i != 0)
|
||||||
builder.append('/');
|
builder.append('/');
|
||||||
builder.append(cpart);
|
builder.append(canonical_part);
|
||||||
}
|
}
|
||||||
m_parts = move(canonical_parts);
|
m_parts = move(canonical_parts);
|
||||||
m_string = builder.to_string();
|
m_string = builder.to_string();
|
||||||
|
|
|
@ -26,4 +26,37 @@ TEST_CASE(dotdot_coalescing)
|
||||||
EXPECT_EQ(FileSystemPath("/../../../../").string(), "/");
|
EXPECT_EQ(FileSystemPath("/../../../../").string(), "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(relative_paths)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
FileSystemPath path("simple");
|
||||||
|
EXPECT_EQ(path.is_valid(), true);
|
||||||
|
EXPECT_EQ(path.string(), "./simple");
|
||||||
|
EXPECT_EQ(path.parts().size(), 2);
|
||||||
|
EXPECT_EQ(path.basename(), "simple");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
FileSystemPath path("a/relative/path");
|
||||||
|
EXPECT_EQ(path.is_valid(), true);
|
||||||
|
EXPECT_EQ(path.string(), "./a/relative/path");
|
||||||
|
EXPECT_EQ(path.parts().size(), 4);
|
||||||
|
EXPECT_EQ(path.basename(), "path");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
FileSystemPath path("./././foo");
|
||||||
|
EXPECT_EQ(path.is_valid(), true);
|
||||||
|
EXPECT_EQ(path.string(), "./foo");
|
||||||
|
EXPECT_EQ(path.parts().size(), 2);
|
||||||
|
EXPECT_EQ(path.basename(), "foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
FileSystemPath path(".");
|
||||||
|
EXPECT_EQ(path.is_valid(), true);
|
||||||
|
EXPECT_EQ(path.string(), ".");
|
||||||
|
EXPECT_EQ(path.parts().size(), 1);
|
||||||
|
EXPECT_EQ(path.basename(), ".");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_MAIN(FileSystemPath)
|
TEST_MAIN(FileSystemPath)
|
||||||
|
|
Loading…
Reference in a new issue