fs/AllocatedPath: pass std::string_view to FromUTF8()

This commit is contained in:
Max Kellermann
2020-04-03 15:55:19 +02:00
parent 56b4b010d6
commit 7a58b8c3e8
8 changed files with 16 additions and 19 deletions

View File

@@ -25,7 +25,7 @@
AllocatedPath::~AllocatedPath() noexcept = default;
AllocatedPath
AllocatedPath::FromUTF8(const char *path_utf8) noexcept
AllocatedPath::FromUTF8(std::string_view path_utf8) noexcept
{
#ifdef FS_CHARSET_ALWAYS_UTF8
return FromFS(path_utf8);
@@ -39,7 +39,7 @@ AllocatedPath::FromUTF8(const char *path_utf8) noexcept
}
AllocatedPath
AllocatedPath::FromUTF8Throw(const char *path_utf8)
AllocatedPath::FromUTF8Throw(std::string_view path_utf8)
{
#ifdef FS_CHARSET_ALWAYS_UTF8
return FromFS(path_utf8);

View File

@@ -173,15 +173,18 @@ public:
* Convert a UTF-8 C string to an #AllocatedPath instance.
* Returns return a "nulled" instance on error.
*/
gcc_pure gcc_nonnull_all
static AllocatedPath FromUTF8(const char *path_utf8) noexcept;
gcc_pure
static AllocatedPath FromUTF8(std::string_view path_utf8) noexcept;
static AllocatedPath FromUTF8(const char *path_utf8) noexcept {
return FromUTF8(std::string_view(path_utf8));
}
/**
* Convert a UTF-8 C string to an #AllocatedPath instance.
* Throws a std::runtime_error on error.
*/
gcc_nonnull_all
static AllocatedPath FromUTF8Throw(const char *path_utf8);
static AllocatedPath FromUTF8Throw(std::string_view path_utf8);
/**
* Copy an #AllocatedPath object.

View File

@@ -118,19 +118,14 @@ PathToUTF8(PathTraitsFS::const_pointer path_fs)
#if defined(HAVE_FS_CHARSET) || defined(_WIN32)
PathTraitsFS::string
PathFromUTF8(PathTraitsUTF8::const_pointer path_utf8)
PathFromUTF8(PathTraitsUTF8::string_view path_utf8)
{
#if !CLANG_CHECK_VERSION(3,6)
/* disabled on clang due to -Wtautological-pointer-compare */
assert(path_utf8 != nullptr);
#endif
#ifdef _WIN32
const auto buffer = MultiByteToWideChar(CP_UTF8, path_utf8);
return PathTraitsFS::string(buffer);
#else
if (fs_converter == nullptr)
return path_utf8;
return PathTraitsFS::string(path_utf8);
const auto buffer = fs_converter->FromUTF8(path_utf8);
return PathTraitsFS::string(buffer);

View File

@@ -53,8 +53,7 @@ PathToUTF8(PathTraitsFS::const_pointer path_fs);
*
* Throws std::runtime_error on error.
*/
gcc_nonnull_all
PathTraitsFS::string
PathFromUTF8(PathTraitsUTF8::const_pointer path_utf8);
PathFromUTF8(PathTraitsUTF8::string_view path_utf8);
#endif