1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-31 19:34:05 +00:00

refactor: tr_sys_path_is_relative() takes a string_view (#2142)

* refactor: tr_sys_path_is_relative() takes a string_view
This commit is contained in:
Charles Kerr 2021-11-13 10:08:50 -06:00 committed by GitHub
parent e144ad6da8
commit 86f4e37571
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 12 deletions

View file

@ -321,11 +321,9 @@ bool tr_sys_path_get_info(char const* path, int flags, tr_sys_path_info* info, t
return ret;
}
bool tr_sys_path_is_relative(char const* path)
bool tr_sys_path_is_relative(std::string_view path)
{
TR_ASSERT(path != nullptr);
return path[0] != '/';
return std::empty(path) || path.front() != '/';
}
bool tr_sys_path_is_same(char const* path1, char const* path2, tr_error** error)

View file

@ -116,9 +116,9 @@ static constexpr bool is_slash(char c)
return c == '\\' || c == '/';
}
static constexpr bool is_unc_path(char const* path)
static constexpr bool is_unc_path(std::string_view path)
{
return is_slash(path[0]) && path[1] == path[0];
return std::size(path) >= 2 && is_slash(path[0]) && path[1] == path[0];
}
static bool is_valid_path(char const* path)
@ -150,6 +150,11 @@ static bool is_valid_path(char const* path)
static wchar_t* path_to_native_path_ex(char const* path, int extra_chars_after, int* real_result_size)
{
if (path == nullptr)
{
return nullptr;
}
/* Extending maximum path length limit up to ~32K. See "Naming Files, Paths, and Namespaces"
(https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx) for more info */
@ -458,18 +463,22 @@ bool tr_sys_path_get_info(char const* path, int flags, tr_sys_path_info* info, t
return ret;
}
bool tr_sys_path_is_relative(char const* path)
bool tr_sys_path_is_relative(std::string_view path)
{
TR_ASSERT(path != nullptr);
/* UNC path: `\\...`. */
if (is_unc_path(path))
{
return false;
}
/* Local path: `X:` or `X:\...`. */
if (isalpha(path[0]) && path[1] == ':' && (path[2] == '\0' || is_slash(path[2])))
/* Local path: `X:` */
if (std::size(path) == 2 && isalpha(path[0]) && path[1] == ':')
{
return false;
}
/* Local path: `X:\...`. */
if (std::size(path) > 2 && isalpha(path[0]) && path[1] == ':' && is_slash(path[2]))
{
return false;
}

View file

@ -8,6 +8,7 @@
#pragma once
#include <string_view>
#include <inttypes.h>
#include <time.h>
@ -181,7 +182,7 @@ bool tr_sys_path_exists(char const* path, struct tr_error** error);
*
* @return `True` if path is relative, `false` otherwise
*/
bool tr_sys_path_is_relative(char const* path);
bool tr_sys_path_is_relative(std::string_view path);
/**
* @brief Test to see if the two filenames point to the same file.