mirror of
https://github.com/transmission/transmission
synced 2025-02-20 21:26:53 +00:00
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
This commit is contained in:
parent
da142f400e
commit
e7a229e6c3
5 changed files with 37 additions and 39 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()`.
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue