1
0
Fork 0

Codechange: Use format instead of to_string.

pull/14164/head
frosch 2025-04-29 12:41:06 +02:00 committed by frosch
parent fda93b6f35
commit afe3dfb3a4
12 changed files with 38 additions and 40 deletions

View File

@ -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 = {\<LF\>'.
*/
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++;
}

View File

@ -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 <typename T> 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 <typename E> void WriteEnumT(const std::string &name, E e)
template <typename E> 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 <typename S> void WriteStructT(const std::string &name, const S *s)
template <typename S> 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 <typename S> void WriteStructT(const std::string &name, const std::deque<S> *s)
template <typename S> void WriteStructT(std::string_view name, const std::deque<S> *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);

View File

@ -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 */

View File

@ -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.

View File

@ -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();

View File

@ -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;

View File

@ -1627,7 +1627,7 @@ bool NetworkMakeClientNameUnique(std::string &name)
if (!is_name_unique) {
/* Try a new name (<name> #1, <name> #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. */

View File

@ -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;
}

View File

@ -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));
}

View File

@ -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

View File

@ -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));
}
/**

View File

@ -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 {