mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-04 10:12:28 +02:00
Merge bitcoin/bitcoin#29040: refactor: Remove pre-C++20 code, fs::path cleanup
6666713041refactor: Rename fs::path::u8string() to fs::path::utf8string() (MarcoFalke)856c88776fArgsManager: return path by value from GetBlocksDirPath() (Vasil Dimov)fa3d9304e8refactor: Remove pre-C++20 fs code (MarcoFalke)fa00098e1aAdd tests for C++20 std::u8string (MarcoFalke)fa2bac08c2refactor: Avoid copy/move in fs.h (MarcoFalke)faea30227brefactor: Use C++20 std::chrono::days (MarcoFalke) Pull request description: This: * Removes dead code. * Avoids unused copies in some places. * Adds copies in other places for safety. ACKs for top commit: achow101: ACK6666713041ryanofsky: Code review ACK6666713041. Just documentation change since last review. stickies-v: re-ACK6666713041Tree-SHA512: 6176e44f30b310d51632ec2d3827c3819905d0ddc6a4b57acfcb6cfa1f9735176da75ee8ed4a4abd1296cb0b83bee9374cc6f91ffac87c19b63c435eeadf3f46
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2017-2022 The Bitcoin Core developers
|
||||
// Copyright (c) 2017-present The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#endif
|
||||
|
||||
#include <cassert>
|
||||
#include <cerrno>
|
||||
#include <string>
|
||||
|
||||
namespace fsbridge {
|
||||
@@ -130,4 +131,4 @@ std::string get_filesystem_error_message(const fs::filesystem_error& e)
|
||||
#endif
|
||||
}
|
||||
|
||||
} // fsbridge
|
||||
} // namespace fsbridge
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2017-2022 The Bitcoin Core developers
|
||||
// Copyright (c) 2017-present The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
#include <ios>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
/** Filesystem operations and types */
|
||||
@@ -35,7 +37,7 @@ public:
|
||||
// Allow path objects arguments for compatibility.
|
||||
path(std::filesystem::path path) : std::filesystem::path::path(std::move(path)) {}
|
||||
path& operator=(std::filesystem::path path) { std::filesystem::path::operator=(std::move(path)); return *this; }
|
||||
path& operator/=(std::filesystem::path path) { std::filesystem::path::operator/=(path); return *this; }
|
||||
path& operator/=(const std::filesystem::path& path) { std::filesystem::path::operator/=(path); return *this; }
|
||||
|
||||
// Allow literal string arguments, which are safe as long as the literals are ASCII.
|
||||
path(const char* c) : std::filesystem::path(c) {}
|
||||
@@ -52,12 +54,15 @@ public:
|
||||
// Disallow std::string conversion method to avoid locale-dependent encoding on windows.
|
||||
std::string string() const = delete;
|
||||
|
||||
std::string u8string() const
|
||||
/**
|
||||
* Return a UTF-8 representation of the path as a std::string, for
|
||||
* compatibility with code using std::string. For code using the newer
|
||||
* std::u8string type, it is more efficient to call the inherited
|
||||
* std::filesystem::path::u8string method instead.
|
||||
*/
|
||||
std::string utf8string() const
|
||||
{
|
||||
const auto& utf8_str{std::filesystem::path::u8string()};
|
||||
// utf8_str might either be std::string (C++17) or std::u8string
|
||||
// (C++20). Convert both to std::string. This method can be removed
|
||||
// after switching to C++20.
|
||||
const std::u8string& utf8_str{std::filesystem::path::u8string()};
|
||||
return std::string{utf8_str.begin(), utf8_str.end()};
|
||||
}
|
||||
|
||||
@@ -69,11 +74,7 @@ public:
|
||||
|
||||
static inline path u8path(const std::string& utf8_str)
|
||||
{
|
||||
#if __cplusplus < 202002L
|
||||
return std::filesystem::u8path(utf8_str);
|
||||
#else
|
||||
return std::filesystem::path(std::u8string{utf8_str.begin(), utf8_str.end()});
|
||||
#endif
|
||||
}
|
||||
|
||||
// Disallow implicit std::string conversion for absolute to avoid
|
||||
@@ -97,9 +98,9 @@ static inline auto quoted(const std::string& s)
|
||||
}
|
||||
|
||||
// Allow safe path append operations.
|
||||
static inline path operator/(path p1, path p2)
|
||||
static inline path operator/(path p1, const path& p2)
|
||||
{
|
||||
p1 /= std::move(p2);
|
||||
p1 /= p2;
|
||||
return p1;
|
||||
}
|
||||
static inline path operator/(path p1, const char* p2)
|
||||
@@ -140,7 +141,7 @@ static inline bool copy_file(const path& from, const path& to, copy_options opti
|
||||
* Because \ref PathToString and \ref PathFromString functions don't specify an
|
||||
* encoding, they are meant to be used internally, not externally. They are not
|
||||
* appropriate to use in applications requiring UTF-8, where
|
||||
* fs::path::u8string() and fs::u8path() methods should be used instead. Other
|
||||
* fs::path::u8string() / fs::path::utf8string() and fs::u8path() methods should be used instead. Other
|
||||
* applications could require still different encodings. For example, JSON, XML,
|
||||
* or URI applications might prefer to use higher-level escapes (\uXXXX or
|
||||
* &XXXX; or %XX) instead of multibyte encoding. Rust, Python, Java applications
|
||||
@@ -154,13 +155,13 @@ static inline std::string PathToString(const path& path)
|
||||
// use here, because these methods encode the path using C++'s narrow
|
||||
// multibyte encoding, which on Windows corresponds to the current "code
|
||||
// page", which is unpredictable and typically not able to represent all
|
||||
// valid paths. So fs::path::u8string() and
|
||||
// valid paths. So fs::path::utf8string() and
|
||||
// fs::u8path() functions are used instead on Windows. On
|
||||
// POSIX, u8string/u8path functions are not safe to use because paths are
|
||||
// POSIX, u8string/utf8string/u8path functions are not safe to use because paths are
|
||||
// not always valid UTF-8, so plain string methods which do not transform
|
||||
// the path there are used.
|
||||
#ifdef WIN32
|
||||
return path.u8string();
|
||||
return path.utf8string();
|
||||
#else
|
||||
static_assert(std::is_same<path::string_type, std::string>::value, "PathToString not implemented on this platform");
|
||||
return path.std::filesystem::path::string();
|
||||
|
||||
Reference in New Issue
Block a user