Skip to content

Commit 6666713

Browse files
author
MarcoFalke
committed
refactor: Rename fs::path::u8string() to fs::path::utf8string()
1 parent 856c887 commit 6666713

File tree

10 files changed

+29
-23
lines changed

10 files changed

+29
-23
lines changed

doc/developer-notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ A few guidelines for introducing and reviewing new RPC interfaces:
14061406

14071407
- *Rationale*: User-facing consistency.
14081408

1409-
- Use `fs::path::u8string()` and `fs::u8path()` functions when converting path
1409+
- Use `fs::path::u8string()`/`fs::path::utf8string()` and `fs::u8path()` functions when converting path
14101410
to JSON strings, not `fs::PathToString` and `fs::PathFromString`
14111411

14121412
- *Rationale*: JSON strings are Unicode strings, not byte strings, and

src/bench/wallet_create.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void WalletCreate(benchmark::Bench& bench, bool encrypted)
3434

3535
fs::path wallet_path = test_setup->m_path_root / strprintf("test_wallet_%d", random.rand32()).c_str();
3636
bench.run([&] {
37-
auto wallet = CreateWallet(context, wallet_path.u8string(), /*load_on_start=*/std::nullopt, options, status, error_string, warnings);
37+
auto wallet = CreateWallet(context, wallet_path.utf8string(), /*load_on_start=*/std::nullopt, options, status, error_string, warnings);
3838
assert(status == DatabaseStatus::SUCCESS);
3939
assert(wallet != nullptr);
4040

src/qt/guiutil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ fs::path QStringToPath(const QString &path)
669669

670670
QString PathToQString(const fs::path &path)
671671
{
672-
return QString::fromStdString(path.u8string());
672+
return QString::fromStdString(path.utf8string());
673673
}
674674

675675
QString NetworkToQString(Network net)

src/rpc/blockchain.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,7 +2603,7 @@ static RPCHelpMan dumptxoutset()
26032603
if (fs::exists(path)) {
26042604
throw JSONRPCError(
26052605
RPC_INVALID_PARAMETER,
2606-
path.u8string() + " already exists. If you are sure this is what you want, "
2606+
path.utf8string() + " already exists. If you are sure this is what you want, "
26072607
"move it out of the way first");
26082608
}
26092609

@@ -2612,15 +2612,15 @@ static RPCHelpMan dumptxoutset()
26122612
if (afile.IsNull()) {
26132613
throw JSONRPCError(
26142614
RPC_INVALID_PARAMETER,
2615-
"Couldn't open file " + temppath.u8string() + " for writing.");
2615+
"Couldn't open file " + temppath.utf8string() + " for writing.");
26162616
}
26172617

26182618
NodeContext& node = EnsureAnyNodeContext(request.context);
26192619
UniValue result = CreateUTXOSnapshot(
26202620
node, node.chainman->ActiveChainstate(), afile, path, temppath);
26212621
fs::rename(temppath, path);
26222622

2623-
result.pushKV("path", path.u8string());
2623+
result.pushKV("path", path.utf8string());
26242624
return result;
26252625
},
26262626
};
@@ -2692,7 +2692,7 @@ UniValue CreateUTXOSnapshot(
26922692
result.pushKV("coins_written", maybe_stats->coins_count);
26932693
result.pushKV("base_hash", tip->GetBlockHash().ToString());
26942694
result.pushKV("base_height", tip->nHeight);
2695-
result.pushKV("path", path.u8string());
2695+
result.pushKV("path", path.utf8string());
26962696
result.pushKV("txoutset_hash", maybe_stats->hashSerialized.ToString());
26972697
result.pushKV("nchaintx", tip->nChainTx);
26982698
return result;
@@ -2745,7 +2745,7 @@ static RPCHelpMan loadtxoutset()
27452745
if (afile.IsNull()) {
27462746
throw JSONRPCError(
27472747
RPC_INVALID_PARAMETER,
2748-
"Couldn't open file " + path.u8string() + " for reading.");
2748+
"Couldn't open file " + path.utf8string() + " for reading.");
27492749
}
27502750

27512751
SnapshotMetadata metadata;

src/rpc/mempool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ static RPCHelpMan savemempool()
806806
}
807807

808808
UniValue ret(UniValue::VOBJ);
809-
ret.pushKV("filename", dump_path.u8string());
809+
ret.pushKV("filename", dump_path.utf8string());
810810

811811
return ret;
812812
},

src/rpc/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static RPCHelpMan getrpcinfo()
243243
UniValue result(UniValue::VOBJ);
244244
result.pushKV("active_commands", active_commands);
245245

246-
const std::string path = LogInstance().m_file_path.u8string();
246+
const std::string path = LogInstance().m_file_path.utf8string();
247247
UniValue log_path(UniValue::VSTR, path);
248248
result.pushKV("logpath", log_path);
249249

src/test/fs_tests.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ BOOST_AUTO_TEST_CASE(fsbridge_pathtostring)
2020
std::string u8_str = "fs_tests_₿_🏃";
2121
std::u8string str8{u8"fs_tests_₿_🏃"};
2222
BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(u8_str)), u8_str);
23-
BOOST_CHECK_EQUAL(fs::u8path(u8_str).u8string(), u8_str);
24-
BOOST_CHECK_EQUAL(fs::path(str8).u8string(), u8_str);
25-
BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).u8string(), u8_str);
23+
BOOST_CHECK_EQUAL(fs::u8path(u8_str).utf8string(), u8_str);
24+
BOOST_CHECK_EQUAL(fs::path(str8).utf8string(), u8_str);
25+
BOOST_CHECK(fs::path(str8).u8string() == str8);
26+
BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).utf8string(), u8_str);
2627
BOOST_CHECK_EQUAL(fs::PathToString(fs::u8path(u8_str)), u8_str);
2728
#ifndef WIN32
2829
// On non-windows systems, verify that arbitrary byte strings containing

src/util/fs.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,15 @@ class path : public std::filesystem::path
5454
// Disallow std::string conversion method to avoid locale-dependent encoding on windows.
5555
std::string string() const = delete;
5656

57-
std::string u8string() const
57+
/**
58+
* Return a UTF-8 representation of the path as a std::string, for
59+
* compatibility with code using std::string. For code using the newer
60+
* std::u8string type, it is more efficient to call the inherited
61+
* std::filesystem::path::u8string method instead.
62+
*/
63+
std::string utf8string() const
5864
{
5965
const std::u8string& utf8_str{std::filesystem::path::u8string()};
60-
// Convert to std::string as a convenience for use in RPC code.
6166
return std::string{utf8_str.begin(), utf8_str.end()};
6267
}
6368

@@ -136,7 +141,7 @@ static inline bool copy_file(const path& from, const path& to, copy_options opti
136141
* Because \ref PathToString and \ref PathFromString functions don't specify an
137142
* encoding, they are meant to be used internally, not externally. They are not
138143
* appropriate to use in applications requiring UTF-8, where
139-
* fs::path::u8string() and fs::u8path() methods should be used instead. Other
144+
* fs::path::u8string() / fs::path::utf8string() and fs::u8path() methods should be used instead. Other
140145
* applications could require still different encodings. For example, JSON, XML,
141146
* or URI applications might prefer to use higher-level escapes (\uXXXX or
142147
* &XXXX; or %XX) instead of multibyte encoding. Rust, Python, Java applications
@@ -150,13 +155,13 @@ static inline std::string PathToString(const path& path)
150155
// use here, because these methods encode the path using C++'s narrow
151156
// multibyte encoding, which on Windows corresponds to the current "code
152157
// page", which is unpredictable and typically not able to represent all
153-
// valid paths. So fs::path::u8string() and
158+
// valid paths. So fs::path::utf8string() and
154159
// fs::u8path() functions are used instead on Windows. On
155-
// POSIX, u8string/u8path functions are not safe to use because paths are
160+
// POSIX, u8string/utf8string/u8path functions are not safe to use because paths are
156161
// not always valid UTF-8, so plain string methods which do not transform
157162
// the path there are used.
158163
#ifdef WIN32
159-
return path.u8string();
164+
return path.utf8string();
160165
#else
161166
static_assert(std::is_same<path::string_type, std::string>::value, "PathToString not implemented on this platform");
162167
return path.std::filesystem::path::string();

src/wallet/rpc/backup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ RPCHelpMan dumpwallet()
734734
* It may also avoid other security issues.
735735
*/
736736
if (fs::exists(filepath)) {
737-
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");
737+
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");
738738
}
739739

740740
std::ofstream file;
@@ -828,7 +828,7 @@ RPCHelpMan dumpwallet()
828828
file.close();
829829

830830
UniValue reply(UniValue::VOBJ);
831-
reply.pushKV("filename", filepath.u8string());
831+
reply.pushKV("filename", filepath.utf8string());
832832

833833
return reply;
834834
},

src/wallet/rpc/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static RPCHelpMan listwalletdir()
165165
UniValue wallets(UniValue::VARR);
166166
for (const auto& path : ListDatabases(GetWalletDir())) {
167167
UniValue wallet(UniValue::VOBJ);
168-
wallet.pushKV("name", path.u8string());
168+
wallet.pushKV("name", path.utf8string());
169169
wallets.push_back(wallet);
170170
}
171171

@@ -802,7 +802,7 @@ static RPCHelpMan migratewallet()
802802
if (res->solvables_wallet) {
803803
r.pushKV("solvables_name", res->solvables_wallet->GetName());
804804
}
805-
r.pushKV("backup_path", res->backup_path.u8string());
805+
r.pushKV("backup_path", res->backup_path.utf8string());
806806

807807
return r;
808808
},

0 commit comments

Comments
 (0)