refactor: Rename fs::path::u8string() to fs::path::utf8string()

This commit is contained in:
MarcoFalke 2023-12-14 13:23:32 +01:00
parent 856c88776f
commit 6666713041
No known key found for this signature in database
10 changed files with 29 additions and 23 deletions

View file

@ -1406,7 +1406,7 @@ A few guidelines for introducing and reviewing new RPC interfaces:
- *Rationale*: User-facing consistency. - *Rationale*: User-facing consistency.
- Use `fs::path::u8string()` and `fs::u8path()` functions when converting path - Use `fs::path::u8string()`/`fs::path::utf8string()` and `fs::u8path()` functions when converting path
to JSON strings, not `fs::PathToString` and `fs::PathFromString` to JSON strings, not `fs::PathToString` and `fs::PathFromString`
- *Rationale*: JSON strings are Unicode strings, not byte strings, and - *Rationale*: JSON strings are Unicode strings, not byte strings, and

View file

@ -34,7 +34,7 @@ static void WalletCreate(benchmark::Bench& bench, bool encrypted)
fs::path wallet_path = test_setup->m_path_root / strprintf("test_wallet_%d", random.rand32()).c_str(); fs::path wallet_path = test_setup->m_path_root / strprintf("test_wallet_%d", random.rand32()).c_str();
bench.run([&] { bench.run([&] {
auto wallet = CreateWallet(context, wallet_path.u8string(), /*load_on_start=*/std::nullopt, options, status, error_string, warnings); auto wallet = CreateWallet(context, wallet_path.utf8string(), /*load_on_start=*/std::nullopt, options, status, error_string, warnings);
assert(status == DatabaseStatus::SUCCESS); assert(status == DatabaseStatus::SUCCESS);
assert(wallet != nullptr); assert(wallet != nullptr);

View file

@ -669,7 +669,7 @@ fs::path QStringToPath(const QString &path)
QString PathToQString(const fs::path &path) QString PathToQString(const fs::path &path)
{ {
return QString::fromStdString(path.u8string()); return QString::fromStdString(path.utf8string());
} }
QString NetworkToQString(Network net) QString NetworkToQString(Network net)

View file

@ -2603,7 +2603,7 @@ static RPCHelpMan dumptxoutset()
if (fs::exists(path)) { if (fs::exists(path)) {
throw JSONRPCError( throw JSONRPCError(
RPC_INVALID_PARAMETER, RPC_INVALID_PARAMETER,
path.u8string() + " already exists. If you are sure this is what you want, " path.utf8string() + " already exists. If you are sure this is what you want, "
"move it out of the way first"); "move it out of the way first");
} }
@ -2612,7 +2612,7 @@ static RPCHelpMan dumptxoutset()
if (afile.IsNull()) { if (afile.IsNull()) {
throw JSONRPCError( throw JSONRPCError(
RPC_INVALID_PARAMETER, RPC_INVALID_PARAMETER,
"Couldn't open file " + temppath.u8string() + " for writing."); "Couldn't open file " + temppath.utf8string() + " for writing.");
} }
NodeContext& node = EnsureAnyNodeContext(request.context); NodeContext& node = EnsureAnyNodeContext(request.context);
@ -2620,7 +2620,7 @@ static RPCHelpMan dumptxoutset()
node, node.chainman->ActiveChainstate(), afile, path, temppath); node, node.chainman->ActiveChainstate(), afile, path, temppath);
fs::rename(temppath, path); fs::rename(temppath, path);
result.pushKV("path", path.u8string()); result.pushKV("path", path.utf8string());
return result; return result;
}, },
}; };
@ -2692,7 +2692,7 @@ UniValue CreateUTXOSnapshot(
result.pushKV("coins_written", maybe_stats->coins_count); result.pushKV("coins_written", maybe_stats->coins_count);
result.pushKV("base_hash", tip->GetBlockHash().ToString()); result.pushKV("base_hash", tip->GetBlockHash().ToString());
result.pushKV("base_height", tip->nHeight); result.pushKV("base_height", tip->nHeight);
result.pushKV("path", path.u8string()); result.pushKV("path", path.utf8string());
result.pushKV("txoutset_hash", maybe_stats->hashSerialized.ToString()); result.pushKV("txoutset_hash", maybe_stats->hashSerialized.ToString());
result.pushKV("nchaintx", tip->nChainTx); result.pushKV("nchaintx", tip->nChainTx);
return result; return result;
@ -2745,7 +2745,7 @@ static RPCHelpMan loadtxoutset()
if (afile.IsNull()) { if (afile.IsNull()) {
throw JSONRPCError( throw JSONRPCError(
RPC_INVALID_PARAMETER, RPC_INVALID_PARAMETER,
"Couldn't open file " + path.u8string() + " for reading."); "Couldn't open file " + path.utf8string() + " for reading.");
} }
SnapshotMetadata metadata; SnapshotMetadata metadata;

View file

@ -806,7 +806,7 @@ static RPCHelpMan savemempool()
} }
UniValue ret(UniValue::VOBJ); UniValue ret(UniValue::VOBJ);
ret.pushKV("filename", dump_path.u8string()); ret.pushKV("filename", dump_path.utf8string());
return ret; return ret;
}, },

View file

@ -243,7 +243,7 @@ static RPCHelpMan getrpcinfo()
UniValue result(UniValue::VOBJ); UniValue result(UniValue::VOBJ);
result.pushKV("active_commands", active_commands); result.pushKV("active_commands", active_commands);
const std::string path = LogInstance().m_file_path.u8string(); const std::string path = LogInstance().m_file_path.utf8string();
UniValue log_path(UniValue::VSTR, path); UniValue log_path(UniValue::VSTR, path);
result.pushKV("logpath", log_path); result.pushKV("logpath", log_path);

View file

@ -20,9 +20,10 @@ BOOST_AUTO_TEST_CASE(fsbridge_pathtostring)
std::string u8_str = "fs_tests_₿_🏃"; std::string u8_str = "fs_tests_₿_🏃";
std::u8string str8{u8"fs_tests_₿_🏃"}; std::u8string str8{u8"fs_tests_₿_🏃"};
BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(u8_str)), u8_str); BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(u8_str)), u8_str);
BOOST_CHECK_EQUAL(fs::u8path(u8_str).u8string(), u8_str); BOOST_CHECK_EQUAL(fs::u8path(u8_str).utf8string(), u8_str);
BOOST_CHECK_EQUAL(fs::path(str8).u8string(), u8_str); BOOST_CHECK_EQUAL(fs::path(str8).utf8string(), u8_str);
BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).u8string(), u8_str); BOOST_CHECK(fs::path(str8).u8string() == str8);
BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).utf8string(), u8_str);
BOOST_CHECK_EQUAL(fs::PathToString(fs::u8path(u8_str)), u8_str); BOOST_CHECK_EQUAL(fs::PathToString(fs::u8path(u8_str)), u8_str);
#ifndef WIN32 #ifndef WIN32
// On non-windows systems, verify that arbitrary byte strings containing // On non-windows systems, verify that arbitrary byte strings containing

View file

@ -54,10 +54,15 @@ public:
// Disallow std::string conversion method to avoid locale-dependent encoding on windows. // Disallow std::string conversion method to avoid locale-dependent encoding on windows.
std::string string() const = delete; 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 std::u8string& utf8_str{std::filesystem::path::u8string()}; const std::u8string& utf8_str{std::filesystem::path::u8string()};
// Convert to std::string as a convenience for use in RPC code.
return std::string{utf8_str.begin(), utf8_str.end()}; return std::string{utf8_str.begin(), utf8_str.end()};
} }
@ -136,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 * Because \ref PathToString and \ref PathFromString functions don't specify an
* encoding, they are meant to be used internally, not externally. They are not * encoding, they are meant to be used internally, not externally. They are not
* appropriate to use in applications requiring UTF-8, where * 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, * applications could require still different encodings. For example, JSON, XML,
* or URI applications might prefer to use higher-level escapes (\uXXXX or * or URI applications might prefer to use higher-level escapes (\uXXXX or
* &XXXX; or %XX) instead of multibyte encoding. Rust, Python, Java applications * &XXXX; or %XX) instead of multibyte encoding. Rust, Python, Java applications
@ -150,13 +155,13 @@ static inline std::string PathToString(const path& path)
// use here, because these methods encode the path using C++'s narrow // use here, because these methods encode the path using C++'s narrow
// multibyte encoding, which on Windows corresponds to the current "code // multibyte encoding, which on Windows corresponds to the current "code
// page", which is unpredictable and typically not able to represent all // 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 // 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 // not always valid UTF-8, so plain string methods which do not transform
// the path there are used. // the path there are used.
#ifdef WIN32 #ifdef WIN32
return path.u8string(); return path.utf8string();
#else #else
static_assert(std::is_same<path::string_type, std::string>::value, "PathToString not implemented on this platform"); static_assert(std::is_same<path::string_type, std::string>::value, "PathToString not implemented on this platform");
return path.std::filesystem::path::string(); return path.std::filesystem::path::string();

View file

@ -734,7 +734,7 @@ RPCHelpMan dumpwallet()
* It may also avoid other security issues. * It may also avoid other security issues.
*/ */
if (fs::exists(filepath)) { if (fs::exists(filepath)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, filepath.u8string() + " already exists. If you are sure this is what you want, move it out of the way first"); throw JSONRPCError(RPC_INVALID_PARAMETER, filepath.utf8string() + " already exists. If you are sure this is what you want, move it out of the way first");
} }
std::ofstream file; std::ofstream file;
@ -828,7 +828,7 @@ RPCHelpMan dumpwallet()
file.close(); file.close();
UniValue reply(UniValue::VOBJ); UniValue reply(UniValue::VOBJ);
reply.pushKV("filename", filepath.u8string()); reply.pushKV("filename", filepath.utf8string());
return reply; return reply;
}, },

View file

@ -165,7 +165,7 @@ static RPCHelpMan listwalletdir()
UniValue wallets(UniValue::VARR); UniValue wallets(UniValue::VARR);
for (const auto& path : ListDatabases(GetWalletDir())) { for (const auto& path : ListDatabases(GetWalletDir())) {
UniValue wallet(UniValue::VOBJ); UniValue wallet(UniValue::VOBJ);
wallet.pushKV("name", path.u8string()); wallet.pushKV("name", path.utf8string());
wallets.push_back(wallet); wallets.push_back(wallet);
} }
@ -802,7 +802,7 @@ static RPCHelpMan migratewallet()
if (res->solvables_wallet) { if (res->solvables_wallet) {
r.pushKV("solvables_name", res->solvables_wallet->GetName()); r.pushKV("solvables_name", res->solvables_wallet->GetName());
} }
r.pushKV("backup_path", res->backup_path.u8string()); r.pushKV("backup_path", res->backup_path.utf8string());
return r; return r;
}, },