diff --git a/src/industry.h b/src/industry.h index ba276589b7..9574fc5f38 100644 --- a/src/industry.h +++ b/src/industry.h @@ -184,10 +184,10 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { memset(&counts, 0, sizeof(counts)); } - inline const char *GetCachedName() const + inline const std::string &GetCachedName() const { if (this->cached_name.empty()) this->FillCachedName(); - return this->cached_name.c_str(); + return this->cached_name; } private: diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index c19d5e4501..dcafc80112 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -467,9 +467,9 @@ class NetworkContentListWindow : public Window, ContentCallback { static bool CDECL TagNameFilter(const ContentInfo * const *a, ContentListFilterData &filter) { filter.string_filter.ResetState(); - for (auto &tag : (*a)->tags) filter.string_filter.AddLine(tag.c_str()); + for (auto &tag : (*a)->tags) filter.string_filter.AddLine(tag); - filter.string_filter.AddLine((*a)->name.c_str()); + filter.string_filter.AddLine((*a)->name); return filter.string_filter.GetState(); } diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 209c41f144..3e29c0dcd7 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -379,7 +379,7 @@ protected: assert((*item) != nullptr); sf.ResetState(); - sf.AddLine((*item)->info.server_name.c_str()); + sf.AddLine((*item)->info.server_name); return sf.GetState(); } diff --git a/src/object_gui.cpp b/src/object_gui.cpp index db4f14f144..a644becaa6 100644 --- a/src/object_gui.cpp +++ b/src/object_gui.cpp @@ -142,11 +142,9 @@ public: static bool CDECL TagNameFilter(ObjectClassID const *oc, StringFilter &filter) { ObjectClass *objclass = ObjectClass::Get(*oc); - char buffer[DRAW_STRING_BUFFER]; - GetString(buffer, objclass->name, lastof(buffer)); filter.ResetState(); - filter.AddLine(buffer); + filter.AddLine(GetString(objclass->name)); return filter.GetState(); } diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 885fb80c8e..24a4a89249 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1046,11 +1046,8 @@ public: /** Filter station classes by class name. */ static bool CDECL TagNameFilter(StationClassID const * sc, StringFilter &filter) { - char buffer[DRAW_STRING_BUFFER]; - GetString(buffer, StationClass::Get(*sc)->name, lastof(buffer)); - filter.ResetState(); - filter.AddLine(buffer); + filter.AddLine(GetString(StationClass::Get(*sc)->name)); return filter.GetState(); } diff --git a/src/road_gui.cpp b/src/road_gui.cpp index f46679994e..50660c277b 100644 --- a/src/road_gui.cpp +++ b/src/road_gui.cpp @@ -1249,11 +1249,8 @@ public: /** Filter classes by class name. */ static bool CDECL TagNameFilter(RoadStopClassID const *sc, StringFilter &filter) { - char buffer[DRAW_STRING_BUFFER]; - GetString(buffer, RoadStopClass::Get(*sc)->name, lastof(buffer)); - filter.ResetState(); - filter.AddLine(buffer); + filter.AddLine(GetString(RoadStopClass::Get(*sc)->name)); return filter.GetState(); } diff --git a/src/stringfilter.cpp b/src/stringfilter.cpp index 28a12eab2c..d15bbcde76 100644 --- a/src/stringfilter.cpp +++ b/src/stringfilter.cpp @@ -117,6 +117,19 @@ void StringFilter::AddLine(const char *str) } } +/** + * Pass another text line from the current item to the filter. + * + * You can call this multiple times for a single item, if the filter shall apply to multiple things. + * Before processing the next item you have to call ResetState(). + * + * @param str Another line from the item. + */ +void StringFilter::AddLine(const std::string &str) +{ + AddLine(str.c_str()); +} + /** * Pass another text line from the current item to the filter. * @@ -127,7 +140,5 @@ void StringFilter::AddLine(const char *str) */ void StringFilter::AddLine(StringID str) { - char buffer[DRAW_STRING_BUFFER]; - GetString(buffer, str, lastof(buffer)); - AddLine(buffer); + AddLine(GetString(str)); } diff --git a/src/stringfilter_type.h b/src/stringfilter_type.h index 92db480a6f..68b359c9d5 100644 --- a/src/stringfilter_type.h +++ b/src/stringfilter_type.h @@ -60,6 +60,7 @@ public: void ResetState(); void AddLine(const char *str); + void AddLine(const std::string &str); void AddLine(StringID str); /** diff --git a/src/strings.cpp b/src/strings.cpp index 33dea74511..9b24703fbf 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -1458,7 +1458,7 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg static bool use_cache = true; if (use_cache) { // Use cached version if first call AutoRestoreBackup cache_backup(use_cache, false); - buff = strecpy(buff, i->GetCachedName(), last); + buff = strecpy(buff, i->GetCachedName().c_str(), last); } else if (_scan_for_gender_data) { /* Gender is defined by the industry type. * STR_FORMAT_INDUSTRY_NAME may have the town first, so it would result in the gender of the town name */ @@ -1541,7 +1541,7 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg static bool use_cache = true; if (use_cache) { // Use cached version if first call AutoRestoreBackup cache_backup(use_cache, false); - buff = strecpy(buff, t->GetCachedName(), last); + buff = strecpy(buff, t->GetCachedName().c_str(), last); } else if (!t->name.empty()) { int64 args_array[] = {(int64)(size_t)t->name.c_str()}; StringParameters tmp_params(args_array); diff --git a/src/town.h b/src/town.h index 6b24178f03..0a9711a45d 100644 --- a/src/town.h +++ b/src/town.h @@ -125,11 +125,11 @@ struct Town : TownPool::PoolItem<&_town_pool> { void UpdateVirtCoord(); - inline const char *GetCachedName() const + inline const std::string &GetCachedName() const { - if (!this->name.empty()) return this->name.c_str(); + if (!this->name.empty()) return this->name; if (this->cached_name.empty()) this->FillCachedName(); - return this->cached_name.c_str(); + return this->cached_name; } static inline Town *GetByTile(TileIndex tile)