Use TinyString for custom name fields which are almost always empty

pull/174/head
Jonathan G Rennison 4 years ago
parent 81e69fcbbe
commit 5243cfd6d0

@ -13,11 +13,11 @@
#include "order_type.h"
#include "date_type.h"
#include "timetable.h"
#include <string>
#include "core/tinystring_type.hpp"
/** Various front vehicle properties that are preserved when autoreplacing, using order-backup or switching front engines within a consist. */
struct BaseConsist {
std::string name; ///< Name of vehicle
TinyString name; ///< Name of vehicle
/* Used for timetabling. */
uint32 current_order_time; ///< How many ticks have passed since this order started.

@ -15,6 +15,7 @@
#include "viewport_type.h"
#include "station_map.h"
#include "core/geometry_type.hpp"
#include "core/tinystring_type.hpp"
#include <memory>
typedef Pool<BaseStation, StationID, 32, 64000> StationPool;
@ -56,7 +57,7 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> {
TrackedViewportSign sign; ///< NOSAVE: Dimensions of sign
byte delete_ctr; ///< Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is deleted.
std::string name; ///< Custom name
TinyString name; ///< Custom name
StringID string_id; ///< Default name (town area) of station
mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the station, if not using a custom name

@ -12,13 +12,14 @@
#include "depot_map.h"
#include "core/pool_type.hpp"
#include "core/tinystring_type.hpp"
typedef Pool<Depot, DepotID, 64, 64000> DepotPool;
extern DepotPool _depot_pool;
struct Depot : DepotPool::PoolItem<&_depot_pool> {
Town *town;
std::string name;
TinyString name;
TileIndex xy;
uint16 town_cn; ///< The N-1th depot for this town (consecutive number)

@ -13,13 +13,14 @@
#include "engine_type.h"
#include "vehicle_type.h"
#include "core/pool_type.hpp"
#include "core/tinystring_type.hpp"
#include "newgrf_commons.h"
typedef Pool<Engine, EngineID, 64, 64000> EnginePool;
extern EnginePool _engine_pool;
struct Engine : EnginePool::PoolItem<&_engine_pool> {
std::string name; ///< Custom name of engine.
TinyString name; ///< Custom name of engine.
Date intro_date; ///< Date of introduction of the engine.
Date age;
uint16 reliability; ///< Current reliability of the engine.

@ -23,7 +23,7 @@ static const SaveLoad _depot_desc[] = {
SLEG_CONDVAR(_town_index, SLE_UINT16, SL_MIN_VERSION, SLV_141),
SLE_CONDREF(Depot, town, REF_TOWN, SLV_141, SL_MAX_VERSION),
SLE_CONDVAR(Depot, town_cn, SLE_UINT16, SLV_141, SL_MAX_VERSION),
SLE_CONDSSTR(Depot, name, SLE_STR, SLV_141, SL_MAX_VERSION),
SLE_CONDSTR(Depot, name, SLE_STR, 0, SLV_141, SL_MAX_VERSION),
SLE_CONDVAR(Depot, build_date, SLE_INT32, SLV_142, SL_MAX_VERSION),
SLE_CONDNULL_X(4, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_SPRINGPP, 5)),
SLE_END()

@ -39,7 +39,7 @@ static const SaveLoad _engine_desc[] = {
SLE_CONDVAR(Engine, company_avail, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_104),
SLE_CONDVAR(Engine, company_avail, SLE_UINT16, SLV_104, SL_MAX_VERSION),
SLE_CONDVAR(Engine, company_hidden, SLE_UINT16, SLV_193, SL_MAX_VERSION),
SLE_CONDSSTR(Engine, name, SLE_STR, SLV_84, SL_MAX_VERSION),
SLE_CONDSTR(Engine, name, SLE_STR, 0, SLV_84, SL_MAX_VERSION),
SLE_CONDNULL(16, SLV_2, SLV_144), // old reserved space

@ -893,6 +893,7 @@ void WriteValue(void *ptr, VarType conv, int64 val)
case SLE_VAR_I64: *(int64 *)ptr = val; break;
case SLE_VAR_U64: *(uint64*)ptr = val; break;
case SLE_VAR_NAME: *reinterpret_cast<std::string *>(ptr) = CopyFromOldName(val); break;
case SLE_VAR_CNAME: *(TinyString*)ptr = CopyFromOldName(val); break;
case SLE_VAR_NULL: break;
default: NOT_REACHED();
}

@ -465,8 +465,9 @@ enum VarTypes {
SLE_VAR_STRBQ = 11 << 4, ///< string enclosed in quotes (with pre-allocated buffer)
SLE_VAR_STR = 12 << 4, ///< string pointer
SLE_VAR_STRQ = 13 << 4, ///< string pointer enclosed in quotes
SLE_VAR_NAME = 14 << 4, ///< old custom name to be converted to a char pointer
/* 1 more possible memory-primitives */
SLE_VAR_NAME = 14 << 4, ///< old custom name to be converted to a std::string
SLE_VAR_CNAME = 15 << 4, ///< old custom name to be converted to a char pointer
/* 0 more possible memory-primitives */
/* Shortcut values */
SLE_VAR_CHAR = SLE_VAR_I8,
@ -490,6 +491,7 @@ enum VarTypes {
SLE_STRING = SLE_FILE_STRING | SLE_VAR_STR,
SLE_STRINGQUOTE = SLE_FILE_STRING | SLE_VAR_STRQ,
SLE_NAME = SLE_FILE_STRINGID | SLE_VAR_NAME,
SLE_CNAME = SLE_FILE_STRINGID | SLE_VAR_CNAME,
/* Shortcut values */
SLE_UINT = SLE_UINT32,

@ -65,7 +65,7 @@ void MoveBuoysToWaypoints()
TileIndex xy = st->xy;
Town *town = st->town;
StringID string_id = st->string_id;
std::string name = st->name;
TinyString name = std::move(st->name);
Date build_date = st->build_date;
/* TTDPatch could use "buoys with rail station" for rail waypoints */
bool train = st->train_station.tile != INVALID_TILE;
@ -80,7 +80,7 @@ void MoveBuoysToWaypoints()
Waypoint *wp = new (index) Waypoint(xy);
wp->town = town;
wp->string_id = train ? STR_SV_STNAME_WAYPOINT : STR_SV_STNAME_BUOY;
wp->name = name;
wp->name = std::move(name);
wp->delete_ctr = 0; // Just reset delete counter for once.
wp->build_date = build_date;
wp->owner = train ? GetTileOwner(xy) : OWNER_NONE;
@ -178,7 +178,7 @@ static const SaveLoad _old_station_desc[] = {
SLE_CONDNULL(1, SL_MIN_VERSION, SLV_4), ///< alpha_order
SLE_VAR(Station, string_id, SLE_STRINGID),
SLE_CONDSSTR(Station, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
SLE_CONDSTR(Station, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION),
SLE_CONDVAR(Station, indtype, SLE_UINT8, SLV_103, SL_MAX_VERSION),
SLE_CONDVAR(Station, had_vehicle_of_type, SLE_FILE_U16 | SLE_VAR_U8, SL_MIN_VERSION, SLV_122),
SLE_CONDVAR(Station, had_vehicle_of_type, SLE_UINT8, SLV_122, SL_MAX_VERSION),
@ -399,7 +399,8 @@ static const SaveLoad _base_station_desc[] = {
SLE_VAR(BaseStation, xy, SLE_UINT32),
SLE_REF(BaseStation, town, REF_TOWN),
SLE_VAR(BaseStation, string_id, SLE_STRINGID),
SLE_SSTR(BaseStation, name, SLE_STR | SLF_ALLOW_CONTROL),
SLE_STR(BaseStation, name, SLE_STR | SLF_ALLOW_CONTROL, 0),
SLE_CONDVAR_X(Station, delete_ctr, SLE_UINT8, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_SPRINGPP, 0, 3)),
SLE_CONDVAR_X(Station, delete_ctr, SLE_FILE_U16 | SLE_VAR_U8, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_SPRINGPP, 4)),
SLE_VAR(BaseStation, owner, SLE_UINT8),

@ -133,7 +133,7 @@ static const SaveLoad _town_desc[] = {
SLE_CONDVAR(Town, townnamegrfid, SLE_UINT32, SLV_66, SL_MAX_VERSION),
SLE_VAR(Town, townnametype, SLE_UINT16),
SLE_VAR(Town, townnameparts, SLE_UINT32),
SLE_CONDSSTR(Town, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
SLE_CONDSTR(Town, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION),
SLE_VAR(Town, flags, SLE_UINT8),
SLE_CONDVAR(Town, statues, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_104),

@ -621,8 +621,8 @@ const SaveLoad *GetVehicleDescription(VehicleType vt)
SLE_VAR(Vehicle, subtype, SLE_UINT8),
SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
SLE_CONDVAR(Vehicle, name, SLE_NAME, SL_MIN_VERSION, SLV_84),
SLE_CONDSSTR(Vehicle, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
SLE_CONDVAR(Vehicle, name, SLE_CNAME, SL_MIN_VERSION, SLV_84),
SLE_CONDSTR(Vehicle, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION),
SLE_CONDVAR(Vehicle, unitnumber, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_8),
SLE_CONDVAR(Vehicle, unitnumber, SLE_UINT16, SLV_8, SL_MAX_VERSION),
SLE_VAR(Vehicle, owner, SLE_UINT8),

@ -29,7 +29,7 @@ struct OldWaypoint {
Town *town;
uint16 town_cn;
StringID string_id;
std::string name;
TinyString name;
uint8 delete_ctr;
Date build_date;
uint8 localidx;
@ -118,7 +118,7 @@ void MoveWaypointsToBaseStations()
Waypoint *new_wp = new Waypoint(t);
new_wp->town = wp.town;
new_wp->town_cn = wp.town_cn;
new_wp->name = wp.name;
new_wp->name = std::move(wp.name);
new_wp->delete_ctr = 0; // Just reset delete counter for once.
new_wp->build_date = wp.build_date;
new_wp->owner = wp.owner;
@ -172,7 +172,7 @@ static const SaveLoad _old_waypoint_desc[] = {
SLE_CONDVAR(OldWaypoint, town_cn, SLE_FILE_U8 | SLE_VAR_U16, SLV_12, SLV_89),
SLE_CONDVAR(OldWaypoint, town_cn, SLE_UINT16, SLV_89, SL_MAX_VERSION),
SLE_CONDVAR(OldWaypoint, string_id, SLE_STRINGID, SL_MIN_VERSION, SLV_84),
SLE_CONDSSTR(OldWaypoint, name, SLE_STR, SLV_84, SL_MAX_VERSION),
SLE_CONDSTR(OldWaypoint, name, SLE_STR, 0, SLV_84, SL_MAX_VERSION),
SLE_VAR(OldWaypoint, delete_ctr, SLE_UINT8),
SLE_CONDVAR(OldWaypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32, SLV_3, SLV_31),

@ -19,6 +19,7 @@
#include "openttd.h"
#include "table/strings.h"
#include "company_func.h"
#include "core/tinystring_type.hpp"
#include <list>
#include <memory>
@ -63,7 +64,7 @@ struct Town : TownPool::PoolItem<&_town_pool> {
uint32 townnamegrfid;
uint16 townnametype;
uint32 townnameparts;
std::string name; ///< Custom town name. If empty, the town was not renamed and uses the generated name.
TinyString name; ///< Custom town name. If empty, the town was not renamed and uses the generated name.
mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the town, if not using a custom town name
byte flags; ///< See #TownFlags.

@ -801,29 +801,30 @@ static bool IsUniqueVehicleName(const char *name)
static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
{
std::string buf;
std::string src_name = src->name.c_str();
/* Find the position of the first digit in the last group of digits. */
size_t number_position;
for (number_position = src->name.length(); number_position > 0; number_position--) {
for (number_position = src_name.length(); number_position > 0; number_position--) {
/* The design of UTF-8 lets this work simply without having to check
* for UTF-8 sequences. */
if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
if (src_name[number_position - 1] < '0' || src_name[number_position - 1] > '9') break;
}
/* Format buffer and determine starting number. */
long num;
byte padding = 0;
if (number_position == src->name.length()) {
if (number_position == src_name.length()) {
/* No digit at the end, so start at number 2. */
buf = src->name;
buf = src_name;
buf += " ";
number_position = buf.length();
num = 2;
} else {
/* Found digits, parse them and start at the next number. */
buf = src->name.substr(0, number_position);
buf = src_name.substr(0, number_position);
auto num_str = src->name.substr(number_position);
auto num_str = src_name.substr(number_position);
padding = (byte)num_str.length();
std::istringstream iss(num_str);

Loading…
Cancel
Save