From 0ef34878de670f068091fb2ed51556a1467ba383 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 24 Dec 2023 12:01:49 -0600 Subject: [PATCH] refactor: remove tr_str_is_empty() (#6432) * refactor: do not u se tr_str_is_empty() in tr_torrentSetLocation() * refactor: do not use tr_str_is_empty() in remote.cc * refactor: do not use tr_str_is_empty() in subprocess-win32 * refactor: remove tr_str_is_empty() --- libtransmission/subprocess-win32.cc | 4 +++- libtransmission/torrent.cc | 3 ++- libtransmission/utils.h | 5 ----- utils/remote.cc | 29 +++++++++++++++++------------ 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/libtransmission/subprocess-win32.cc b/libtransmission/subprocess-win32.cc index 46b6a45d7..d347d267c 100644 --- a/libtransmission/subprocess-win32.cc +++ b/libtransmission/subprocess-win32.cc @@ -144,12 +144,14 @@ auto get_current_env() void append_argument(std::string& arguments, char const* argument) { + TR_ASSERT(argument != nullptr); + if (!std::empty(arguments)) { arguments += ' '; } - if (!tr_str_is_empty(argument) && strpbrk(argument, " \t\n\v\"") == nullptr) + if (*argument != '\0' && strpbrk(argument, " \t\n\v\"") == nullptr) { arguments += argument; return; diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index 126c60200..635d71ecb 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -1182,7 +1182,8 @@ void tr_torrent::set_location(std::string_view location, bool move_from_old_path void tr_torrentSetLocation(tr_torrent* tor, char const* location, bool move_from_old_path, int volatile* setme_state) { TR_ASSERT(tr_isTorrent(tor)); - TR_ASSERT(!tr_str_is_empty(location)); + TR_ASSERT(location != nullptr); + TR_ASSERT(*location != '\0'); tor->set_location(location, move_from_old_path, setme_state); } diff --git a/libtransmission/utils.h b/libtransmission/utils.h index f374da76f..e80590f5c 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -100,11 +100,6 @@ int tr_main_win32(int argc, char** argv, int (*real_main)(int, char**)); // --- -[[nodiscard]] constexpr bool tr_str_is_empty(char const* value) -{ - return value == nullptr || *value == '\0'; -} - /** @brief Portability wrapper for `strlcpy()` that uses the system implementation if available */ size_t tr_strlcpy(void* dst, void const* src, size_t siz); diff --git a/utils/remote.cc b/utils/remote.cc index 3931e4e07..cb0aaf741 100644 --- a/utils/remote.cc +++ b/utils/remote.cc @@ -679,23 +679,28 @@ static void setGroup(tr_variant* args, std::string_view group) tr_variantDictAddStrView(args, TR_KEY_group, group); } -static void addFiles(tr_variant* args, tr_quark const key, char const* arg) +[[nodiscard]] auto make_files_list(char const* const str_in) { - tr_variant* files = tr_variantDictAddList(args, key, 100); + auto str = std::string_view{ str_in != nullptr ? str_in : "" }; - if (tr_str_is_empty(arg)) + if (std::empty(str)) { fmt::print(stderr, "No files specified!\n"); - arg = "-1"; /* no file will have this index, so should be a no-op */ + str = "-1"sv; // no file will have this index, so should be a no-op } - if (strcmp(arg, "all") != 0) + auto files = tr_variant::Vector{}; + + if (str != "all"sv) { - for (auto const& idx : tr_num_parse_range(arg)) + files.reserve(100U); + for (auto const& idx : tr_num_parse_range(str)) { - tr_variantListAddInt(files, idx); + files.emplace_back(idx); } } + + return files; } // clang-format off @@ -2935,11 +2940,11 @@ static int processArgs(char const* rpcurl, int argc, char const* const* argv, Re switch (c) { case 'g': - addFiles(args, TR_KEY_files_wanted, optarg); + *tr_variantDictAdd(args, TR_KEY_files_wanted) = make_files_list(optarg); break; case 'G': - addFiles(args, TR_KEY_files_unwanted, optarg); + *tr_variantDictAdd(args, TR_KEY_files_unwanted) = make_files_list(optarg); break; case 'L': @@ -2955,15 +2960,15 @@ static int processArgs(char const* rpcurl, int argc, char const* const* argv, Re break; case 900: - addFiles(args, TR_KEY_priority_high, optarg); + *tr_variantDictAdd(args, TR_KEY_priority_high) = make_files_list(optarg); break; case 901: - addFiles(args, TR_KEY_priority_normal, optarg); + *tr_variantDictAdd(args, TR_KEY_priority_normal) = make_files_list(optarg); break; case 902: - addFiles(args, TR_KEY_priority_low, optarg); + *tr_variantDictAdd(args, TR_KEY_priority_low) = make_files_list(optarg); break; case 700: