Skip to content

Commit c3545a7

Browse files
committed
Merge bitcoin/bitcoin#22653: refactor: Rename JoinErrors and re-use it
bb56486 refactor: Reuse MakeUnorderedList where possible (Hennadii Stepanov) 77a90f0 refactor: Move MakeUnorderedList into util/string.h to make it reusable (Hennadii Stepanov) 6a5ccd6 scripted-diff: Rename JoinErrors in more general MakeUnorderedList (Hennadii Stepanov) Pull request description: A nice `JoinErrors` utility function was introduced in bitcoin-core#379 by Russell Yanofsky. This PR renames this function and re-uses it across the code base. ACKs for top commit: Zero-1729: Concept ACK bb56486 theStack: Code-review ACK bb56486 Talkless: utACK bb56486 ryanofsky: Code review ACK bb56486. Nice deduping, thanks for this! Tree-SHA512: 6bdbfa61f2ffa69e075f46b733f247c6d5b8486779a1dac064285a199a4bb8bc5ef44eaee37086305646b5c88eb6a11990883219a4a9140a5117ee21ed529bb9
2 parents 0b5344b + bb56486 commit c3545a7

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

src/qt/bitcoin.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <qt/utilitydialog.h>
2929
#include <qt/winshutdownmonitor.h>
3030
#include <uint256.h>
31+
#include <util/string.h>
3132
#include <util/system.h>
3233
#include <util/threadnames.h>
3334
#include <util/translation.h>
@@ -144,11 +145,6 @@ static void initTranslations(QTranslator &qtTranslatorBase, QTranslator &qtTrans
144145
QApplication::installTranslator(&translator);
145146
}
146147

147-
static std::string JoinErrors(const std::vector<std::string>& errors)
148-
{
149-
return Join(errors, "\n", [](const std::string& error) { return "- " + error; });
150-
}
151-
152148
static bool InitSettings()
153149
{
154150
if (!gArgs.GetSettingsPath()) {
@@ -158,13 +154,13 @@ static bool InitSettings()
158154
std::vector<std::string> errors;
159155
if (!gArgs.ReadSettingsFile(&errors)) {
160156
bilingual_str error = _("Settings file could not be read");
161-
InitError(Untranslated(strprintf("%s:\n%s\n", error.original, JoinErrors(errors))));
157+
InitError(Untranslated(strprintf("%s:\n%s\n", error.original, MakeUnorderedList(errors))));
162158

163159
QMessageBox messagebox(QMessageBox::Critical, PACKAGE_NAME, QString::fromStdString(strprintf("%s.", error.translated)), QMessageBox::Reset | QMessageBox::Abort);
164160
/*: Explanatory text shown on startup when the settings file cannot be read.
165161
Prompts user to make a choice between resetting or aborting. */
166162
messagebox.setInformativeText(QObject::tr("Do you want to reset settings to default values, or to abort without making changes?"));
167-
messagebox.setDetailedText(QString::fromStdString(JoinErrors(errors)));
163+
messagebox.setDetailedText(QString::fromStdString(MakeUnorderedList(errors)));
168164
messagebox.setTextFormat(Qt::PlainText);
169165
messagebox.setDefaultButton(QMessageBox::Reset);
170166
switch (messagebox.exec()) {
@@ -180,14 +176,14 @@ static bool InitSettings()
180176
errors.clear();
181177
if (!gArgs.WriteSettingsFile(&errors)) {
182178
bilingual_str error = _("Settings file could not be written");
183-
InitError(Untranslated(strprintf("%s:\n%s\n", error.original, JoinErrors(errors))));
179+
InitError(Untranslated(strprintf("%s:\n%s\n", error.original, MakeUnorderedList(errors))));
184180

185181
QMessageBox messagebox(QMessageBox::Critical, PACKAGE_NAME, QString::fromStdString(strprintf("%s.", error.translated)), QMessageBox::Ok);
186182
/*: Explanatory text shown on startup when the settings file could not be written.
187183
Prompts user to check that we have the ability to write to the file.
188184
Explains that the user has the option of running without a settings file.*/
189-
messagebox.setInformativeText(QObject::tr("A fatal error occured. Check that settings file is writable, or try running with -nosettings."));
190-
messagebox.setDetailedText(QString::fromStdString(JoinErrors(errors)));
185+
messagebox.setInformativeText(QObject::tr("A fatal error occurred. Check that settings file is writable, or try running with -nosettings."));
186+
messagebox.setDetailedText(QString::fromStdString(MakeUnorderedList(errors)));
191187
messagebox.setTextFormat(Qt::PlainText);
192188
messagebox.setDefaultButton(QMessageBox::Ok);
193189
messagebox.exec();

src/rpc/blockchain.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <txmempool.h>
3737
#include <undo.h>
3838
#include <util/strencodings.h>
39+
#include <util/string.h>
3940
#include <util/system.h>
4041
#include <util/translation.h>
4142
#include <validation.h>
@@ -1328,7 +1329,7 @@ static RPCHelpMan verifychain()
13281329
"\nVerifies blockchain database.\n",
13291330
{
13301331
{"checklevel", RPCArg::Type::NUM, RPCArg::DefaultHint{strprintf("%d, range=0-4", DEFAULT_CHECKLEVEL)},
1331-
strprintf("How thorough the block verification is:\n - %s", Join(CHECKLEVEL_DOC, "\n- "))},
1332+
strprintf("How thorough the block verification is:\n%s", MakeUnorderedList(CHECKLEVEL_DOC))},
13321333
{"nblocks", RPCArg::Type::NUM, RPCArg::DefaultHint{strprintf("%d, 0=all", DEFAULT_CHECKBLOCKS)}, "The number of blocks to check."},
13331334
},
13341335
RPCResult{

src/util/string.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ inline std::string Join(const std::vector<std::string>& list, const std::string&
6464
return Join<std::string>(list, separator);
6565
}
6666

67+
/**
68+
* Create an unordered multi-line list of items.
69+
*/
70+
inline std::string MakeUnorderedList(const std::vector<std::string>& items)
71+
{
72+
return Join(items, "\n", [](const std::string& item) { return "- " + item; });
73+
}
74+
6775
/**
6876
* Check if a string does not contain any embedded NUL (\0) characters
6977
*/

src/util/system.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,11 @@ bool ArgsManager::InitSettings(std::string& error)
502502

503503
std::vector<std::string> errors;
504504
if (!ReadSettingsFile(&errors)) {
505-
error = strprintf("Failed loading settings file:\n- %s\n", Join(errors, "\n- "));
505+
error = strprintf("Failed loading settings file:\n%s\n", MakeUnorderedList(errors));
506506
return false;
507507
}
508508
if (!WriteSettingsFile(&errors)) {
509-
error = strprintf("Failed saving settings file:\n- %s\n", Join(errors, "\n- "));
509+
error = strprintf("Failed saving settings file:\n%s\n", MakeUnorderedList(errors));
510510
return false;
511511
}
512512
return true;

0 commit comments

Comments
 (0)