1313#include < ios>
1414#include < ostream>
1515#include < string>
16+ #include < system_error>
17+ #include < type_traits>
1618#include < utility>
1719
1820/* * Filesystem operations and types */
@@ -34,7 +36,7 @@ class path : public std::filesystem::path
3436 // Allow path objects arguments for compatibility.
3537 path (std::filesystem::path path) : std::filesystem::path::path(std::move(path)) {}
3638 path& operator =(std::filesystem::path path) { std::filesystem::path::operator =(std::move (path)); return *this ; }
37- path& operator /=(std::filesystem::path path) { std::filesystem::path::operator /=(path); return *this ; }
39+ path& operator /=(const std::filesystem::path& path) { std::filesystem::path::operator /=(path); return *this ; }
3840
3941 // Allow literal string arguments, which are safe as long as the literals are ASCII.
4042 path (const char * c) : std::filesystem::path(c) {}
@@ -51,12 +53,15 @@ class path : public std::filesystem::path
5153 // Disallow std::string conversion method to avoid locale-dependent encoding on windows.
5254 std::string string () const = delete;
5355
54- std::string u8string () const
56+ /* *
57+ * Return a UTF-8 representation of the path as a std::string, for
58+ * compatibility with code using std::string. For code using the newer
59+ * std::u8string type, it is more efficient to call the inherited
60+ * std::filesystem::path::u8string method instead.
61+ */
62+ std::string utf8string () const
5563 {
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.
64+ const std::u8string& utf8_str{std::filesystem::path::u8string ()};
6065 return std::string{utf8_str.begin (), utf8_str.end ()};
6166 }
6267
@@ -68,11 +73,7 @@ class path : public std::filesystem::path
6873
6974static inline path u8path (const std::string& utf8_str)
7075{
71- #if __cplusplus < 202002L
72- return std::filesystem::u8path (utf8_str);
73- #else
7476 return std::filesystem::path (std::u8string{utf8_str.begin (), utf8_str.end ()});
75- #endif
7677}
7778
7879// Disallow implicit std::string conversion for absolute to avoid
@@ -96,9 +97,9 @@ static inline auto quoted(const std::string& s)
9697}
9798
9899// Allow safe path append operations.
99- static inline path operator /(path p1, path p2)
100+ static inline path operator /(path p1, const path& p2)
100101{
101- p1 /= std::move (p2) ;
102+ p1 /= p2 ;
102103 return p1;
103104}
104105static inline path operator /(path p1, const char * p2)
@@ -139,7 +140,7 @@ static inline bool copy_file(const path& from, const path& to, copy_options opti
139140 * Because \ref PathToString and \ref PathFromString functions don't specify an
140141 * encoding, they are meant to be used internally, not externally. They are not
141142 * appropriate to use in applications requiring UTF-8, where
142- * fs::path::u8string() and fs::u8path() methods should be used instead. Other
143+ * fs::path::u8string() / fs::path::utf8string() and fs::u8path() methods should be used instead. Other
143144 * applications could require still different encodings. For example, JSON, XML,
144145 * or URI applications might prefer to use higher-level escapes (\uXXXX or
145146 * &XXXX; or %XX) instead of multibyte encoding. Rust, Python, Java applications
@@ -153,13 +154,13 @@ static inline std::string PathToString(const path& path)
153154 // use here, because these methods encode the path using C++'s narrow
154155 // multibyte encoding, which on Windows corresponds to the current "code
155156 // page", which is unpredictable and typically not able to represent all
156- // valid paths. So fs::path::u8string () and
157+ // valid paths. So fs::path::utf8string () and
157158 // fs::u8path() functions are used instead on Windows. On
158- // POSIX, u8string/u8path functions are not safe to use because paths are
159+ // POSIX, u8string/utf8string/ u8path functions are not safe to use because paths are
159160 // not always valid UTF-8, so plain string methods which do not transform
160161 // the path there are used.
161162#ifdef WIN32
162- return path.u8string ();
163+ return path.utf8string ();
163164#else
164165 static_assert (std::is_same<path::string_type, std::string>::value, " PathToString not implemented on this platform" );
165166 return path.std ::filesystem::path::string ();
0 commit comments