Skip to content

Commit e2e153b

Browse files
committed
Revert "Use std::format instead of custom string formatting (PokemonAutomation#914)"
This reverts commit 3c44efb.
1 parent 3e1934a commit e2e153b

File tree

92 files changed

+360
-250
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

+360
-250
lines changed

Common/Cpp/Options/FloatingPointOption.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ 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-
return std::format("Value too small: min = {}, value = {}", data.m_min_value, x);
124+
std::ostringstream ss;
125+
return "Value too small: min = " + tostr_default(data.m_min_value) + ", value = " + tostr_default(x);
125126
}
126127
if (x > data.m_max_value){
127-
return std::format("Value too large: max = {}, value = {}", data.m_max_value, x);
128+
std::ostringstream ss;
129+
return "Value too large: max = " + tostr_default(data.m_max_value) + ", value = " + tostr_default(x);
128130
}
129131
if (std::isnan(x)){
130132
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 std::format("{:L} {}", value.count(), m_data->m_units);
262+
return tostr_u_commas(value.count()) + " " + m_data->m_units;
263263
}
264264
}
265265

Common/Cpp/Options/TimeExpressionOption.cpp

Lines changed: 9 additions & 5 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 += std::format("{:L}", ticks);
28+
str += tostr_u_commas(ticks);
2929
str += " tick";
3030
// Compute absolute value of the ticks:
3131
uint64_t abs_ticks = 0;
@@ -44,13 +44,17 @@ std::string ticks_to_time(double ticks_per_second, int64_t ticks){
4444
str += "-";
4545
}
4646
if (abs_ticks < MINUTE * 2){
47-
str += std::format("{:.3f} seconds", (double)abs_ticks / SECOND);
47+
str += tostr_fixed((double)abs_ticks / SECOND, 3);
48+
str += " seconds";
4849
}else if (abs_ticks < HOUR * 2){
49-
str += std::format("{:.3f} minutes", (double)abs_ticks / MINUTE);
50+
str += tostr_fixed((double)abs_ticks / MINUTE, 3);
51+
str += " minutes";
5052
}else if (abs_ticks < DAY * 2){
51-
str += std::format("{:.3f} hours", (double)abs_ticks / HOUR);
53+
str += tostr_fixed((double)abs_ticks / HOUR, 3);
54+
str += " hours";
5255
}else{
53-
str += std::format("{:.3f} days", (double)abs_ticks / DAY);
56+
str += tostr_fixed((double)abs_ticks / DAY, 3);
57+
str += " days";
5458
}
5559
str += ")";
5660
return str;

Common/Cpp/PrettyPrint.cpp

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,33 @@ 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+
}
2552

2653

2754
inline std::string byte_prefix(size_t index){
@@ -137,6 +164,22 @@ std::string tostr_bytes(uint64_t bytes){
137164
return tostr_ui_bytes(bytes);
138165
}
139166

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+
140183
std::string now_to_filestring(){
141184
#if _WIN32 && _MSC_VER
142185
#pragma warning(disable:4996)
@@ -201,21 +244,30 @@ std::string duration_to_string(std::chrono::milliseconds milliseconds){
201244

202245
uint64_t ticks = milliseconds.count();
203246

247+
std::string str;
204248
if (ticks < MINUTE * 2){
205-
return std::format("{:.3f} seconds", (double)ticks / SECOND);
249+
str += tostr_fixed((double)ticks / SECOND, 3);
250+
str += " seconds";
206251
}else if (ticks < HOUR * 2){
207-
return std::format("{:.3f} minutes", (double)ticks / MINUTE);
252+
str += tostr_fixed((double)ticks / MINUTE, 3);
253+
str += " minutes";
208254
}else if (ticks < DAY * 2){
209-
return std::format("{:.3f} hours", (double)ticks / HOUR);
255+
str += tostr_fixed((double)ticks / HOUR, 3);
256+
str += " hours";
210257
}else if (ticks < WEEK * 2){
211-
return std::format("{:.3f} days", (double)ticks / DAY);
258+
str += tostr_fixed((double)ticks / DAY, 3);
259+
str += " days";
212260
}else if (ticks < YEARS * 2){
213-
return std::format("{:.3f} weeks", (double)ticks / WEEK);
261+
str += tostr_fixed((double)ticks / WEEK, 3);
262+
str += " weeks";
214263
}else if (ticks < MILLION_YEARS){
215-
return std::format("{:.3f} years", (double)ticks / YEARS);
264+
str += tostr_fixed((double)ticks / YEARS, 3);
265+
str += " years";
216266
}else{
217-
return std::format("{:.3f} million years", (double)ticks / MILLION_YEARS);
267+
str += tostr_fixed((double)ticks / MILLION_YEARS, 3);
268+
str += " million years";
218269
}
270+
return str;
219271
}
220272

221273

Common/Cpp/PrettyPrint.h

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

1514
namespace PokemonAutomation{
1615

1716
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+
2026
// Format current time to a string to be used as filenames.
2127
// e.g. "20220320-044444408355"
2228
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(std::format("Done querying audio inputs... {:.3f} seconds", seconds), COLOR_CYAN);
373+
global_logger_tagged().log("Done querying audio inputs... " + tostr_fixed(seconds, 3) + " 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(std::format("Volume set to: Slider = {} -> Absolute = {}", volume, absolute));
76+
m_logger.log("Volume set to: Slider = " + tostr_default(volume) + " -> Absolute = " + tostr_default(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(std::format("Done starting audio... {:.3f} seconds", seconds), COLOR_CYAN);
79+
logger.log("Done starting audio... " + tostr_fixed(seconds, 3) + " 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(std::format("{:.3f}", specs.base_frequency / 1e9));
63+
str += QString::fromStdString(tostr_fixed(specs.base_frequency / 1000000000., 3));
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 += QString::fromStdString(std::format("Base Frequency: {:.3f} GHz<br>", specs.base_frequency / 1e9));
85+
str += "Base Frequency: " + QString::fromStdString(tostr_fixed(specs.base_frequency / 1000000000., 3)) + " GHz<br>";
8686
str += "<br>";
8787
str += "(p-cores + 0.2 * v-cores) * base-frequency = ";
88-
str += QString::fromStdString(std::format("{:.3f}", efreq / 1e9));
88+
str += QString::fromStdString(tostr_fixed(efreq / 1000000000., 3));
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: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,16 @@ 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(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-
)),
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+
),
377378
QMessageBox::Yes | QMessageBox::No,
378379
QMessageBox::StandardButton::Yes
379380
);

0 commit comments

Comments
 (0)