Skip to content

Commit ed10d21

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

25 files changed

+91
-126
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/PrettyPrint.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ std::string tostr_padded(size_t digits, uint64_t x);
1717
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);
2220
// Convert double to string with fixed precision.
2321
// The precision specifies the number of digits after the decimal point.
2422
std::string tostr_fixed(double x, int precision);

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/Tools/StatAccumulator.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ double StatAccumulatorI32::stddev() const{
4040

4141
std::string StatAccumulatorI32::dump(const char* units, double divider) const{
4242
divider = 1. / divider;
43-
std::string str;
44-
str += "Count = " + tostr_u_commas(m_count);
45-
str += ", Mean = " + tostr_default(mean() * divider) + units;
46-
str += ", Stddev = " + tostr_default(stddev() * divider) + units;
47-
str += ", Min = " + tostr_default(min() * divider) + units;
48-
str += ", Max = " + tostr_default(max() * divider) + units;
49-
return str;
43+
return std::format("Count = {:L}, Mean = {}{}, Stddev = {}{}, Min = {}{}, Max = {}{}",
44+
m_count,
45+
mean() * divider, units,
46+
stddev() * divider, units,
47+
min() * divider, units,
48+
max() * divider, units);
5049
}
5150
void StatAccumulatorI32::log(Logger& logger, const std::string& label, const char* units, double divider) const{
5251
logger.log(label + ": " + dump(units, divider), COLOR_MAGENTA);

SerialPrograms/Source/CommonTools/Audio/AudioPerSpectrumDetectorBase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ void AudioPerSpectrumDetectorBase::throw_if_no_sound(std::chrono::milliseconds m
6161

6262
void AudioPerSpectrumDetectorBase::log_results(){
6363
if (m_last_timestamp != WallClock::min()){
64-
m_logger.log(m_audio_name + " detected! Error Coefficient = " + tostr_default(m_lowest_error), COLOR_BLUE);
64+
m_logger.log(std::format("{} detected! Error Coefficient = {}", m_audio_name, m_lowest_error), COLOR_BLUE);
6565
}else{
66-
m_logger.log(m_audio_name + " not detected. Error Coefficient = " + tostr_default(m_lowest_error), COLOR_PURPLE);
66+
m_logger.log(std::format("{} not detected. Error Coefficient = {}", m_audio_name, m_lowest_error), COLOR_PURPLE);
6767
}
6868

6969
#if 0

SerialPrograms/Source/CommonTools/OCR/OCR_StringMatchResult.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void StringMatchResult::log(Logger& logger, double max_log10p, const std::string
5353
if (results.size() == 1){
5454
auto iter = results.begin();
5555
str += iter->second.to_str();
56-
str += " (log10p = " + tostr_default(iter->first) +")";
56+
str += std::format(" (log10p = {})", iter->first);
5757
}else{
5858
str += "Multiple Candidates =>\n";
5959
size_t printed = 0;
@@ -62,7 +62,7 @@ void StringMatchResult::log(Logger& logger, double max_log10p, const std::string
6262
str += " (" + std::to_string(results.size() - 10) + " more...)\n";
6363
break;
6464
}
65-
str += " " + tostr_default(item.first) + " : " + item.second.to_str() + "\n";
65+
str += std::format(" {} : {}\n", item.first, item.second.to_str());
6666
printed++;
6767
}
6868
}

SerialPrograms/Source/Pokemon/Pokemon_CollectedPokemonInfo.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ std::ostream& operator<<(std::ostream& os, const std::optional<CollectedPokemonI
103103

104104
std::string create_overlay_info(const CollectedPokemonInfo& pokemon){
105105
const std::string& display_name = get_pokemon_name(pokemon.name_slug).display_name();
106-
char dex_str[7]; // leaving enough space to convert dex number uint16_t to string with "0" to fill up at least 4 characters, e.g. "0005"
107-
snprintf(dex_str, sizeof(dex_str), "%04d", pokemon.dex_number);
108-
std::string overlay_log = std::string(dex_str) + " " + display_name;
106+
std::string overlay_log = std::format("{:04} {}", pokemon.dex_number, display_name);
109107
if(pokemon.gender == StatsHuntGenderFilter::Male){
110108
overlay_log += " " + UNICODE_MALE;
111109
} else if (pokemon.gender == StatsHuntGenderFilter::Female){

SerialPrograms/Source/Pokemon/Pokemon_Notification.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ void send_encounter_notification(
115115
shinies = "Maybe Shiny";
116116
break;
117117
case ShinyType::UNKNOWN_SHINY:
118-
shinies = symbol + std::string(" Shiny ") + symbol;
118+
shinies = std::format("{} Shiny {}", symbol, symbol);
119119
break;
120120
case ShinyType::STAR_SHINY:
121-
shinies = symbol + std::string(" Star Shiny ") + symbol;
121+
shinies = std::format("{} Star Shiny {}", symbol, symbol);
122122
break;
123123
case ShinyType::SQUARE_SHINY:
124-
shinies = symbol + std::string(" Square Shiny ") + symbol;
124+
shinies = std::format("{} Square Shiny {}", symbol, symbol);
125125
break;
126126
}
127127
}else if (!results.empty()){
@@ -130,16 +130,16 @@ void send_encounter_notification(
130130
case 0:
131131
if (shiny_detected){
132132
symbol = shiny_symbol(ShinyType::UNKNOWN_SHINY);
133-
shinies = symbol + " Found Shiny! " + symbol + " (Unable to determine which.)";
133+
shinies = std::format("{} Found Shiny! {} (Unable to determine which.)", symbol, symbol);
134134
}else{
135135
shinies = "No Shinies";
136136
}
137137
break;
138138
case 1:
139-
shinies = symbol + " Found Shiny! " + symbol;
139+
shinies = std::format("{} Found Shiny! {}", symbol, symbol);
140140
break;
141141
default:
142-
shinies += symbol + std::string(" Multiple Shinies! ") + symbol;
142+
shinies += std::format("{} Multiple Shinies! {}", symbol, symbol);
143143
break;
144144
}
145145
}
@@ -150,7 +150,7 @@ void send_encounter_notification(
150150
}
151151
if (!shinies.empty()){
152152
if (!std::isnan(alpha)){
153-
shinies += "\n(Detection Alpha = " + tostr_default(alpha) + ")";
153+
shinies += std::format("\n(Detection Alpha = {})", alpha);
154154
}
155155
embeds.emplace_back("Shininess:", std::move(shinies));
156156
}

SerialPrograms/Source/PokemonBDSP/Inference/ShinyDetection/PokemonBDSP_ShinyEncounterDetector.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,10 @@ void determine_shiny_status(
213213
alpha_own += DIALOG_ALPHA;
214214
}
215215
}
216-
env.log(
217-
"ShinyDetector: Wild Alpha = " + tostr_default(alpha_wild_overall) +
218-
(wild_shiny_sound_detected ? " (shiny sound detected)" : "") +
219-
", Left Alpha = " + tostr_default(alpha_wild_left) +
220-
", Right Alpha = " + tostr_default(alpha_wild_right) +
221-
", Your Alpha = " + tostr_default(alpha_own) +
222-
(own_shiny_sound_detected ? " (shiny sound detected)" : ""),
216+
env.log(std::format("ShinyDetector: Wild Alpha = {}{}, Left Alpha = {}, Right Alpha = {}, Your Alpha = {}{}",
217+
alpha_wild_overall, wild_shiny_sound_detected ? " (shiny sound detected)" : "",
218+
alpha_wild_left, alpha_wild_right,
219+
alpha_own, own_shiny_sound_detected ? " (shiny sound detected)" : ""),
223220
COLOR_PURPLE
224221
);
225222

@@ -240,10 +237,7 @@ void determine_shiny_status(
240237

241238
if (DIALOG_ALPHA <= alpha_wild_overall && alpha_wild_overall < DIALOG_ALPHA + 1.5){
242239
dump_image(env.logger(), env.program_info(), "LowShinyAlpha", wild_result.get_best_screenshot());
243-
std::string str;
244-
str += "Low alpha shiny (alpha = ";
245-
str += tostr_default(alpha_wild_overall);
246-
str += ").\nPlease report this image to the " + PROGRAM_NAME + " server.";
240+
std::string str = std::format("Low alpha shiny (alpha = {}).\nPlease report this image to the {} server.", alpha_wild_overall, PROGRAM_NAME);
247241
send_program_recoverable_error_notification(
248242
env, settings,
249243
str,

SerialPrograms/Source/PokemonBDSP/Inference/ShinyDetection/PokemonBDSP_ShinySparkleSet.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ ShinySparkleSetBDSP ShinySparkleSetBDSP::extract_subbox(const ImagePixelBox& sub
5050
ret.update_alphas();
5151
return ret;
5252
}
53-
std::string ShinySparkleSetBDSP::to_str() const{
53+
std::string ShinySparkleSetBDSP::to_str() const {
5454
std::string str;
55-
if (m_alpha_overall < 3.0){
55+
if (m_alpha_overall < 3.0) {
5656
return str;
5757
}
58-
str += "SparkleDetector";
59-
if (!balls.empty()){
60-
str += " - Balls: " + std::to_string(balls.size());
58+
str = "SparkleDetector";
59+
if (!balls.empty()) {
60+
str += std::format(" - Balls: {}", balls.size());
6161
}
62-
if (!stars.empty()){
63-
str += " - Stars: " + std::to_string(stars.size());
62+
if (!stars.empty()) {
63+
str += std::format(" - Stars: {}", stars.size());
6464
}
65-
str += " - (alpha = " + tostr_default(m_alpha_overall) + ")";
65+
str += std::format(" - (alpha = {})", m_alpha_overall);
6666
return str;
6767
}
6868
void ShinySparkleSetBDSP::draw_boxes(

0 commit comments

Comments
 (0)