1
0
Fork 0

Codechange: Add format_append as short-hand to format_to + back_inserter.

pull/14164/head
frosch 2025-04-29 12:37:34 +02:00 committed by frosch
parent 98481ecc01
commit fda93b6f35
12 changed files with 32 additions and 27 deletions

View File

@ -2108,7 +2108,7 @@ static bool ConNetworkAuthorizedKey(std::span<std::string_view> argv)
IConsolePrint(CC_HELP, "Instead of a key, use 'client:<id>' to add/remove the key of that given client.");
std::string buffer;
for (auto [name, _] : _console_cmd_authorized_keys) fmt::format_to(std::back_inserter(buffer), ", {}", name);
for (auto [name, _] : _console_cmd_authorized_keys) format_append(buffer, ", {}", name);
IConsolePrint(CC_HELP, "The supported types are: all{} and company:<id>.", buffer);
return true;
}
@ -2611,7 +2611,7 @@ static bool ConNewGRFProfile(std::span<std::string_view> argv)
started++;
if (!grfids.empty()) grfids += ", ";
fmt::format_to(std::back_inserter(grfids), "[{:08X}]", std::byteswap(pr.grffile->grfid));
format_append(grfids, "[{:08X}]", std::byteswap(pr.grffile->grfid));
}
}
if (started > 0) {

View File

@ -45,4 +45,10 @@ struct fmt::formatter<T, Char> : fmt::formatter<typename T::BaseType> {
}
};
template <class... Args>
void format_append(std::string &out, fmt::format_string<Args...> &&fmt, Args&&... args)
{
fmt::format_to(std::back_inserter(out), std::forward<decltype(fmt)>(fmt), std::forward<decltype(args)>(args)...);
}
#endif /* FORMAT_HPP */

View File

@ -203,7 +203,7 @@ std::string GetDebugString()
std::string result;
for (const auto &debug_level : _debug_levels) {
if (!result.empty()) result += ", ";
fmt::format_to(std::back_inserter(result), "{}={}", debug_level.name, *debug_level.level);
format_append(result, "{}={}", debug_level.name, *debug_level.level);
}
return result;
}

View File

@ -364,7 +364,7 @@ class NetworkContentListWindow : public Window, ContentCallback {
if (!first) url.push_back(',');
first = false;
fmt::format_to(std::back_inserter(url), "{:08X}:{}", ci->unique_id, FormatArrayAsHex(ci->md5sum));
format_append(url, "{:08X}:{}", ci->unique_id, FormatArrayAsHex(ci->md5sum));
}
} else {
url += "do=searchtext&q=";
@ -376,7 +376,7 @@ class NetworkContentListWindow : public Window, ContentCallback {
/* Escape special chars, such as &%,= */
if (*search < 0x30) {
fmt::format_to(std::back_inserter(url), "%{:02X}", *search);
format_append(url, "%{:02X}", *search);
} else {
url.push_back(*search);
}

View File

@ -238,19 +238,19 @@ static void WriteSavegameInfo(const std::string &name)
std::string message;
message.reserve(1024);
fmt::format_to(std::back_inserter(message), "Name: {}\n", name);
fmt::format_to(std::back_inserter(message), "Savegame ver: {}\n", _sl_version);
fmt::format_to(std::back_inserter(message), "NewGRF ver: 0x{:08X}\n", last_ottd_rev);
fmt::format_to(std::back_inserter(message), "Modified: {}\n", ever_modified);
format_append(message, "Name: {}\n", name);
format_append(message, "Savegame ver: {}\n", _sl_version);
format_append(message, "NewGRF ver: 0x{:08X}\n", last_ottd_rev);
format_append(message, "Modified: {}\n", ever_modified);
if (removed_newgrfs) {
fmt::format_to(std::back_inserter(message), "NewGRFs have been removed\n");
format_append(message, "NewGRFs have been removed\n");
}
message += "NewGRFs:\n";
if (_load_check_data.HasNewGrfs()) {
for (const auto &c : _load_check_data.grfconfig) {
fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", std::byteswap(c->ident.grfid),
format_append(message, "{:08X} {} {}\n", std::byteswap(c->ident.grfid),
FormatArrayAsHex(c->flags.Test(GRFConfigFlag::Compatible) ? c->original_md5sum : c->ident.md5sum), c->filename);
}
}

View File

@ -274,13 +274,13 @@ static const uint MAX_FRAMES = 64;
/* Get symbol name and line info if possible. */
DWORD64 offset;
if (proc.pSymGetSymFromAddr64(hCur, frame.AddrPC.Offset, &offset, sym_info)) {
message += fmt::format(" {} + {}", sym_info->Name, offset);
format_append(message, " {} + {}", sym_info->Name, offset);
DWORD line_offs;
IMAGEHLP_LINE64 line;
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
if (proc.pSymGetLineFromAddr64(hCur, frame.AddrPC.Offset, &line_offs, &line)) {
message += fmt::format(" ({}:{})", line.FileName, line.LineNumber);
format_append(message, " ({}:{})", line.FileName, line.LineNumber);
}
}

View File

@ -383,11 +383,11 @@ static void CDECL HandleSavegameLoadCrash(int signum)
for (const auto &c : _grfconfig) {
if (c->flags.Test(GRFConfigFlag::Compatible)) {
const GRFIdentifier &replaced = _gamelog.GetOverriddenIdentifier(*c);
fmt::format_to(std::back_inserter(message), "NewGRF {:08X} (checksum {}) not found.\n Loaded NewGRF \"{}\" (checksum {}) with same GRF ID instead.\n",
format_append(message, "NewGRF {:08X} (checksum {}) not found.\n Loaded NewGRF \"{}\" (checksum {}) with same GRF ID instead.\n",
std::byteswap(c->ident.grfid), FormatArrayAsHex(c->original_md5sum), c->filename, FormatArrayAsHex(replaced.md5sum));
}
if (c->status == GCS_NOT_FOUND) {
fmt::format_to(std::back_inserter(message), "NewGRF {:08X} ({}) not found; checksum {}.\n",
format_append(message, "NewGRF {:08X} ({}) not found; checksum {}.\n",
std::byteswap(c->ident.grfid), c->filename, FormatArrayAsHex(c->ident.md5sum));
}
}

View File

@ -152,7 +152,7 @@ static std::string_view MakeScreenshotName(std::string_view default_fn, std::str
size_t len = _screenshot_name.size();
/* Add extension to screenshot file */
_screenshot_name += fmt::format(".{}", ext);
format_append(_screenshot_name, ".{}", ext);
std::string_view screenshot_dir = crashlog ? _personal_dir : FiosGetScreenshotDir();
@ -163,7 +163,7 @@ static std::string_view MakeScreenshotName(std::string_view default_fn, std::str
if (!FileExists(_full_screenshot_path)) break;
/* If file exists try another one with same name, but just with a higher index */
_screenshot_name.erase(len);
_screenshot_name += fmt::format("#{}.{}", serial, ext);
format_append(_screenshot_name, "#{}.{}", serial, ext);
}
return _full_screenshot_path;

View File

@ -83,19 +83,19 @@ public:
std::string message;
message.reserve(1024);
fmt::format_to(std::back_inserter(message), "Graphics set: {} ({})\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version);
format_append(message, "Graphics set: {} ({})\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version);
message += "NewGRFs:\n";
if (_game_mode != GM_MENU) {
for (const auto &c : _grfconfig) {
fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", std::byteswap(c->ident.grfid), FormatArrayAsHex(c->ident.md5sum), c->filename);
format_append(message, "{:08X} {} {}\n", std::byteswap(c->ident.grfid), FormatArrayAsHex(c->ident.md5sum), c->filename);
}
}
message += "\nCompanies:\n";
for (const Company *c : Company::Iterate()) {
if (c->ai_info == nullptr) {
fmt::format_to(std::back_inserter(message), "{:2d}: Human\n", c->index);
format_append(message, "{:2d}: Human\n", c->index);
} else {
fmt::format_to(std::back_inserter(message), "{:2d}: {} (v{})\n", c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
format_append(message, "{:2d}: {} (v{})\n", c->index, c->ai_info->GetName(), c->ai_info->GetVersion());
}
}
text[1].key = const_cast<char *>("Description");

View File

@ -168,7 +168,7 @@ std::string ScriptConfig::SettingsToString() const
std::string result;
for (const auto &item : this->settings) {
fmt::format_to(std::back_inserter(result), "{}={},", item.first, item.second);
format_append(result, "{}={},", item.first, item.second);
}
/* Remove the last ','. */

View File

@ -12,7 +12,7 @@
#include "../string_func.h"
#include "../strings_func.h"
#include "../3rdparty/fmt/format.h"
#include "../core/format.hpp"
#include "../safeguards.h"
@ -90,13 +90,12 @@ void Script_CreateDummy(HSQUIRRELVM vm, StringID string, std::string_view type)
/* 2) We construct the AI's code. This is done by merging a header, body and footer */
std::string dummy_script;
auto back_inserter = std::back_inserter(dummy_script);
/* Just a rough ballpark estimate. */
dummy_script.reserve(error_message.size() + 128 + 64 * messages.size());
fmt::format_to(back_inserter, "class Dummy{0} extends {0}Controller {{\n function Start()\n {{\n", type);
format_append(dummy_script, "class Dummy{0} extends {0}Controller {{\n function Start()\n {{\n", type);
for (std::string &message : messages) {
fmt::format_to(back_inserter, " {}Log.Error(\"{}\");\n", type, message);
format_append(dummy_script, " {}Log.Error(\"{}\");\n", type, message);
}
dummy_script += " }\n}\n";

View File

@ -78,7 +78,7 @@ std::string FormatArrayAsHex(std::span<const uint8_t> data)
str.reserve(data.size() * 2 + 1);
for (auto b : data) {
fmt::format_to(std::back_inserter(str), "{:02X}", b);
format_append(str, "{:02X}", b);
}
return str;