2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-06-29 14:11:03 +03:00
|
|
|
* Copyright (c) 2021, Max Wipfli <max.wipfli@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-05-26 14:52:44 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-03-23 15:45:10 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
#include <AK/Vector.h>
|
2018-10-28 10:54:20 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 18:06:21 +03:00
|
|
|
char s_single_dot = '.';
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
LexicalPath::LexicalPath(ByteString path)
|
2021-06-29 21:26:52 +03:00
|
|
|
: m_string(canonicalized_path(move(path)))
|
2018-10-28 10:54:20 +03:00
|
|
|
{
|
2019-08-23 20:55:51 +03:00
|
|
|
if (m_string.is_empty()) {
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 18:06:21 +03:00
|
|
|
m_string = ".";
|
|
|
|
m_dirname = m_string;
|
|
|
|
m_basename = {};
|
|
|
|
m_title = {};
|
|
|
|
m_extension = {};
|
2019-08-23 20:55:51 +03:00
|
|
|
m_parts.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 18:06:21 +03:00
|
|
|
m_parts = m_string.split_view('/');
|
2020-07-15 19:25:23 +03:00
|
|
|
|
2021-07-01 16:01:29 +03:00
|
|
|
auto last_slash_index = m_string.view().find_last('/');
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 18:06:21 +03:00
|
|
|
if (!last_slash_index.has_value()) {
|
|
|
|
// The path contains a single part and is not absolute. m_dirname = "."sv
|
|
|
|
m_dirname = { &s_single_dot, 1 };
|
|
|
|
} else if (*last_slash_index == 0) {
|
|
|
|
// The path contains a single part and is absolute. m_dirname = "/"sv
|
|
|
|
m_dirname = m_string.substring_view(0, 1);
|
2020-07-12 20:37:00 +03:00
|
|
|
} else {
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 18:06:21 +03:00
|
|
|
m_dirname = m_string.substring_view(0, *last_slash_index);
|
2020-07-12 20:37:00 +03:00
|
|
|
}
|
2019-07-29 07:45:50 +03:00
|
|
|
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 18:06:21 +03:00
|
|
|
if (m_string == "/")
|
|
|
|
m_basename = m_string;
|
|
|
|
else {
|
|
|
|
VERIFY(m_parts.size() > 0);
|
|
|
|
m_basename = m_parts.last();
|
|
|
|
}
|
|
|
|
|
2021-07-01 16:01:29 +03:00
|
|
|
auto last_dot_index = m_basename.find_last('.');
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 18:06:21 +03:00
|
|
|
// NOTE: if the dot index is 0, this means we have ".foo", it's not an extension, as the title would then be "".
|
|
|
|
if (last_dot_index.has_value() && *last_dot_index != 0) {
|
|
|
|
m_title = m_basename.substring_view(0, *last_dot_index);
|
|
|
|
m_extension = m_basename.substring_view(*last_dot_index + 1);
|
|
|
|
} else {
|
|
|
|
m_title = m_basename;
|
|
|
|
m_extension = {};
|
2018-10-28 10:54:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
Vector<ByteString> LexicalPath::parts() const
|
2021-06-29 21:26:52 +03:00
|
|
|
{
|
2023-12-16 17:19:34 +03:00
|
|
|
Vector<ByteString> vector;
|
2021-06-29 21:26:52 +03:00
|
|
|
vector.ensure_capacity(m_parts.size());
|
|
|
|
for (auto& part : m_parts)
|
|
|
|
vector.unchecked_append(part);
|
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
bool LexicalPath::has_extension(StringView extension) const
|
2019-05-26 23:33:30 +03:00
|
|
|
{
|
2020-05-26 12:12:18 +03:00
|
|
|
return m_string.ends_with(extension, CaseSensitivity::CaseInsensitive);
|
2018-10-28 10:54:20 +03:00
|
|
|
}
|
|
|
|
|
2022-06-24 01:40:21 +03:00
|
|
|
bool LexicalPath::is_child_of(LexicalPath const& possible_parent) const
|
|
|
|
{
|
|
|
|
// Any relative path is a child of an absolute path.
|
|
|
|
if (!this->is_absolute() && possible_parent.is_absolute())
|
|
|
|
return true;
|
|
|
|
// An absolute path can't meaningfully be a child of a relative path.
|
|
|
|
if (this->is_absolute() && !possible_parent.is_absolute())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Two relative paths and two absolute paths can be meaningfully compared.
|
|
|
|
if (possible_parent.parts_view().size() > this->parts_view().size())
|
|
|
|
return false;
|
|
|
|
auto common_parts_with_parent = this->parts_view().span().trim(possible_parent.parts_view().size());
|
|
|
|
return common_parts_with_parent == possible_parent.parts_view().span();
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString LexicalPath::canonicalized_path(ByteString path)
|
2019-07-15 07:49:28 +03:00
|
|
|
{
|
2021-06-29 21:26:52 +03:00
|
|
|
// NOTE: We never allow an empty m_string, if it's empty, we just set it to '.'.
|
|
|
|
if (path.is_empty())
|
|
|
|
return ".";
|
|
|
|
|
|
|
|
// NOTE: If there are no dots, no '//' and the path doesn't end with a slash, it is already canonical.
|
|
|
|
if (!path.contains("."sv) && !path.contains("//"sv) && !path.ends_with('/'))
|
|
|
|
return path;
|
|
|
|
|
|
|
|
auto is_absolute = path[0] == '/';
|
|
|
|
auto parts = path.split_view('/');
|
|
|
|
size_t approximate_canonical_length = 0;
|
2023-12-16 17:19:34 +03:00
|
|
|
Vector<ByteString> canonical_parts;
|
2021-06-29 21:26:52 +03:00
|
|
|
|
|
|
|
for (auto& part : parts) {
|
|
|
|
if (part == ".")
|
|
|
|
continue;
|
|
|
|
if (part == "..") {
|
|
|
|
if (canonical_parts.is_empty()) {
|
|
|
|
if (is_absolute) {
|
|
|
|
// At the root, .. does nothing.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (canonical_parts.last() != "..") {
|
|
|
|
// A .. and a previous non-.. part cancel each other.
|
|
|
|
canonical_parts.take_last();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
approximate_canonical_length += part.length() + 1;
|
|
|
|
canonical_parts.append(part);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canonical_parts.is_empty() && !is_absolute)
|
|
|
|
canonical_parts.append(".");
|
|
|
|
|
|
|
|
StringBuilder builder(approximate_canonical_length);
|
|
|
|
if (is_absolute)
|
|
|
|
builder.append('/');
|
|
|
|
builder.join('/', canonical_parts);
|
2023-12-16 17:19:34 +03:00
|
|
|
return builder.to_byte_string();
|
2019-07-15 07:49:28 +03:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString LexicalPath::absolute_path(ByteString dir_path, ByteString target)
|
2021-09-11 13:55:07 +03:00
|
|
|
{
|
|
|
|
if (LexicalPath(target).is_absolute()) {
|
|
|
|
return LexicalPath::canonicalized_path(target);
|
|
|
|
}
|
|
|
|
return LexicalPath::canonicalized_path(join(dir_path, target).string());
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString LexicalPath::relative_path(StringView a_path, StringView a_prefix)
|
2021-02-20 17:34:31 +03:00
|
|
|
{
|
2021-06-29 21:59:38 +03:00
|
|
|
if (!a_path.starts_with('/') || !a_prefix.starts_with('/')) {
|
2023-12-16 17:19:34 +03:00
|
|
|
// FIXME: This should probably VERIFY or return an Optional<ByteString>.
|
2022-07-11 20:32:29 +03:00
|
|
|
return ""sv;
|
2021-06-29 21:59:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a_path == a_prefix)
|
|
|
|
return ".";
|
2021-02-20 17:34:31 +03:00
|
|
|
|
2021-06-29 21:59:38 +03:00
|
|
|
// NOTE: Strip optional trailing slashes, except if the full path is only "/".
|
|
|
|
auto path = canonicalized_path(a_path);
|
|
|
|
auto prefix = canonicalized_path(a_prefix);
|
2021-02-20 17:34:31 +03:00
|
|
|
|
2021-06-29 21:59:38 +03:00
|
|
|
if (path == prefix)
|
|
|
|
return ".";
|
|
|
|
|
|
|
|
// NOTE: Handle this special case first.
|
|
|
|
if (prefix == "/"sv)
|
|
|
|
return path.substring_view(1);
|
|
|
|
|
|
|
|
// NOTE: This means the prefix is a direct child of the path.
|
|
|
|
if (path.starts_with(prefix) && path[prefix.length()] == '/') {
|
|
|
|
return path.substring_view(prefix.length() + 1);
|
|
|
|
}
|
2021-02-20 17:34:31 +03:00
|
|
|
|
2022-12-12 02:33:34 +03:00
|
|
|
auto path_parts = path.split_view('/');
|
|
|
|
auto prefix_parts = prefix.split_view('/');
|
|
|
|
size_t index_of_first_part_that_differs = 0;
|
|
|
|
for (; index_of_first_part_that_differs < path_parts.size() && index_of_first_part_that_differs < prefix_parts.size(); index_of_first_part_that_differs++) {
|
|
|
|
if (path_parts[index_of_first_part_that_differs] != prefix_parts[index_of_first_part_that_differs])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
for (size_t part_index = index_of_first_part_that_differs; part_index < prefix_parts.size(); part_index++) {
|
|
|
|
builder.append("../"sv);
|
|
|
|
}
|
|
|
|
for (size_t part_index = index_of_first_part_that_differs; part_index < path_parts.size(); part_index++) {
|
|
|
|
builder.append(path_parts[part_index]);
|
|
|
|
if (part_index != path_parts.size() - 1) // We don't need a slash after the file name or the name of the last directory
|
|
|
|
builder.append('/');
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
return builder.to_byte_string();
|
2021-02-20 17:34:31 +03:00
|
|
|
}
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
LexicalPath LexicalPath::append(StringView value) const
|
2021-05-12 22:17:39 +03:00
|
|
|
{
|
2021-06-29 18:55:12 +03:00
|
|
|
return LexicalPath::join(m_string, value);
|
|
|
|
}
|
2021-05-12 22:17:39 +03:00
|
|
|
|
2021-11-21 21:55:44 +03:00
|
|
|
LexicalPath LexicalPath::prepend(StringView value) const
|
|
|
|
{
|
|
|
|
return LexicalPath::join(value, m_string);
|
|
|
|
}
|
|
|
|
|
2021-06-29 18:55:12 +03:00
|
|
|
LexicalPath LexicalPath::parent() const
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
return append(".."sv);
|
2021-05-12 22:17:39 +03:00
|
|
|
}
|
|
|
|
|
2019-05-26 23:33:30 +03:00
|
|
|
}
|