changeset 20050:f8312ae70bc6 draft

(svn r24983) -Change: Apply the same name sorting rules to content and NewGRF list as for the server list.
author frosch <frosch@openttd.org>
date Sat, 09 Feb 2013 17:31:07 +0000
parents cac151934646
children cb9b29e8450c
files src/network/network_content_gui.cpp src/network/network_gui.cpp src/newgrf_gui.cpp src/string.cpp src/string_func.h
diffstat 5 files changed, 24 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/network/network_content_gui.cpp
+++ b/src/network/network_content_gui.cpp
@@ -323,7 +323,7 @@
 	/** Sort content by name. */
 	static int CDECL NameSorter(const ContentInfo * const *a, const ContentInfo * const *b)
 	{
-		return strnatcmp((*a)->name, (*b)->name); // Sort by name (natural sorting).
+		return strnatcmp((*a)->name, (*b)->name, true); // Sort by name (natural sorting).
 	}
 
 	/** Sort content by type. */
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -263,24 +263,10 @@
 		this->UpdateListPos();
 	}
 
-	/**
-	 * Skip some of the 'garbage' in the string that we don't want to use
-	 * to sort on. This way the alphabetical sorting will work better as
-	 * we would be actually using those characters instead of some other
-	 * characters such as spaces and tildes at the begin of the name.
-	 * @param str The string to skip the initial garbage of.
-	 * @return The string with the garbage skipped.
-	 */
-	static const char *SkipGarbage(const char *str)
-	{
-		while (*str != '\0' && (*str < 'A' || IsInsideMM(*str, '[', '`' + 1) || IsInsideMM(*str, '{', '~' + 1))) str++;
-		return str;
-	}
-
 	/** Sort servers by name. */
 	static int CDECL NGameNameSorter(NetworkGameList * const *a, NetworkGameList * const *b)
 	{
-		int r = strnatcmp(SkipGarbage((*a)->info.server_name), SkipGarbage((*b)->info.server_name)); // Sort by name (natural sorting).
+		int r = strnatcmp((*a)->info.server_name, (*b)->info.server_name, true); // Sort by name (natural sorting).
 		return r == 0 ? (*a)->address.CompareTo((*b)->address) : r;
 	}
 
--- a/src/newgrf_gui.cpp
+++ b/src/newgrf_gui.cpp
@@ -1368,7 +1368,7 @@
 	/** Sort grfs by name. */
 	static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b)
 	{
-		int i = strnatcmp((*a)->GetName(), (*b)->GetName()); // Sort by name (natural sorting).
+		int i = strnatcmp((*a)->GetName(), (*b)->GetName(), true); // Sort by name (natural sorting).
 		if (i != 0) return i;
 
 		i = (*a)->version - (*b)->version;
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -597,14 +597,33 @@
 #endif /* DEFINE_STRCASESTR */
 
 /**
+ * Skip some of the 'garbage' in the string that we don't want to use
+ * to sort on. This way the alphabetical sorting will work better as
+ * we would be actually using those characters instead of some other
+ * characters such as spaces and tildes at the begin of the name.
+ * @param str The string to skip the initial garbage of.
+ * @return The string with the garbage skipped.
+ */
+static const char *SkipGarbage(const char *str)
+{
+	while (*str != '\0' && (*str < 'A' || IsInsideMM(*str, '[', '`' + 1) || IsInsideMM(*str, '{', '~' + 1))) str++;
+	return str;
+}
+
+/**
  * Compares two strings using case insensitive natural sort.
  *
  * @param s1 First string to compare.
  * @param s2 Second string to compare.
+ * @param ignore_garbage_at_front Skip punctuation characters in the front
  * @return Less than zero if s1 < s2, zero if s1 == s2, greater than zero if s1 > s2.
  */
-int strnatcmp(const char *s1, const char *s2)
+int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
 {
+	if (ignore_garbage_at_front) {
+		s1 = SkipGarbage(s1);
+		s2 = SkipGarbage(s2);
+	}
 #ifdef WITH_ICU
 	if (_current_collator != NULL) {
 		UErrorCode status = U_ZERO_ERROR;
--- a/src/string_func.h
+++ b/src/string_func.h
@@ -211,6 +211,6 @@
 char *strcasestr(const char *haystack, const char *needle);
 #endif /* strcasestr is available */
 
-int strnatcmp(const char *s1, const char *s2);
+int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front = false);
 
 #endif /* STRING_FUNC_H */