Skip to content

Commit

Permalink
Kernel: Make KLexicalPath::basename() more compliant
Browse files Browse the repository at this point in the history
This removes some assertions from KLexicalPath::basename() by supporting
paths with trailing slashes, empty paths, paths consisting of only
slashes and paths with ending "." and ".." segments.
  • Loading branch information
MaxWipfli authored and awesomekling committed Jul 11, 2021
1 parent a363863 commit 9ab528f
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions Kernel/KLexicalPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Kernel::KLexicalPath {

static StringView const s_single_dot = "."sv;

bool is_absolute(StringView const& path)
{
return !path.is_empty() && path[0] == '/';
Expand All @@ -29,15 +31,20 @@ bool is_canonical(StringView const& path)
return true;
}

StringView basename(StringView const& path)
StringView basename(StringView const& a_path)
{
if (a_path == "/"sv)
return a_path;
if (a_path.is_empty())
return s_single_dot;
auto path = a_path.trim("/"sv, TrimMode::Right);
// NOTE: If it's empty now, it means the path was just a series of slashes.
if (path.is_empty())
return a_path.substring_view(0, 1);
auto slash_index = path.find_last('/');
if (!slash_index.has_value()) {
VERIFY(!path.is_empty());
if (!slash_index.has_value())
return path;
}
auto basename = path.substring_view(*slash_index + 1);
VERIFY(!basename.is_empty() && basename != "."sv && basename != ".."sv);
return basename;
}

Expand Down

0 comments on commit 9ab528f

Please sign in to comment.