AK: Add LexicalPath::is_root()

This commit is contained in:
stasoid 2024-11-15 19:00:57 +05:00 committed by Andrew Kaster
parent 77d205571d
commit a423493dd8
Notes: github-actions[bot] 2024-11-20 05:08:15 +00:00
3 changed files with 14 additions and 2 deletions

View file

@ -63,6 +63,11 @@ bool LexicalPath::is_absolute_path(StringView path)
return path.starts_with('/'); return path.starts_with('/');
} }
bool LexicalPath::is_root() const
{
return m_string == "/";
}
Vector<ByteString> LexicalPath::parts() const Vector<ByteString> LexicalPath::parts() const
{ {
Vector<ByteString> vector; Vector<ByteString> vector;

View file

@ -28,6 +28,8 @@ public:
static bool is_absolute_path(StringView path); static bool is_absolute_path(StringView path);
bool is_absolute() const { return is_absolute_path(m_string); } bool is_absolute() const { return is_absolute_path(m_string); }
bool is_root() const;
ByteString const& string() const { return m_string; } ByteString const& string() const { return m_string; }
StringView dirname() const { return m_dirname; } StringView dirname() const { return m_dirname; }

View file

@ -45,6 +45,11 @@ bool LexicalPath::is_absolute_path(StringView path)
return path.length() >= 2 && path[1] == ':'; return path.length() >= 2 && path[1] == ':';
} }
bool LexicalPath::is_root() const
{
return AK::is_root(m_parts);
}
Vector<ByteString> LexicalPath::parts() const Vector<ByteString> LexicalPath::parts() const
{ {
Vector<ByteString> vector; Vector<ByteString> vector;
@ -81,7 +86,7 @@ ByteString LexicalPath::canonicalized_path(ByteString path)
continue; continue;
if (part == ".." && !canonical_parts.is_empty()) { if (part == ".." && !canonical_parts.is_empty()) {
// At the root, .. does nothing. // At the root, .. does nothing.
if (is_root(canonical_parts)) if (AK::is_root(canonical_parts))
continue; continue;
// A .. and a previous non-.. part cancel each other. // A .. and a previous non-.. part cancel each other.
if (canonical_parts.last() != "..") { if (canonical_parts.last() != "..") {
@ -95,7 +100,7 @@ ByteString LexicalPath::canonicalized_path(ByteString path)
StringBuilder builder; StringBuilder builder;
builder.join('\\', canonical_parts); builder.join('\\', canonical_parts);
// "X:" -> "X:\" // "X:" -> "X:\"
if (is_root(canonical_parts)) if (AK::is_root(canonical_parts))
builder.append('\\'); builder.append('\\');
path = builder.to_byte_string(); path = builder.to_byte_string();
return path == "" ? "." : path; return path == "" ? "." : path;