Add: AppendStringInPlace() to append translated string ID into an existing string. (#12969)

This allows avoiding a string copy when building strings.
This commit is contained in:
Peter Nelson 2024-10-07 19:05:38 +01:00 committed by GitHub
parent 3cd1200668
commit 14b986609b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 21 additions and 7 deletions

View File

@ -896,7 +896,7 @@ struct DepotWindow : Window {
SetDParam(1, loaded[cargo_type]); // {CARGO} #2
SetDParam(2, cargo_type); // {SHORTCARGO} #1
SetDParam(3, capacity[cargo_type]); // {SHORTCARGO} #2
details += GetString(STR_DEPOT_VEHICLE_TOOLTIP_CARGO);
AppendStringInPlace(details, STR_DEPOT_VEHICLE_TOOLTIP_CARGO);
}
/* Show tooltip window */

View File

@ -394,11 +394,11 @@ void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel
std::string message = GetString(summary_msg);
if (detailed_msg != INVALID_STRING_ID) {
message += " ";
message += GetString(detailed_msg);
AppendStringInPlace(message, detailed_msg);
}
if (extra_msg != INVALID_STRING_ID) {
message += " ";
message += GetString(extra_msg);
AppendStringInPlace(message, extra_msg);
}
if (textref_stack_size > 0) StopTextRefStackUsage();

View File

@ -386,7 +386,7 @@ class BuildIndustryWindow : public Window {
}
SetDParam(0, CargoSpec::Get(cargolist[j])->name);
SetDParamStr(1, cargo_suffix[j].text);
cargostring += GetString(STR_INDUSTRY_VIEW_CARGO_LIST_EXTENSION);
AppendStringInPlace(cargostring, STR_INDUSTRY_VIEW_CARGO_LIST_EXTENSION);
}
if (numcargo > 0) {

View File

@ -394,7 +394,7 @@ bool LinkGraphOverlay::ShowTooltip(Point pt, TooltipCloseCondition close_cond)
const auto time = link.time ? back_time ? ((link.time + back_time) / 2) : link.time : back_time;
if (time > 0) {
SetDParam(0, time);
tooltip_extension += GetString(STR_LINKGRAPH_STATS_TOOLTIP_TIME_EXTENSION);
AppendStringInPlace(tooltip_extension, STR_LINKGRAPH_STATS_TOOLTIP_TIME_EXTENSION);
}
SetDParam(0, link.cargo);
SetDParam(1, link.Usage());

View File

@ -60,10 +60,10 @@ void DrawRoadVehDetails(const Vehicle *v, const Rect &r)
SetDParam(0, cid);
SetDParam(1, max_cargo[cid]);
capacity += GetString(STR_JUST_CARGO);
AppendStringInPlace(capacity, STR_JUST_CARGO);
if (subtype_text[cid] != STR_NULL) {
capacity += GetString(subtype_text[cid]);
AppendStringInPlace(capacity, subtype_text[cid]);
}
first = false;

View File

@ -322,6 +322,19 @@ std::string GetString(StringID string)
return GetStringWithArgs(string, _global_string_params);
}
/**
* Resolve the given StringID and append in place into an existing std::string with all the associated
* DParam lookups and formatting.
* @param result The std::string to place the translated string.
* @param string The unique identifier of the translatable string.
*/
void AppendStringInPlace(std::string &result, StringID string)
{
_global_string_params.PrepareForNextRun();
StringBuilder builder(result);
GetStringWithArgs(builder, string, _global_string_params);
}
/**
* Get a parsed string with most special stringcodes replaced by the string parameters.
* @param string The ID of the string to parse.

View File

@ -61,6 +61,7 @@ inline StringID MakeStringID(StringTab tab, uint index)
std::string GetString(StringID string);
const char *GetStringPtr(StringID string);
void AppendStringInPlace(std::string &result, StringID string);
uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type);
uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type);