summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorMax Wipfli <mail@maxwipfli.ch>2021-06-29 17:06:21 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-30 11:13:54 +0200
commit7405536a1abcc23d6a54565687800b14bde3db53 (patch)
treebb3e3affb873a170eebfb6d08a3abae9b08d4ffc /Userland
parentfc6d051dfdddc13835548537d025e760ba186b5b (diff)
downloadserenity-7405536a1abcc23d6a54565687800b14bde3db53.zip
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.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibCore/File.cpp9
-rw-r--r--Userland/Libraries/LibGUI/EmojiInputDialog.cpp4
-rw-r--r--Userland/Libraries/LibGUI/FileSystemModel.cpp7
-rw-r--r--Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp2
-rw-r--r--Userland/Utilities/basename.cpp2
-rw-r--r--Userland/Utilities/mkdir.cpp2
6 files changed, 14 insertions, 12 deletions
diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp
index 0a08414396..a8e62d05a0 100644
--- a/Userland/Libraries/LibCore/File.cpp
+++ b/Userland/Libraries/LibCore/File.cpp
@@ -271,16 +271,17 @@ static String get_duplicate_name(const String& path, int duplicate_count)
LexicalPath lexical_path(path);
StringBuilder duplicated_name;
duplicated_name.append('/');
- for (size_t i = 0; i < lexical_path.parts().size() - 1; ++i) {
- duplicated_name.appendff("{}/", lexical_path.parts()[i]);
+ auto& parts = lexical_path.parts_view();
+ for (size_t i = 0; i < parts.size() - 1; ++i) {
+ duplicated_name.appendff("{}/", parts[i]);
}
auto prev_duplicate_tag = String::formatted("({})", duplicate_count);
auto title = lexical_path.title();
if (title.ends_with(prev_duplicate_tag)) {
// remove the previous duplicate tag "(n)" so we can add a new tag.
- title = title.substring(0, title.length() - prev_duplicate_tag.length());
+ title = title.substring_view(0, title.length() - prev_duplicate_tag.length());
}
- duplicated_name.appendff("{} ({})", lexical_path.title(), duplicate_count);
+ duplicated_name.appendff("{} ({})", title, duplicate_count);
if (!lexical_path.extension().is_empty()) {
duplicated_name.appendff(".{}", lexical_path.extension());
}
diff --git a/Userland/Libraries/LibGUI/EmojiInputDialog.cpp b/Userland/Libraries/LibGUI/EmojiInputDialog.cpp
index 098a2ce1ab..7a25e701c3 100644
--- a/Userland/Libraries/LibGUI/EmojiInputDialog.cpp
+++ b/Userland/Libraries/LibGUI/EmojiInputDialog.cpp
@@ -26,10 +26,10 @@ static Vector<u32> supported_emoji_code_points()
auto lexical_path = LexicalPath(filename);
if (lexical_path.extension() != "png")
continue;
- auto basename = lexical_path.basename();
+ auto& basename = lexical_path.basename();
if (!basename.starts_with("U+"))
continue;
- u32 code_point = strtoul(basename.characters() + 2, nullptr, 16);
+ u32 code_point = strtoul(basename.to_string().characters() + 2, nullptr, 16);
code_points.append(code_point);
}
return code_points;
diff --git a/Userland/Libraries/LibGUI/FileSystemModel.cpp b/Userland/Libraries/LibGUI/FileSystemModel.cpp
index 71e08653f7..fe5f31a4be 100644
--- a/Userland/Libraries/LibGUI/FileSystemModel.cpp
+++ b/Userland/Libraries/LibGUI/FileSystemModel.cpp
@@ -202,15 +202,16 @@ FileSystemModel::Node const* FileSystemModel::node_for_path(String const& path)
if (lexical_path.string() == "/")
return node;
- for (size_t i = 0; i < lexical_path.parts().size(); ++i) {
- auto& part = lexical_path.parts()[i];
+ auto& parts = lexical_path.parts_view();
+ for (size_t i = 0; i < parts.size(); ++i) {
+ auto& part = parts[i];
bool found = false;
for (auto& child : node->children) {
if (child.name == part) {
const_cast<Node&>(child).reify_if_needed();
node = &child;
found = true;
- if (i == lexical_path.parts().size() - 1)
+ if (i == parts.size() - 1)
return node;
break;
}
diff --git a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp
index 31626bdb6f..9b5371a801 100644
--- a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp
+++ b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp
@@ -402,7 +402,7 @@ int main(int argc, char** argv)
}
LexicalPath lexical_path(path);
- auto namespace_ = lexical_path.parts().at(lexical_path.parts().size() - 2);
+ auto& namespace_ = lexical_path.parts_view().at(lexical_path.parts_view().size() - 2);
auto data = file_or_error.value()->read_all();
auto interface = IDL::parse_interface(path, data);
diff --git a/Userland/Utilities/basename.cpp b/Userland/Utilities/basename.cpp
index 4b6031715e..fa2b480f1d 100644
--- a/Userland/Utilities/basename.cpp
+++ b/Userland/Utilities/basename.cpp
@@ -27,7 +27,7 @@ int main(int argc, char** argv)
auto result = LexicalPath::basename(path);
if (!suffix.is_null() && result.length() != suffix.length() && result.ends_with(suffix))
- result = result.substring(0, result.length() - suffix.length());
+ result = result.substring_view(0, result.length() - suffix.length());
outln("{}", result);
return 0;
diff --git a/Userland/Utilities/mkdir.cpp b/Userland/Utilities/mkdir.cpp
index 05752966b9..74a02e2246 100644
--- a/Userland/Utilities/mkdir.cpp
+++ b/Userland/Utilities/mkdir.cpp
@@ -44,7 +44,7 @@ int main(int argc, char** argv)
StringBuilder path_builder;
if (lexical_path.is_absolute())
path_builder.append("/");
- for (auto& part : lexical_path.parts()) {
+ for (auto& part : lexical_path.parts_view()) {
path_builder.append(part);
auto path = path_builder.build();
struct stat st;