Skip to content

Commit ac4aeb2

Browse files
committed
Use std::format instead of custom string formatting
1 parent 29f4119 commit ac4aeb2

File tree

92 files changed

+250
-360
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+250
-360
lines changed

Common/Cpp/Options/FloatingPointOption.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,10 @@ JsonValue FloatingPointCell::to_json() const{
121121
std::string FloatingPointCell::check_validity(double x) const{
122122
const Data& data = *m_data;
123123
if (x < data.m_min_value){
124-
std::ostringstream ss;
125-
return "Value too small: min = " + tostr_default(data.m_min_value) + ", value = " + tostr_default(x);
124+
return std::format("Value too small: min = {}, value = {}", data.m_min_value, x);
126125
}
127126
if (x > data.m_max_value){
128-
std::ostringstream ss;
129-
return "Value too large: max = " + tostr_default(data.m_max_value) + ", value = " + tostr_default(x);
127+
return std::format("Value too large: max = {}, value = {}", data.m_max_value, x);
130128
}
131129
if (std::isnan(x)){
132130
return "Value is NaN.";

Common/Cpp/Options/TimeDurationOption.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ std::string TimeDurationCell<Type>::time_string(const std::string& text) const{
259259
if constexpr (self >= milliseconds){
260260
return duration_to_string(std::chrono::duration_cast<Milliseconds>(value));
261261
}else{
262-
return tostr_u_commas(value.count()) + " " + m_data->m_units;
262+
return std::format("{:L} {}", value.count(), m_data->m_units);
263263
}
264264
}
265265

Common/Cpp/Options/TimeExpressionOption.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ std::string ticks_to_time(double ticks_per_second, int64_t ticks){
2525
const double DAY = HOUR * 24;
2626

2727
std::string str;
28-
str += tostr_u_commas(ticks);
28+
str += std::format("{:L}", ticks);
2929
str += " tick";
3030
// Compute absolute value of the ticks:
3131
uint64_t abs_ticks = 0;
@@ -44,17 +44,13 @@ std::string ticks_to_time(double ticks_per_second, int64_t ticks){
4444
str += "-";
4545
}
4646
if (abs_ticks < MINUTE * 2){
47-
str += tostr_fixed((double)abs_ticks / SECOND, 3);
48-
str += " seconds";
47+
str += std::format("{:.3f} seconds", (double)abs_ticks / SECOND);
4948
}else if (abs_ticks < HOUR * 2){
50-
str += tostr_fixed((double)abs_ticks / MINUTE, 3);
51-
str += " minutes";
49+
str += std::format("{:.3f} minutes", (double)abs_ticks / MINUTE);
5250
}else if (abs_ticks < DAY * 2){
53-
str += tostr_fixed((double)abs_ticks / HOUR, 3);
54-
str += " hours";
51+
str += std::format("{:.3f} hours", (double)abs_ticks / HOUR);
5552
}else{
56-
str += tostr_fixed((double)abs_ticks / DAY, 3);
57-
str += " days";
53+
str += std::format("{:.3f} days", (double)abs_ticks / DAY);
5854
}
5955
str += ")";
6056
return str;

Common/Cpp/PrettyPrint.cpp

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,6 @@ std::string tostr_padded(size_t digits, uint64_t x){
2222
}
2323
return std::string(digits - str.size(), '0') + str;
2424
}
25-
std::string tostr_u_commas(int64_t x){
26-
// Prints out x with comma separators.
27-
28-
std::string str = std::to_string(x);
29-
std::string out;
30-
31-
const char* ptr = str.c_str();
32-
// len: how many digits, don't count "-" in the negative numbers
33-
size_t len = str.size() - (x < 0);
34-
35-
size_t commas = (len + 2) / 3 - 1;
36-
size_t shift = len - commas * 3 + (x < 0);
37-
38-
while (true){
39-
char ch = *ptr++;
40-
if (ch == '\0')
41-
break;
42-
if (shift == 0){
43-
out += ',';
44-
shift = 3;
45-
}
46-
out += ch;
47-
shift--;
48-
}
49-
50-
return out;
51-
}
5225

5326

5427
inline std::string byte_prefix(size_t index){
@@ -164,22 +137,6 @@ std::string tostr_bytes(uint64_t bytes){
164137
return tostr_ui_bytes(bytes);
165138
}
166139

167-
168-
169-
std::string tostr_default(double x){
170-
std::ostringstream ss;
171-
ss << x;
172-
return ss.str();
173-
}
174-
std::string tostr_fixed(double x, int precision){
175-
std::ostringstream out;
176-
out << std::setprecision(precision);
177-
out << std::fixed;
178-
out << x;
179-
return out.str();
180-
}
181-
182-
183140
std::string now_to_filestring(){
184141
#if _WIN32 && _MSC_VER
185142
#pragma warning(disable:4996)
@@ -244,30 +201,21 @@ std::string duration_to_string(std::chrono::milliseconds milliseconds){
244201

245202
uint64_t ticks = milliseconds.count();
246203

247-
std::string str;
248204
if (ticks < MINUTE * 2){
249-
str += tostr_fixed((double)ticks / SECOND, 3);
250-
str += " seconds";
205+
return std::format("{:.3f} seconds", (double)ticks / SECOND);
251206
}else if (ticks < HOUR * 2){
252-
str += tostr_fixed((double)ticks / MINUTE, 3);
253-
str += " minutes";
207+
return std::format("{:.3f} minutes", (double)ticks / MINUTE);
254208
}else if (ticks < DAY * 2){
255-
str += tostr_fixed((double)ticks / HOUR, 3);
256-
str += " hours";
209+
return std::format("{:.3f} hours", (double)ticks / HOUR);
257210
}else if (ticks < WEEK * 2){
258-
str += tostr_fixed((double)ticks / DAY, 3);
259-
str += " days";
211+
return std::format("{:.3f} days", (double)ticks / DAY);
260212
}else if (ticks < YEARS * 2){
261-
str += tostr_fixed((double)ticks / WEEK, 3);
262-
str += " weeks";
213+
return std::format("{:.3f} weeks", (double)ticks / WEEK);
263214
}else if (ticks < MILLION_YEARS){
264-
str += tostr_fixed((double)ticks / YEARS, 3);
265-
str += " years";
215+
return std::format("{:.3f} years", (double)ticks / YEARS);
266216
}else{
267-
str += tostr_fixed((double)ticks / MILLION_YEARS, 3);
268-
str += " million years";
217+
return std::format("{:.3f} million years", (double)ticks / MILLION_YEARS);
269218
}
270-
return str;
271219
}
272220

273221

Common/Cpp/PrettyPrint.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,13 @@
1010
#include <string>
1111
#include <set>
1212
#include <chrono>
13+
#include <format>
1314

1415
namespace PokemonAutomation{
1516

1617
std::string tostr_padded(size_t digits, uint64_t x);
17-
std::string tostr_u_commas(int64_t x);
1818
std::string tostr_bytes(uint64_t bytes);
1919

20-
// Convert double to string using the default precision on ostream.
21-
std::string tostr_default(double x);
22-
// Convert double to string with fixed precision.
23-
// The precision specifies the number of digits after the decimal point.
24-
std::string tostr_fixed(double x, int precision);
25-
2620
// Format current time to a string to be used as filenames.
2721
// e.g. "20220320-044444408355"
2822
std::string now_to_filestring();

SerialPrograms/Source/CommonFramework/AudioPipeline/AudioInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ std::vector<AudioDeviceInfo> AudioDeviceInfo::all_input_devices(){
370370

371371
WallClock end = current_time();
372372
double seconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.;
373-
global_logger_tagged().log("Done querying audio inputs... " + tostr_fixed(seconds, 3) + " seconds", COLOR_CYAN);
373+
global_logger_tagged().log(std::format("Done querying audio inputs... {:.3f} seconds", seconds), COLOR_CYAN);
374374

375375
bool show_all_devices = GlobalSettings::instance().AUDIO_PIPELINE->SHOW_ALL_DEVICES;
376376
if (show_all_devices){

SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSink.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class AudioOutputDevice : public AudioFloatToStream, private ObjectStreamListene
7373
void set_volume(double volume){
7474
auto scope_check = m_sanitizer.check_scope();
7575
double absolute = convertAudioVolumeFromSlider(volume);
76-
m_logger.log("Volume set to: Slider = " + tostr_default(volume) + " -> Absolute = " + tostr_default(absolute));
76+
m_logger.log(std::format("Volume set to: Slider = {} -> Absolute = {}", volume, absolute));
7777
m_sink.setVolume(absolute);
7878
}
7979

SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSource.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class AudioInputDevice final : public QIODevice{
7676
m_source->start(this);
7777
WallClock end = current_time();
7878
double seconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.;
79-
logger.log("Done starting audio... " + tostr_fixed(seconds, 3) + " seconds", COLOR_CYAN);
79+
logger.log(std::format("Done starting audio... {:.3f} seconds", seconds), COLOR_CYAN);
8080
}
8181
~AudioInputDevice(){
8282
if (m_source){

SerialPrograms/Source/CommonFramework/Environment/HardwareValidation_x86.tpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ bool check_hardware(){
6060
str += "You can continue, but the program may not work correctly.<br>";
6161
str += "(i.e. Increased error rate. Fail to reliably detect shinies, etc...)<br><br>";
6262
str += "(Reason: Base frequency measured at ";
63-
str += QString::fromStdString(tostr_fixed(specs.base_frequency / 1000000000., 3));
63+
str += QString::fromStdString(std::format("{:.3f}", specs.base_frequency / 1e9));
6464
str += " GHz which is very slow.)<br><br>";
6565
str += "Recommendation: Use a more powerful computer.";
6666
box.warning(nullptr, "Warning", str);
@@ -82,10 +82,10 @@ bool check_hardware(){
8282
str += "Threads: " + QString::number(specs.threads) + "<br>";
8383
str += "Sockets: " + QString::number(specs.sockets) + "<br>";
8484
str += "Numa Nodes: " + QString::number(specs.numa_nodes) + "<br>";
85-
str += "Base Frequency: " + QString::fromStdString(tostr_fixed(specs.base_frequency / 1000000000., 3)) + " GHz<br>";
85+
str += QString::fromStdString(std::format("Base Frequency: {:.3f} GHz<br>", specs.base_frequency / 1e9));
8686
str += "<br>";
8787
str += "(p-cores + 0.2 * v-cores) * base-frequency = ";
88-
str += QString::fromStdString(tostr_fixed(efreq / 1000000000., 3));
88+
str += QString::fromStdString(std::format("{:.3f}", efreq / 1e9));
8989
str += " GHz<br><br>";
9090
str += "Recommendation: Use a more powerful computer.";
9191
box.warning(nullptr, "Warning", str);

SerialPrograms/Source/CommonFramework/ErrorReports/ErrorReports.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,16 +365,15 @@ std::unique_ptr<AsyncTask> send_all_unsent_reports(Logger& logger, bool allow_pr
365365
QMessageBox::StandardButton button = box.information(
366366
nullptr,
367367
"Error Reporting",
368-
QString::fromStdString(
369-
(reports.size() == 1
370-
? "There is " + tostr_u_commas(reports.size()) + " error report.<br><br>"
371-
"Would you like to help make this program better by sending it to the developers?<br><br>"
372-
: "There are " + tostr_u_commas(reports.size()) + " error reports.<br><br>"
373-
"Would you like to help make this program better by sending them to the developers?<br><br>"
374-
) +
375-
make_text_url(ERROR_PATH_UNSENT, "View unsent error reports.")// + "<br><br>"
376-
// "(You can change )"
377-
),
368+
QString::fromStdString(std::format(
369+
"There {} {:L} error report{}.<br><br>"
370+
"Would you like to help make this program better by sending {} to the developers?<br><br>{}",
371+
(reports.size() == 1 ? "is" : "are"),
372+
reports.size(),
373+
(reports.size() == 1 ? "" : "s"),
374+
(reports.size() == 1 ? "it" : "them"),
375+
make_text_url(ERROR_PATH_UNSENT, "View unsent error reports.")
376+
)),
378377
QMessageBox::Yes | QMessageBox::No,
379378
QMessageBox::StandardButton::Yes
380379
);

0 commit comments

Comments
 (0)