diff --git a/src/misc/dbg_helpers.cpp b/src/misc/dbg_helpers.cpp index 63b348e73c..7e2d4314c4 100644 --- a/src/misc/dbg_helpers.cpp +++ b/src/misc/dbg_helpers.cpp @@ -106,31 +106,17 @@ void DumpTarget::WriteIndent() } } -/** Write 'name = value' with indent and new-line. */ -void DumpTarget::WriteValue(const std::string &name, int value) -{ - WriteIndent(); - m_out += name + " = " + std::to_string(value) + "\n"; -} - -/** Write 'name = value' with indent and new-line. */ -void DumpTarget::WriteValue(const std::string &name, const std::string &value_str) -{ - WriteIndent(); - m_out += name + " = " + value_str + "\n"; -} - /** Write name & TileIndex to the output. */ -void DumpTarget::WriteTile(const std::string &name, TileIndex tile) +void DumpTarget::WriteTile(std::string_view name, TileIndex tile) { WriteIndent(); - m_out += name + " = " + TileStr(tile) + "\n"; + format_append(m_out, "{} = {}\n", name, TileStr(tile)); } /** * Open new structure (one level deeper than the current one) 'name = {\'. */ -void DumpTarget::BeginStruct(size_t type_id, const std::string &name, const void *ptr) +void DumpTarget::BeginStruct(size_t type_id, std::string_view name, const void *ptr) { /* make composite name */ std::string cur_name = GetCurrentStructName(); @@ -147,7 +133,7 @@ void DumpTarget::BeginStruct(size_t type_id, const std::string &name, const void m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name)); WriteIndent(); - m_out += name + " = {\n"; + format_append(m_out, "{} = {{\n", name); m_indent++; } diff --git a/src/misc/dbg_helpers.h b/src/misc/dbg_helpers.h index e67e929edb..f34dd85bba 100644 --- a/src/misc/dbg_helpers.h +++ b/src/misc/dbg_helpers.h @@ -16,6 +16,7 @@ #include "../signal_type.h" #include "../tile_type.h" #include "../track_type.h" +#include "../core/format.hpp" /** Helper template class that provides C array length and item type */ template struct ArrayT; @@ -156,21 +157,26 @@ struct DumpTarget { void WriteIndent(); - void WriteValue(const std::string &name, int value); - void WriteValue(const std::string &name, const std::string &value_str); - void WriteTile(const std::string &name, TileIndex t); + /** Write 'name = value' with indent and new-line. */ + void WriteValue(std::string_view name, const auto &value) + { + WriteIndent(); + format_append(m_out, "{} = {}\n", name, value); + } + + void WriteTile(std::string_view name, TileIndex t); /** Dump given enum value (as a number and as named value) */ - template void WriteEnumT(const std::string &name, E e) + template void WriteEnumT(std::string_view name, E e) { WriteValue(name, ValueStr(e)); } - void BeginStruct(size_t type_id, const std::string &name, const void *ptr); + void BeginStruct(size_t type_id, std::string_view name, const void *ptr); void EndStruct(); /** Dump nested object (or only its name if this instance is already known). */ - template void WriteStructT(const std::string &name, const S *s) + template void WriteStructT(std::string_view name, const S *s) { static size_t type_id = ++LastTypeId(); @@ -193,7 +199,7 @@ struct DumpTarget { } /** Dump nested object (or only its name if this instance is already known). */ - template void WriteStructT(const std::string &name, const std::deque *s) + template void WriteStructT(std::string_view name, const std::deque *s) { static size_t type_id = ++LastTypeId(); @@ -211,7 +217,7 @@ struct DumpTarget { /* Still unknown, dump it */ BeginStruct(type_id, name, s); size_t num_items = s->size(); - this->WriteValue("num_items", std::to_string(num_items)); + this->WriteValue("num_items", num_items); for (size_t i = 0; i < num_items; i++) { const auto &item = (*s)[i]; this->WriteStructT(fmt::format("item[{}]", i), &item); diff --git a/src/music/midifile.cpp b/src/music/midifile.cpp index e581bc342c..9985c96e85 100644 --- a/src/music/midifile.cpp +++ b/src/music/midifile.cpp @@ -1065,7 +1065,7 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song) AppendPathSeparator(tempdirname); FioCreateDirectory(tempdirname); - std::string output_filename = tempdirname + std::to_string(song.cat_index) + ".mid"; + std::string output_filename = fmt::format("{}{}.mid", tempdirname, song.cat_index); if (FileExists(output_filename)) { /* If the file already exists, assume it's the correct decoded data */ diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp index ff667aab7f..ebb27f58ca 100644 --- a/src/network/core/address.cpp +++ b/src/network/core/address.cpp @@ -209,7 +209,7 @@ SOCKET NetworkAddress::Resolve(int family, int socktype, int flags, SocketList * hints.ai_socktype = socktype; /* The port needs to be a string. Six is enough to contain all characters + '\0'. */ - std::string port_name = std::to_string(this->GetPort()); + std::string port_name = fmt::format("{}", this->GetPort()); bool reset_hostname = false; /* Setting both hostname to nullptr and port to 0 is not allowed. diff --git a/src/network/core/tcp_connect.cpp b/src/network/core/tcp_connect.cpp index 0862bdc6a6..44a4f550d1 100644 --- a/src/network/core/tcp_connect.cpp +++ b/src/network/core/tcp_connect.cpp @@ -228,7 +228,7 @@ void TCPConnecter::Resolve() hints.ai_flags = AI_ADDRCONFIG; hints.ai_socktype = SOCK_STREAM; - std::string port_name = std::to_string(address.GetPort()); + std::string port_name = fmt::format("{}", address.GetPort()); static bool getaddrinfo_timeout_error_shown = false; auto start = std::chrono::steady_clock::now(); diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp index e67fecfaec..cdbe3de668 100644 --- a/src/network/network_content.cpp +++ b/src/network/network_content.cpp @@ -323,7 +323,7 @@ void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const Conten { std::string content_request; for (const ContentID &id : content) { - content_request += std::to_string(id) + "\n"; + format_append(content_request, "{}\n", id); } this->http_response_index = -1; diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 6e9459c646..862057107e 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -1627,7 +1627,7 @@ bool NetworkMakeClientNameUnique(std::string &name) if (!is_name_unique) { /* Try a new name ( #1, #2, and so on) */ - name = original_name + " #" + std::to_string(number); + name = fmt::format("{} #{}", original_name, number); /* The constructed client name is larger than the limit, * so... bail out as no valid name can be created. */ diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 5b7db9b30d..eb4862c7a0 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -643,7 +643,7 @@ std::string GRFBuildParamList(const GRFConfig &c) std::string result; for (const uint32_t &value : c.param) { if (!result.empty()) result += ' '; - result += std::to_string(value); + format_append(result, "{}", value); } return result; } diff --git a/src/pathfinder/water_regions.cpp b/src/pathfinder/water_regions.cpp index 10597c0cef..7c327cd6a4 100644 --- a/src/pathfinder/water_regions.cpp +++ b/src/pathfinder/water_regions.cpp @@ -196,7 +196,7 @@ public: { Debug(map, 9, "Water region {},{} labels and edge traversability = ...", GetWaterRegionX(this->tile_area.tile), GetWaterRegionY(this->tile_area.tile)); - const size_t max_element_width = std::to_string(this->NumberOfPatches()).size(); + const size_t max_element_width = fmt::format("{}", this->NumberOfPatches()).size(); std::string traversability = fmt::format("{:0{}b}", this->GetEdgeTraversabilityBits(DIAGDIR_NW), WATER_REGION_EDGE_LENGTH); Debug(map, 9, " {:{}}", fmt::join(traversability, " "), max_element_width); @@ -206,8 +206,11 @@ public: std::string line{}; for (int x = 0; x < WATER_REGION_EDGE_LENGTH; ++x) { const auto label = this->GetLabel(TileAddXY(this->tile_area.tile, x, y)); - const std::string label_str = label == INVALID_WATER_REGION_PATCH ? "." : std::to_string(label); - line = fmt::format("{:{}}", label_str, max_element_width) + " " + line; + if (label == INVALID_WATER_REGION_PATCH) { + line = fmt::format("{:{}} {}", ".", max_element_width, line); + } else { + line = fmt::format("{:{}} {}", label, max_element_width, line); + } } Debug(map, 9, "{} | {}| {}", GB(this->GetEdgeTraversabilityBits(DIAGDIR_SW), y, 1), line, GB(this->GetEdgeTraversabilityBits(DIAGDIR_NE), y, 1)); } diff --git a/src/safeguards.h b/src/safeguards.h index d00c7c0c71..70a272cbf9 100644 --- a/src/safeguards.h +++ b/src/safeguards.h @@ -67,6 +67,9 @@ #define fputs SAFEGUARD_DO_NOT_USE_THIS_METHOD #define putchar SAFEGUARD_DO_NOT_USE_THIS_METHOD +/* Use fmt::format instead */ +#define to_string SAFEGUARD_DO_NOT_USE_THIS_METHOD + /* Use our own templated implementation instead of a macro or function with only one type. */ #ifdef min #undef min diff --git a/src/settings.cpp b/src/settings.cpp index d54048112d..7a86bfa46a 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -324,7 +324,7 @@ std::string ListSettingDesc::FormatValue(const void *object) const default: NOT_REACHED(); } if (i != 0) result += ','; - result += std::to_string(v); + format_append(result, "{}", v); } return result; } @@ -332,7 +332,7 @@ std::string ListSettingDesc::FormatValue(const void *object) const std::string OneOfManySettingDesc::FormatSingleValue(uint id) const { if (id >= this->many.size()) { - return std::to_string(id); + return fmt::format("{}", id); } return this->many[id]; } @@ -735,7 +735,7 @@ std::string IntSettingDesc::FormatValue(const void *object) const } else { i = (uint32_t)this->Read(object); } - return std::to_string(i); + return fmt::format("{}", i); } std::string BoolSettingDesc::FormatValue(const void *object) const @@ -1218,7 +1218,7 @@ static void SaveVersionInConfig(IniFile &ini) IniGroup &group = ini.GetOrCreateGroup("version"); group.GetOrCreateItem("version_string").SetValue(_openttd_revision); group.GetOrCreateItem("version_number").SetValue(fmt::format("{:08X}", _openttd_newgrf_version)); - group.GetOrCreateItem("ini_version").SetValue(std::to_string(INIFILE_VERSION)); + group.GetOrCreateItem("ini_version").SetValue(fmt::format("{}", INIFILE_VERSION)); } /** diff --git a/src/survey.cpp b/src/survey.cpp index 838a702fb1..19c829d90a 100644 --- a/src/survey.cpp +++ b/src/survey.cpp @@ -311,7 +311,7 @@ void SurveyFont(nlohmann::json &survey) void SurveyCompanies(nlohmann::json &survey) { for (const Company *c : Company::Iterate()) { - auto &company = survey[std::to_string(c->index.base())]; + auto &company = survey[fmt::format("{}", c->index.base())]; if (c->ai_info == nullptr) { company["type"] = "human"; } else {