summaryrefslogtreecommitdiff
path: root/Userland/Utilities
diff options
context:
space:
mode:
authorMax Wipfli <mail@maxwipfli.ch>2021-06-29 16:46:16 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-30 11:13:54 +0200
commitfc6d051dfdddc13835548537d025e760ba186b5b (patch)
tree12a5602dd4efc20f023fea95d390f3e241f6f9cf /Userland/Utilities
parent9b8f35259c375f6911b76c38ec37e6d422b26bbe (diff)
downloadserenity-fc6d051dfdddc13835548537d025e760ba186b5b.zip
AK+Everywhere: Add and use static APIs for LexicalPath
The LexicalPath instance methods dirname(), basename(), title() and extension() will be changed to return StringView const& in a further commit. Due to this, users creating temporary LexicalPath objects just to call one of those getters will recieve a StringView const& pointing to a possible freed buffer. To avoid this, static methods for those APIs have been added, which will return a String by value to avoid those problems. All cases where temporary LexicalPath objects have been used as described above haven been changed to use the static APIs.
Diffstat (limited to 'Userland/Utilities')
-rw-r--r--Userland/Utilities/basename.cpp2
-rw-r--r--Userland/Utilities/bt.cpp2
-rw-r--r--Userland/Utilities/cp.cpp2
-rw-r--r--Userland/Utilities/dirname.cpp2
-rw-r--r--Userland/Utilities/du.cpp2
-rw-r--r--Userland/Utilities/ln.cpp2
-rw-r--r--Userland/Utilities/mv.cpp2
-rw-r--r--Userland/Utilities/test.cpp2
-rw-r--r--Userland/Utilities/tree.cpp2
9 files changed, 9 insertions, 9 deletions
diff --git a/Userland/Utilities/basename.cpp b/Userland/Utilities/basename.cpp
index ee54a200f8..4b6031715e 100644
--- a/Userland/Utilities/basename.cpp
+++ b/Userland/Utilities/basename.cpp
@@ -24,7 +24,7 @@ int main(int argc, char** argv)
args_parser.add_positional_argument(suffix, "Suffix to strip from name", "suffix", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
- auto result = LexicalPath(path).basename();
+ 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());
diff --git a/Userland/Utilities/bt.cpp b/Userland/Utilities/bt.cpp
index 18d56bdc00..c291894dbc 100644
--- a/Userland/Utilities/bt.cpp
+++ b/Userland/Utilities/bt.cpp
@@ -65,7 +65,7 @@ int main(int argc, char** argv)
out("\033]8;;{}\033\\", url.serialize());
}
- out("\033[34;1m{}:{}\033[0m", LexicalPath(source_position.file_path).basename(), source_position.line_number);
+ out("\033[34;1m{}:{}\033[0m", LexicalPath::basename(source_position.file_path), source_position.line_number);
if (linked)
out("\033]8;;\033\\");
diff --git a/Userland/Utilities/cp.cpp b/Userland/Utilities/cp.cpp
index 86a08e6877..3f6803e674 100644
--- a/Userland/Utilities/cp.cpp
+++ b/Userland/Utilities/cp.cpp
@@ -36,7 +36,7 @@ int main(int argc, char** argv)
for (auto& source : sources) {
auto destination_path = destination_is_existing_dir
- ? String::formatted("{}/{}", destination, LexicalPath(source).basename())
+ ? String::formatted("{}/{}", destination, LexicalPath::basename(source))
: destination;
auto result = Core::File::copy_file_or_directory(
diff --git a/Userland/Utilities/dirname.cpp b/Userland/Utilities/dirname.cpp
index 05edc7d551..313c532a5a 100644
--- a/Userland/Utilities/dirname.cpp
+++ b/Userland/Utilities/dirname.cpp
@@ -14,6 +14,6 @@ int main(int argc, char** argv)
args_parser.add_positional_argument(path, "Path", "path");
args_parser.parse(argc, argv);
- outln("{}", LexicalPath(path).dirname());
+ outln("{}", LexicalPath::dirname(path));
return 0;
}
diff --git a/Userland/Utilities/du.cpp b/Userland/Utilities/du.cpp
index 0034ef9714..eb007193e0 100644
--- a/Userland/Utilities/du.cpp
+++ b/Userland/Utilities/du.cpp
@@ -151,7 +151,7 @@ int print_space_usage(const String& path, const DuOption& du_option, int max_dep
}
}
- const auto basename = LexicalPath(path).basename();
+ const auto basename = LexicalPath::basename(path);
for (const auto& pattern : du_option.excluded_patterns) {
if (basename.matches(pattern, CaseSensitivity::CaseSensitive))
return 0;
diff --git a/Userland/Utilities/ln.cpp b/Userland/Utilities/ln.cpp
index 7525e042c9..ece88fb947 100644
--- a/Userland/Utilities/ln.cpp
+++ b/Userland/Utilities/ln.cpp
@@ -30,7 +30,7 @@ int main(int argc, char** argv)
String path_buffer;
if (!path) {
- path_buffer = LexicalPath(target).basename();
+ path_buffer = LexicalPath::basename(target);
path = path_buffer.characters();
}
diff --git a/Userland/Utilities/mv.cpp b/Userland/Utilities/mv.cpp
index 6962a6a3e7..1e917f69b7 100644
--- a/Userland/Utilities/mv.cpp
+++ b/Userland/Utilities/mv.cpp
@@ -59,7 +59,7 @@ int main(int argc, char** argv)
String combined_new_path;
const char* new_path = original_new_path;
if (S_ISDIR(st.st_mode)) {
- auto old_basename = LexicalPath(old_path).basename();
+ auto old_basename = LexicalPath::basename(old_path);
combined_new_path = String::formatted("{}/{}", original_new_path, old_basename);
new_path = combined_new_path.characters();
}
diff --git a/Userland/Utilities/test.cpp b/Userland/Utilities/test.cpp
index aea9d67c7b..a868348411 100644
--- a/Userland/Utilities/test.cpp
+++ b/Userland/Utilities/test.cpp
@@ -498,7 +498,7 @@ int main(int argc, char* argv[])
return 126;
}
- if (LexicalPath { argv[0] }.basename() == "[") {
+ if (LexicalPath::basename(argv[0]) == "[") {
--argc;
if (StringView { argv[argc] } != "]")
fatal_error("test invoked as '[' requires a closing bracket ']'");
diff --git a/Userland/Utilities/tree.cpp b/Userland/Utilities/tree.cpp
index a6a3f2c78b..e31522b2a2 100644
--- a/Userland/Utilities/tree.cpp
+++ b/Userland/Utilities/tree.cpp
@@ -36,7 +36,7 @@ static void print_directory_tree(const String& root_path, int depth, const Strin
out("{}|-- ", root_indent_string);
}
- String root_dir_name = LexicalPath(root_path).basename();
+ String root_dir_name = LexicalPath::basename(root_path);
out("\033[34;1m{}\033[0m\n", root_dir_name);
if (depth >= max_depth) {