AK: Add is_canonical method for LexicalPath

Similarly to KLexicalPath, we might need to check if a path is canonical
or not.
This commit is contained in:
Liav A. 2024-02-03 13:16:00 +02:00 committed by Tim Schumacher
parent 0e6624dc86
commit 1c6cf62c4a
2 changed files with 18 additions and 0 deletions

View file

@ -72,6 +72,23 @@ bool LexicalPath::has_extension(StringView extension) const
return m_string.ends_with(extension, CaseSensitivity::CaseInsensitive); return m_string.ends_with(extension, CaseSensitivity::CaseInsensitive);
} }
bool LexicalPath::is_canonical() const
{
// FIXME: This can probably be done more efficiently.
// FIXME: Find a way to share this with KLexicalPath?
if (m_string.is_empty())
return false;
if (m_string.ends_with('/') && m_string.length() != 1)
return false;
if (m_string.starts_with("./"sv) || m_string.contains("/./"sv) || m_string.ends_with("/."sv))
return false;
if (m_string.starts_with("../"sv) || m_string.contains("/../"sv) || m_string.ends_with("/.."sv))
return false;
if (m_string.contains("//"sv))
return false;
return true;
}
bool LexicalPath::is_child_of(LexicalPath const& possible_parent) const bool LexicalPath::is_child_of(LexicalPath const& possible_parent) const
{ {
// Any relative path is a child of an absolute path. // Any relative path is a child of an absolute path.

View file

@ -27,6 +27,7 @@ public:
explicit LexicalPath(ByteString); explicit LexicalPath(ByteString);
bool is_absolute() const { return !m_string.is_empty() && m_string[0] == '/'; } bool is_absolute() const { return !m_string.is_empty() && m_string[0] == '/'; }
bool is_canonical() 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; }