From e4145fa338c1a21743d8c4debf8290fff4aad093 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 27 Oct 2024 01:02:04 +0100 Subject: [PATCH] Fix bb8a0c7641: Strip control codes before sorting NewGRF names. Now that SkipGarbage doesn't skip all multi-byte utf-8 characters, string control codes are not skipped either. This gave unintended sorting when NewGRF names start with colour codes. Use StrMakeValid() before comparing. This has to make a copy of the string for each sort step, so there is likely a performance penalty. --- src/newgrf_gui.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index a4e73f626a..d6fdc75849 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -1448,7 +1448,9 @@ private: /** Sort grfs by name. */ static bool NameSorter(const GRFConfig * const &a, const GRFConfig * const &b) { - int i = StrNaturalCompare(a->GetName(), b->GetName(), true); // Sort by name (natural sorting). + std::string name_a = StrMakeValid(a->GetName(), SVS_NONE); // Make a copy without control codes. + std::string name_b = StrMakeValid(b->GetName(), SVS_NONE); // Make a copy without control codes. + int i = StrNaturalCompare(name_a, name_b, true); // Sort by name (natural sorting). if (i != 0) return i < 0; i = a->version - b->version;