diff options
| author | Hato <108814993+Hato1125@users.noreply.github.com> | 2025-08-18 04:18:51 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-17 20:18:51 +0100 |
| commit | d8901786109dba6af3eac03c1e723f807ed0117a (patch) | |
| tree | 05690a0ee6ad28f5b76a5b03a54b141a81167250 /hyprtester/src/main.cpp | |
| parent | bca96a5d3be8f3b02171a031511848d031ffa4b0 (diff) | |
internal: reference command-line arguments instead of copying them (#11422)
Eliminates the need to copy command-line arguments into a std::vector<std::string>, reducing memory usage and improving performance by referencing the original arguments directly.
Diffstat (limited to 'hyprtester/src/main.cpp')
| -rw-r--r-- | hyprtester/src/main.cpp | 137 |
1 files changed, 73 insertions, 64 deletions
diff --git a/hyprtester/src/main.cpp b/hyprtester/src/main.cpp index 2fa27fa8..f7f2c198 100644 --- a/hyprtester/src/main.cpp +++ b/hyprtester/src/main.cpp @@ -22,11 +22,16 @@ #include <filesystem> #include <hyprutils/os/Process.hpp> #include <hyprutils/memory/WeakPtr.hpp> +#include <hyprutils/memory/Casts.hpp> +using namespace Hyprutils::Memory; + #include <csignal> #include <cerrno> #include <chrono> #include <thread> #include <print> +#include <string_view> +#include <span> #include "Log.hpp" @@ -87,97 +92,101 @@ static void help() { int main(int argc, char** argv, char** envp) { - std::string configPath = ""; - std::string binaryPath = ""; - std::string pluginPath = std::filesystem::current_path().string(); + std::string configPath = ""; + std::string binaryPath = ""; + std::string pluginPath = std::filesystem::current_path().string(); - std::vector<std::string> args{argv + 1, argv + argc}; + if (argc > 1) { + std::span<char*> args{argv + 1, sc<std::size_t>(argc - 1)}; - for (auto it = args.begin(); it != args.end(); it++) { - if (*it == "--config" || *it == "-c") { - if (std::next(it) == args.end()) { - help(); + for (auto it = args.begin(); it != args.end(); it++) { + std::string_view value = *it; - return 1; - } + if (value == "--config" || value == "-c") { + if (std::next(it) == args.end()) { + help(); + + return 1; + } + + configPath = *std::next(it); - configPath = *std::next(it); + try { + configPath = std::filesystem::canonical(configPath); - try { - configPath = std::filesystem::canonical(configPath); + if (!std::filesystem::is_regular_file(configPath)) { + throw std::exception(); + } + } catch (...) { + std::println(stderr, "[ ERROR ] Config file '{}' doesn't exist!", configPath); + help(); - if (!std::filesystem::is_regular_file(configPath)) { - throw std::exception(); + return 1; } - } catch (...) { - std::println(stderr, "[ ERROR ] Config file '{}' doesn't exist!", configPath); - help(); - return 1; - } + it++; - it++; + continue; + } else if (value == "--binary" || value == "-b") { + if (std::next(it) == args.end()) { + help(); - continue; - } else if (*it == "--binary" || *it == "-b") { - if (std::next(it) == args.end()) { - help(); + return 1; + } - return 1; - } + binaryPath = *std::next(it); - binaryPath = *std::next(it); + try { + binaryPath = std::filesystem::canonical(binaryPath); - try { - binaryPath = std::filesystem::canonical(binaryPath); + if (!std::filesystem::is_regular_file(binaryPath)) { + throw std::exception(); + } + } catch (...) { + std::println(stderr, "[ ERROR ] Binary '{}' doesn't exist!", binaryPath); + help(); - if (!std::filesystem::is_regular_file(binaryPath)) { - throw std::exception(); + return 1; } - } catch (...) { - std::println(stderr, "[ ERROR ] Binary '{}' doesn't exist!", binaryPath); - help(); - return 1; - } + it++; - it++; + continue; + } else if (value == "--plugin" || value == "-p") { + if (std::next(it) == args.end()) { + help(); - continue; - } else if (*it == "--plugin" || *it == "-p") { - if (std::next(it) == args.end()) { - help(); + return 1; + } - return 1; - } + pluginPath = *std::next(it); - pluginPath = *std::next(it); + try { + pluginPath = std::filesystem::canonical(pluginPath); - try { - pluginPath = std::filesystem::canonical(pluginPath); + if (!std::filesystem::is_regular_file(pluginPath)) { + throw std::exception(); + } + } catch (...) { + std::println(stderr, "[ ERROR ] plugin '{}' doesn't exist!", pluginPath); + help(); - if (!std::filesystem::is_regular_file(pluginPath)) { - throw std::exception(); + return 1; } - } catch (...) { - std::println(stderr, "[ ERROR ] plugin '{}' doesn't exist!", pluginPath); - help(); - return 1; - } + it++; - it++; - - continue; - } else if (*it == "--help" || *it == "-h") { - help(); + continue; + } else if (value == "--help" || value == "-h") { + help(); - return 0; - } else { - std::println(stderr, "[ ERROR ] Unknown option '{}' !", *it); - help(); + return 0; + } else { + std::println(stderr, "[ ERROR ] Unknown option '{}' !", *it); + help(); - return 1; + return 1; + } } } |
