2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-06-29 11:11:03 +00:00
|
|
|
* Copyright (c) 2021, Max Wipfli <max.wipfli@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-26 11:52:44 +00:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-03-23 12:45:10 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
#include <AK/Vector.h>
|
2018-10-28 07:54:20 +00: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 15:06:21 +00:00
|
|
|
char s_single_dot = '.';
|
|
|
|
|
2021-04-16 21:44:06 +00:00
|
|
|
LexicalPath::LexicalPath(String s)
|
|
|
|
: m_string(move(s))
|
2018-10-28 07:54:20 +00:00
|
|
|
{
|
2019-07-15 04:49:28 +00:00
|
|
|
canonicalize();
|
2018-10-28 07:54:20 +00: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 15:06:21 +00:00
|
|
|
Vector<String> LexicalPath::parts() const
|
|
|
|
{
|
|
|
|
Vector<String> vector;
|
|
|
|
vector.ensure_capacity(m_parts.size());
|
|
|
|
for (auto& part : m_parts)
|
|
|
|
vector.unchecked_append(part);
|
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:52:44 +00:00
|
|
|
void LexicalPath::canonicalize()
|
2018-10-28 07:54:20 +00: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 15:06:21 +00:00
|
|
|
// NOTE: We never allow an empty m_string, if it's empty, we just set it to '.'.
|
2019-08-23 17:55:51 +00: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 15:06:21 +00:00
|
|
|
m_string = ".";
|
|
|
|
m_dirname = m_string;
|
|
|
|
m_basename = {};
|
|
|
|
m_title = {};
|
|
|
|
m_extension = {};
|
2019-08-23 17:55:51 +00: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 15:06:21 +00:00
|
|
|
// NOTE: If there are no dots, no '//' and the path doesn't end with a slash, it is already canonical.
|
|
|
|
if (m_string.contains("."sv) || m_string.contains("//"sv) || m_string.ends_with('/')) {
|
|
|
|
auto parts = m_string.split_view('/');
|
|
|
|
size_t approximate_canonical_length = 0;
|
|
|
|
Vector<String> canonical_parts;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-05-22 17:50:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-15 04:49:28 +00:00
|
|
|
approximate_canonical_length += part.length() + 1;
|
2018-12-03 00:38:22 +00:00
|
|
|
canonical_parts.append(part);
|
2019-07-15 04:49:28 +00:00
|
|
|
}
|
2018-11-18 13:57:41 +00: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 15:06:21 +00:00
|
|
|
if (canonical_parts.is_empty() && !is_absolute())
|
|
|
|
canonical_parts.append(".");
|
2020-01-07 11:35:45 +00: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 15:06:21 +00:00
|
|
|
StringBuilder builder(approximate_canonical_length);
|
|
|
|
if (is_absolute())
|
|
|
|
builder.append('/');
|
|
|
|
builder.join('/', canonical_parts);
|
|
|
|
m_string = builder.to_string();
|
2021-05-27 11:52:13 +00: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 15:06:21 +00:00
|
|
|
m_parts = m_string.split_view('/');
|
2020-07-15 16:25:23 +00: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 15:06:21 +00:00
|
|
|
auto last_slash_index = m_string.view().find_last_of('/');
|
|
|
|
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 17:37:00 +00: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 15:06:21 +00:00
|
|
|
m_dirname = m_string.substring_view(0, *last_slash_index);
|
2020-07-12 17:37:00 +00:00
|
|
|
}
|
2019-07-29 04:45:50 +00: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 15:06:21 +00:00
|
|
|
if (m_string == "/")
|
|
|
|
m_basename = m_string;
|
|
|
|
else {
|
|
|
|
VERIFY(m_parts.size() > 0);
|
|
|
|
m_basename = m_parts.last();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto last_dot_index = m_basename.find_last_of('.');
|
|
|
|
// 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 07:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 11:01:56 +00:00
|
|
|
bool LexicalPath::has_extension(StringView const& extension) const
|
2019-05-26 20:33:30 +00:00
|
|
|
{
|
2020-05-26 09:12:18 +00:00
|
|
|
return m_string.ends_with(extension, CaseSensitivity::CaseInsensitive);
|
2018-10-28 07:54:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 21:44:06 +00:00
|
|
|
String LexicalPath::canonicalized_path(String path)
|
2019-07-15 04:49:28 +00:00
|
|
|
{
|
2021-04-16 21:44:06 +00:00
|
|
|
return LexicalPath(move(path)).string();
|
2019-07-15 04:49:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 11:01:56 +00:00
|
|
|
String LexicalPath::relative_path(String absolute_path, String const& prefix)
|
2021-02-20 14:34:31 +00:00
|
|
|
{
|
|
|
|
if (!LexicalPath { absolute_path }.is_absolute() || !LexicalPath { prefix }.is_absolute())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (!absolute_path.starts_with(prefix))
|
|
|
|
return absolute_path;
|
|
|
|
|
2021-04-03 22:48:26 +00:00
|
|
|
size_t prefix_length = LexicalPath { prefix }.string().length();
|
|
|
|
if (prefix != "/")
|
|
|
|
prefix_length++;
|
2021-02-20 14:34:31 +00:00
|
|
|
if (prefix_length >= absolute_path.length())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return absolute_path.substring(prefix_length);
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:55:12 +00:00
|
|
|
LexicalPath LexicalPath::append(StringView const& value) const
|
2021-05-12 19:17:39 +00:00
|
|
|
{
|
2021-06-29 15:55:12 +00:00
|
|
|
return LexicalPath::join(m_string, value);
|
|
|
|
}
|
2021-05-12 19:17:39 +00:00
|
|
|
|
2021-06-29 15:55:12 +00:00
|
|
|
LexicalPath LexicalPath::parent() const
|
|
|
|
{
|
|
|
|
return append("..");
|
2021-05-12 19:17:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 20:33:30 +00:00
|
|
|
}
|