changeset 4863:7b78e791925f draft

(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.
author peter1138 <peter1138@openttd.org>
date Mon, 16 Oct 2006 10:29:29 +0000
parents 8c82a934382c
children 9d4e7c47f520
files vehicle_gui.c
diffstat 1 files changed, 19 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/vehicle_gui.c
+++ b/vehicle_gui.c
@@ -47,8 +47,6 @@
 } 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 @@
 	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 @@
 	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);