mirror of
https://github.com/SerenityOS/serenity
synced 2026-04-25 17:15:42 +02:00
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:
@@ -72,6 +72,23 @@ bool LexicalPath::has_extension(StringView extension) const
|
||||
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
|
||||
{
|
||||
// Any relative path is a child of an absolute path.
|
||||
|
||||
@@ -27,6 +27,7 @@ public:
|
||||
explicit LexicalPath(ByteString);
|
||||
|
||||
bool is_absolute() const { return !m_string.is_empty() && m_string[0] == '/'; }
|
||||
bool is_canonical() const;
|
||||
ByteString const& string() const { return m_string; }
|
||||
|
||||
StringView dirname() const { return m_dirname; }
|
||||
|
||||
Reference in New Issue
Block a user