From e7a229e6c321bb290a3f4b816affbdd91fa69ef6 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 15 Nov 2021 18:50:14 -0600 Subject: [PATCH] refactor: tr_sys_path_dirname(), tr_sys_path_basename() now take std::string_view parameters (#2177) * refactor tr_sys_path_dirname(), tr_sys_path_basename() now take std::string_views --- libtransmission/file-posix.cc | 12 +++---- libtransmission/file-win32.cc | 52 ++++++++++++++++-------------- libtransmission/file.h | 4 +-- libtransmission/platform.cc | 6 ++-- tests/libtransmission/file-test.cc | 2 +- 5 files changed, 37 insertions(+), 39 deletions(-) diff --git a/libtransmission/file-posix.cc b/libtransmission/file-posix.cc index 112d51619..7e58f100f 100644 --- a/libtransmission/file-posix.cc +++ b/libtransmission/file-posix.cc @@ -378,11 +378,9 @@ char* tr_sys_path_resolve(char const* path, tr_error** error) return ret; } -char* tr_sys_path_basename(char const* path, tr_error** error) +char* tr_sys_path_basename(std::string_view path, tr_error** error) { - TR_ASSERT(path != nullptr); - - char* const tmp = tr_strdup(path); + char* const tmp = tr_strvDup(path); char* ret = basename(tmp); if (ret != nullptr) @@ -399,11 +397,9 @@ char* tr_sys_path_basename(char const* path, tr_error** error) return ret; } -char* tr_sys_path_dirname(char const* path, tr_error** error) +char* tr_sys_path_dirname(std::string_view path, tr_error** error) { - TR_ASSERT(path != nullptr); - - char* const tmp = tr_strdup(path); + char* const tmp = tr_strvDup(path); char* ret = dirname(tmp); if (ret != nullptr) diff --git a/libtransmission/file-win32.cc b/libtransmission/file-win32.cc index bf9b887ec..27f43baed 100644 --- a/libtransmission/file-win32.cc +++ b/libtransmission/file-win32.cc @@ -18,6 +18,8 @@ #include "tr-assert.h" #include "utils.h" +using namespace std::literals; + #ifndef MAXSIZE_T #define MAXSIZE_T ((SIZE_T) ~((SIZE_T)0)) #endif @@ -121,7 +123,7 @@ static constexpr bool is_unc_path(std::string_view path) return std::size(path) >= 2 && is_slash(path[0]) && path[1] == path[0]; } -static bool is_valid_path(char const* path) +static bool is_valid_path(std::string_view path) { if (is_unc_path(path)) { @@ -132,20 +134,20 @@ static bool is_valid_path(char const* path) } else { - char const* colon_pos = strchr(path, ':'); + auto pos = path.find(':'); - if (colon_pos != nullptr) + if (pos != path.npos) { - if (colon_pos != path + 1 || !isalpha(path[0])) + if (pos != 1 || !isalpha(path[0])) { return false; } - path += 2; + path.remove_prefix(2); } } - return strpbrk(path, "<>:\"|?*") == nullptr; + return path.find_first_of("<>:\"|?*"sv) == path.npos; } static wchar_t* path_to_native_path_ex(char const* path, int extra_chars_after, int* real_result_size) @@ -623,9 +625,9 @@ cleanup: return ret; } -char* tr_sys_path_basename(char const* path, tr_error** error) +char* tr_sys_path_basename(std::string_view path, tr_error** error) { - if (tr_str_is_empty(path)) + if (std::empty(path)) { return tr_strdup("."); } @@ -636,21 +638,22 @@ char* tr_sys_path_basename(char const* path, tr_error** error) return nullptr; } - char const* end = path + strlen(path); + char const* const begin = std::data(path); + char const* end = begin + std::size(path); - while (end > path && is_slash(*(end - 1))) + while (end > begin && is_slash(*(end - 1))) { --end; } - if (end == path) + if (end == begin) { return tr_strdup("/"); } char const* name = end; - while (name > path && *(name - 1) != ':' && !is_slash(*(name - 1))) + while (name > begin && *(name - 1) != ':' && !is_slash(*(name - 1))) { --name; } @@ -663,9 +666,9 @@ char* tr_sys_path_basename(char const* path, tr_error** error) return tr_strndup(name, end - name); } -char* tr_sys_path_dirname(char const* path, tr_error** error) +char* tr_sys_path_dirname(std::string_view path, tr_error** error) { - if (tr_str_is_empty(path)) + if (std::empty(path)) { return tr_strdup("."); } @@ -680,44 +683,45 @@ char* tr_sys_path_dirname(char const* path, tr_error** error) if (is_unc && path[2] == '\0') { - return tr_strdup(path); + return tr_strvDup(path); } - char const* end = path + strlen(path); + char const* const begin = std::data(path); + char const* end = begin + std::size(path); - while (end > path && is_slash(*(end - 1))) + while (end > begin && is_slash(*(end - 1))) { --end; } - if (end == path) + if (end == begin) { return tr_strdup("/"); } char const* name = end; - while (name > path && *(name - 1) != ':' && !is_slash(*(name - 1))) + while (name > begin && *(name - 1) != ':' && !is_slash(*(name - 1))) { --name; } - while (name > path && is_slash(*(name - 1))) + while (name > begin && is_slash(*(name - 1))) { --name; } - if (name == path) + if (name == begin) { return tr_strdup(is_unc ? "\\\\" : "."); } - if (name > path && *(name - 1) == ':' && *name != '\0' && !is_slash(*name)) + if (name > begin && *(name - 1) == ':' && *name != '\0' && !is_slash(*name)) { - return tr_strdup_printf("%c:.", path[0]); + return tr_strdup_printf("%c:.", begin[0]); } - return tr_strndup(path, name - path); + return tr_strndup(begin, name - begin); } bool tr_sys_path_rename(char const* src_path, char const* dst_path, tr_error** error) diff --git a/libtransmission/file.h b/libtransmission/file.h index 70fd31af8..91c275f2b 100644 --- a/libtransmission/file.h +++ b/libtransmission/file.h @@ -225,7 +225,7 @@ char* tr_sys_path_resolve(char const* path, struct tr_error** error); * it when no longer needed), `nullptr` otherwise (with `error` set * accordingly). */ -char* tr_sys_path_basename(char const* path, struct tr_error** error); +char* tr_sys_path_basename(std::string_view path, struct tr_error** error); /** * @brief Portability wrapper for `dirname()`. @@ -239,7 +239,7 @@ char* tr_sys_path_basename(char const* path, struct tr_error** error); * when no longer needed), `nullptr` otherwise (with `error` set * accordingly). */ -char* tr_sys_path_dirname(char const* path, struct tr_error** error); +char* tr_sys_path_dirname(std::string_view path, struct tr_error** error); /** * @brief Portability wrapper for `rename()`. diff --git a/libtransmission/platform.cc b/libtransmission/platform.cc index efba13b76..52b21b963 100644 --- a/libtransmission/platform.cc +++ b/libtransmission/platform.cc @@ -543,11 +543,9 @@ char const* tr_getWebClientDir([[maybe_unused]] tr_session const* session) if (s == nullptr) /* check calling module place */ { wchar_t wide_module_path[MAX_PATH]; - char* module_path; - char* dir; GetModuleFileNameW(nullptr, wide_module_path, TR_N_ELEMENTS(wide_module_path)); - module_path = tr_win32_native_to_utf8(wide_module_path, -1); - dir = tr_sys_path_dirname(module_path, nullptr); + char* module_path = tr_win32_native_to_utf8(wide_module_path, -1); + char* dir = tr_sys_path_dirname(module_path, nullptr); tr_free(module_path); if (dir != nullptr) diff --git a/tests/libtransmission/file-test.cc b/tests/libtransmission/file-test.cc index 73ab44299..59785bb3a 100644 --- a/tests/libtransmission/file-test.cc +++ b/tests/libtransmission/file-test.cc @@ -159,7 +159,7 @@ protected: char const* output; }; - void testPathXname(XnameTestData const* data, size_t data_size, char* (*func)(char const*, tr_error**)) + void testPathXname(XnameTestData const* data, size_t data_size, char* (*func)(std::string_view, tr_error**)) { for (size_t i = 0; i < data_size; ++i) {