@@ -51,12 +51,26 @@ class path : public std::filesystem::path
51
51
// Disallow std::string conversion method to avoid locale-dependent encoding on windows.
52
52
std::string string () const = delete;
53
53
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
+
54
63
// Required for path overloads in <fstream>.
55
64
// See https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=96e0367ead5d8dcac3bec2865582e76e2fbab190
56
65
path& make_preferred () { std::filesystem::path::make_preferred (); return *this ; }
57
66
path filename () const { return std::filesystem::path::filename (); }
58
67
};
59
68
69
+ static inline path u8path (const std::string& utf8_str)
70
+ {
71
+ return std::filesystem::u8path (utf8_str);
72
+ }
73
+
60
74
// Disallow implicit std::string conversion for absolute to avoid
61
75
// locale-dependent encoding on windows.
62
76
static inline path absolute (const path& p)
@@ -116,8 +130,8 @@ static inline std::string PathToString(const path& path)
116
130
// use here, because these methods encode the path using C++'s narrow
117
131
// multibyte encoding, which on Windows corresponds to the current "code
118
132
// 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
121
135
// POSIX, u8string/u8path functions are not safe to use because paths are
122
136
// not always valid UTF-8, so plain string methods which do not transform
123
137
// the path there are used.
0 commit comments