mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-09 23:50:25 +00:00
Fix: Violation of strict weak ordering in group name sorters
This could be caused by a group being renamed, and the old name being cached from a previous sort. See: #7838
This commit is contained in:
parent
d830a34394
commit
c167648d75
@ -615,28 +615,6 @@ private:
|
|||||||
ShowDropDownList(this, std::move(list), sel, widget);
|
ShowDropDownList(this, std::move(list), sel, widget);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool GroupNameSorter(const Group * const &a, const Group * const &b)
|
|
||||||
{
|
|
||||||
static const Group *last_group[2] = { nullptr, nullptr };
|
|
||||||
static char last_name[2][64] = { "", "" };
|
|
||||||
|
|
||||||
if (a != last_group[0]) {
|
|
||||||
last_group[0] = a;
|
|
||||||
SetDParam(0, a->index);
|
|
||||||
GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (b != last_group[1]) {
|
|
||||||
last_group[1] = b;
|
|
||||||
SetDParam(0, b->index);
|
|
||||||
GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
|
|
||||||
}
|
|
||||||
|
|
||||||
int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
|
|
||||||
if (r == 0) return a->index < b->index;
|
|
||||||
return r < 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddChildren(GUIGroupList *source, GroupID parent, int indent)
|
void AddChildren(GUIGroupList *source, GroupID parent, int indent)
|
||||||
{
|
{
|
||||||
for (const Group *g : *source) {
|
for (const Group *g : *source) {
|
||||||
@ -665,7 +643,27 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
list.ForceResort();
|
list.ForceResort();
|
||||||
list.Sort(&GroupNameSorter);
|
|
||||||
|
/* Sort the groups by their name */
|
||||||
|
const Group *last_group[2] = { nullptr, nullptr };
|
||||||
|
char last_name[2][64] = { "", "" };
|
||||||
|
list.Sort([&](const Group * const &a, const Group * const &b) -> bool {
|
||||||
|
if (a != last_group[0]) {
|
||||||
|
last_group[0] = a;
|
||||||
|
SetDParam(0, a->index);
|
||||||
|
GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (b != last_group[1]) {
|
||||||
|
last_group[1] = b;
|
||||||
|
SetDParam(0, b->index);
|
||||||
|
GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
|
||||||
|
if (r == 0) return a->index < b->index;
|
||||||
|
return r < 0;
|
||||||
|
});
|
||||||
|
|
||||||
AddChildren(&list, INVALID_GROUP, 0);
|
AddChildren(&list, INVALID_GROUP, 0);
|
||||||
}
|
}
|
||||||
|
@ -141,29 +141,6 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sort the groups by their name */
|
|
||||||
static bool GroupNameSorter(const Group * const &a, const Group * const &b)
|
|
||||||
{
|
|
||||||
static const Group *last_group[2] = { nullptr, nullptr };
|
|
||||||
static char last_name[2][64] = { "", "" };
|
|
||||||
|
|
||||||
if (a != last_group[0]) {
|
|
||||||
last_group[0] = a;
|
|
||||||
SetDParam(0, a->index);
|
|
||||||
GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (b != last_group[1]) {
|
|
||||||
last_group[1] = b;
|
|
||||||
SetDParam(0, b->index);
|
|
||||||
GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
|
|
||||||
}
|
|
||||||
|
|
||||||
int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
|
|
||||||
if (r == 0) return a->index < b->index;
|
|
||||||
return r < 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* (Re)Build the group list.
|
* (Re)Build the group list.
|
||||||
*
|
*
|
||||||
@ -185,7 +162,27 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
list.ForceResort();
|
list.ForceResort();
|
||||||
list.Sort(&GroupNameSorter);
|
|
||||||
|
/* Sort the groups by their name */
|
||||||
|
const Group *last_group[2] = { nullptr, nullptr };
|
||||||
|
char last_name[2][64] = { "", "" };
|
||||||
|
list.Sort([&](const Group * const &a, const Group * const &b) {
|
||||||
|
if (a != last_group[0]) {
|
||||||
|
last_group[0] = a;
|
||||||
|
SetDParam(0, a->index);
|
||||||
|
GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (b != last_group[1]) {
|
||||||
|
last_group[1] = b;
|
||||||
|
SetDParam(0, b->index);
|
||||||
|
GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
|
||||||
|
if (r == 0) return a->index < b->index;
|
||||||
|
return r < 0;
|
||||||
|
});
|
||||||
|
|
||||||
AddChildren(&list, INVALID_GROUP, 0);
|
AddChildren(&list, INVALID_GROUP, 0);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user