1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:34:57 +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.
This commit is contained in:
Max Wipfli 2021-06-29 17:06:21 +02:00 committed by Andreas Kling
parent fc6d051dfd
commit 7405536a1a
10 changed files with 98 additions and 79 deletions

View file

@ -12,85 +12,102 @@
namespace AK {
char s_single_dot = '.';
LexicalPath::LexicalPath(String s)
: m_string(move(s))
{
canonicalize();
}
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;
}
void LexicalPath::canonicalize()
{
// NOTE: We never allow an empty m_string, if it's empty, we just set it to '.'.
if (m_string.is_empty()) {
m_string = ".";
m_dirname = m_string;
m_basename = {};
m_title = {};
m_extension = {};
m_parts.clear();
return;
}
m_is_absolute = m_string[0] == '/';
auto parts = m_string.split_view('/');
// 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;
size_t approximate_canonical_length = 0;
Vector<String> canonical_parts;
for (size_t i = 0; i < parts.size(); ++i) {
auto& part = parts[i];
if (part == ".")
continue;
if (part == "..") {
if (canonical_parts.is_empty()) {
if (m_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;
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;
}
}
}
}
if (!part.is_empty()) {
approximate_canonical_length += part.length() + 1;
canonical_parts.append(part);
}
}
if (canonical_parts.is_empty()) {
m_string = m_basename = m_dirname = m_is_absolute ? "/" : ".";
return;
if (canonical_parts.is_empty() && !is_absolute())
canonical_parts.append(".");
StringBuilder builder(approximate_canonical_length);
if (is_absolute())
builder.append('/');
builder.join('/', canonical_parts);
m_string = builder.to_string();
}
StringBuilder dirname_builder(approximate_canonical_length);
for (size_t i = 0; i < canonical_parts.size() - 1; ++i) {
auto& canonical_part = canonical_parts[i];
if (m_is_absolute || i != 0)
dirname_builder.append('/');
dirname_builder.append(canonical_part);
}
m_dirname = dirname_builder.to_string();
m_parts = m_string.split_view('/');
if (m_dirname.is_empty()) {
m_dirname = m_is_absolute ? "/" : ".";
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);
} else {
m_dirname = m_string.substring_view(0, *last_slash_index);
}
m_basename = canonical_parts.last();
if (m_string == "/")
m_basename = m_string;
else {
VERIFY(m_parts.size() > 0);
m_basename = m_parts.last();
}
Optional<size_t> last_dot = StringView(m_basename).find_last_of('.');
if (last_dot.has_value()) {
m_title = m_basename.substring(0, last_dot.value());
m_extension = m_basename.substring(last_dot.value() + 1, m_basename.length() - last_dot.value() - 1);
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 = {};
}
StringBuilder builder(approximate_canonical_length);
for (size_t i = 0; i < canonical_parts.size(); ++i) {
auto& canonical_part = canonical_parts[i];
if (m_is_absolute || i != 0)
builder.append('/');
builder.append(canonical_part);
}
m_parts = move(canonical_parts);
m_string = builder.to_string();
}
bool LexicalPath::has_extension(StringView const& extension) const