From cd337672babddc8a498fb1c67cf38504e049ea83 Mon Sep 17 00:00:00 2001 From: peter1138 Date: Mon, 16 Oct 2006 10:29:29 +0000 Subject: [PATCH] (svn r6789) - Codechange: Tidy up VehicleNameSorter() to use no global vars, and match the style of EngineNameSorter(). This also removes a bug where _internal_name_sorter_id was never set properly for anything other than trains. --- vehicle_gui.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/vehicle_gui.c b/vehicle_gui.c index 893182fe23..41bb10a67f 100644 --- a/vehicle_gui.c +++ b/vehicle_gui.c @@ -47,8 +47,6 @@ typedef struct vehiclelist_d { } vehiclelist_d; assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(vehiclelist_d)); -static uint32 _internal_name_sorter_id; // internal StringID for default vehicle-names -static const Vehicle* _last_vehicle; // cached vehicle to hopefully speed up name-sorting static bool _internal_sort_order; // descending/ascending static RailType _railtype_selected_in_replace_gui; @@ -149,8 +147,6 @@ static void SortVehicleList(vehiclelist_d *vl) if (!(vl->l.flags & VL_RESORT)) return; _internal_sort_order = vl->l.flags & VL_DESC; - _internal_name_sorter_id = STR_SV_TRAIN_NAME; - _last_vehicle = NULL; // used for "cache" in namesorting qsort((void*)vl->sort_list, vl->l.list_length, sizeof(vl->sort_list[0]), _vehicle_sorter[vl->l.sort_type]); @@ -508,25 +504,34 @@ static int CDECL VehicleNumberSorter(const void *a, const void *b) return (_internal_sort_order & 1) ? -r : r; } -static char _bufcache[64]; // used together with _last_vehicle to hopefully speed up stringsorting static int CDECL VehicleNameSorter(const void *a, const void *b) { + static const Vehicle *last_vehicle[2] = { NULL, NULL }; + static char last_name[2][64] = { "", "" }; + const Vehicle* va = *(const Vehicle**)a; const Vehicle* vb = *(const Vehicle**)b; - char buf1[64] = "\0"; int r; - if (va->string_id != _internal_name_sorter_id) GetString(buf1, va->string_id); - - if (vb != _last_vehicle) { - _last_vehicle = vb; - _bufcache[0] = '\0'; - if (vb->string_id != _internal_name_sorter_id) { - GetString(_bufcache, vb->string_id); + if (va != last_vehicle[0]) { + last_vehicle[0] = va; + if (IsCustomName(va->string_id)) { + GetString(last_name[0], va->string_id); + } else { + last_name[0][0] = '\0'; } } - r = strcmp(buf1, _bufcache); // sort by name + if (vb != last_vehicle[1]) { + last_vehicle[1] = vb; + if (IsCustomName(vb->string_id)) { + GetString(last_name[1], vb->string_id); + } else { + last_name[1][0] = '\0'; + } + } + + r = strcmp(last_name[0], last_name[1]); // sort by name VEHICLEUNITNUMBERSORTER(r, va, vb);