Skip to content

Commit fabb7c4

Browse files
author
MarcoFalke
committed
Make fs.h C++20 compliant
Without the changes, the file will fail to compile under C++20 because char8_t can not be converted to char implicitly.
1 parent fae2220 commit fabb7c4

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/fs.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,26 @@ class path : public std::filesystem::path
5151
// Disallow std::string conversion method to avoid locale-dependent encoding on windows.
5252
std::string string() const = delete;
5353

54+
std::string u8string() const
55+
{
56+
const auto& utf8_str{std::filesystem::path::u8string()};
57+
// utf8_str might either be std::string (C++17) or std::u8string
58+
// (C++20). Convert both to std::string. This method can be removed
59+
// after switching to C++20.
60+
return std::string{utf8_str.begin(), utf8_str.end()};
61+
}
62+
5463
// Required for path overloads in <fstream>.
5564
// See https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=96e0367ead5d8dcac3bec2865582e76e2fbab190
5665
path& make_preferred() { std::filesystem::path::make_preferred(); return *this; }
5766
path filename() const { return std::filesystem::path::filename(); }
5867
};
5968

69+
static inline path u8path(const std::string& utf8_str)
70+
{
71+
return std::filesystem::u8path(utf8_str);
72+
}
73+
6074
// Disallow implicit std::string conversion for absolute to avoid
6175
// locale-dependent encoding on windows.
6276
static inline path absolute(const path& p)
@@ -116,8 +130,8 @@ static inline std::string PathToString(const path& path)
116130
// use here, because these methods encode the path using C++'s narrow
117131
// multibyte encoding, which on Windows corresponds to the current "code
118132
// page", which is unpredictable and typically not able to represent all
119-
// valid paths. So std::filesystem::path::u8string() and
120-
// std::filesystem::u8path() functions are used instead on Windows. On
133+
// valid paths. So fs::path::u8string() and
134+
// fs::u8path() functions are used instead on Windows. On
121135
// POSIX, u8string/u8path functions are not safe to use because paths are
122136
// not always valid UTF-8, so plain string methods which do not transform
123137
// the path there are used.

0 commit comments

Comments
 (0)