Merge pull request #87874 from AThousandShips/sort_fix

Fix sorting of files/dirs in dialogs
This commit is contained in:
Rémi Verschelde 2024-03-24 01:13:12 +01:00
commit f0544ebba6
No known key found for this signature in database
GPG key ID: C3336907360768E1
13 changed files with 181 additions and 93 deletions

View file

@ -927,52 +927,106 @@ static _FORCE_INLINE_ signed char natural_cmp_common(const char32_t *&r_this_str
return 0;
}
static _FORCE_INLINE_ signed char naturalcasecmp_to_base(const char32_t *p_this_str, const char32_t *p_that_str) {
if (p_this_str && p_that_str) {
while (*p_this_str == '.' || *p_that_str == '.') {
if (*p_this_str++ != '.') {
return 1;
}
if (*p_that_str++ != '.') {
return -1;
}
if (!*p_that_str) {
return 1;
}
if (!*p_this_str) {
return -1;
}
}
while (*p_this_str) {
if (!*p_that_str) {
return 1;
} else if (is_digit(*p_this_str)) {
if (!is_digit(*p_that_str)) {
return -1;
}
signed char ret = natural_cmp_common(p_this_str, p_that_str);
if (ret) {
return ret;
}
} else if (is_digit(*p_that_str)) {
return 1;
} else {
if (*p_this_str < *p_that_str) { // If current character in this is less, we are less.
return -1;
} else if (*p_this_str > *p_that_str) { // If current character in this is greater, we are greater.
return 1;
}
p_this_str++;
p_that_str++;
}
}
if (*p_that_str) {
return -1;
}
}
return 0;
}
signed char String::naturalcasecmp_to(const String &p_str) const {
const char32_t *this_str = get_data();
const char32_t *that_str = p_str.get_data();
if (this_str && that_str) {
while (*this_str == '.' || *that_str == '.') {
if (*this_str++ != '.') {
return naturalcasecmp_to_base(this_str, that_str);
}
static _FORCE_INLINE_ signed char naturalnocasecmp_to_base(const char32_t *p_this_str, const char32_t *p_that_str) {
if (p_this_str && p_that_str) {
while (*p_this_str == '.' || *p_that_str == '.') {
if (*p_this_str++ != '.') {
return 1;
}
if (*that_str++ != '.') {
if (*p_that_str++ != '.') {
return -1;
}
if (!*that_str) {
if (!*p_that_str) {
return 1;
}
if (!*this_str) {
if (!*p_this_str) {
return -1;
}
}
while (*this_str) {
if (!*that_str) {
while (*p_this_str) {
if (!*p_that_str) {
return 1;
} else if (is_digit(*this_str)) {
if (!is_digit(*that_str)) {
} else if (is_digit(*p_this_str)) {
if (!is_digit(*p_that_str)) {
return -1;
}
signed char ret = natural_cmp_common(this_str, that_str);
signed char ret = natural_cmp_common(p_this_str, p_that_str);
if (ret) {
return ret;
}
} else if (is_digit(*that_str)) {
} else if (is_digit(*p_that_str)) {
return 1;
} else {
if (*this_str < *that_str) { // If current character in this is less, we are less.
if (_find_upper(*p_this_str) < _find_upper(*p_that_str)) { // If current character in this is less, we are less.
return -1;
} else if (*this_str > *that_str) { // If current character in this is greater, we are greater.
} else if (_find_upper(*p_this_str) > _find_upper(*p_that_str)) { // If current character in this is greater, we are greater.
return 1;
}
this_str++;
that_str++;
p_this_str++;
p_that_str++;
}
}
if (*that_str) {
if (*p_that_str) {
return -1;
}
}
@ -984,55 +1038,50 @@ signed char String::naturalnocasecmp_to(const String &p_str) const {
const char32_t *this_str = get_data();
const char32_t *that_str = p_str.get_data();
if (this_str && that_str) {
while (*this_str == '.' || *that_str == '.') {
if (*this_str++ != '.') {
return 1;
}
if (*that_str++ != '.') {
return -1;
}
if (!*that_str) {
return 1;
}
if (!*this_str) {
return -1;
}
}
return naturalnocasecmp_to_base(this_str, that_str);
}
while (*this_str) {
if (!*that_str) {
return 1;
} else if (is_digit(*this_str)) {
if (!is_digit(*that_str)) {
return -1;
}
signed char ret = natural_cmp_common(this_str, that_str);
if (ret) {
return ret;
}
} else if (is_digit(*that_str)) {
return 1;
} else {
if (_find_upper(*this_str) < _find_upper(*that_str)) { // If current character in this is less, we are less.
return -1;
} else if (_find_upper(*this_str) > _find_upper(*that_str)) { // If current character in this is greater, we are greater.
return 1;
}
this_str++;
that_str++;
}
static _FORCE_INLINE_ signed char file_cmp_common(const char32_t *&r_this_str, const char32_t *&r_that_str) {
// Compare leading `_` sequences.
while (*r_this_str && *r_that_str) {
// Sort `_` lower than everything except `.`
if (*r_this_str != '_' && *r_that_str == '_') {
return *r_this_str == '.' ? -1 : 1;
}
if (*that_str) {
return -1;
if (*r_this_str == '_' && *r_that_str != '_') {
return *r_that_str == '.' ? 1 : -1;
}
r_this_str++;
r_that_str++;
}
return 0;
}
signed char String::filecasecmp_to(const String &p_str) const {
const char32_t *this_str = get_data();
const char32_t *that_str = p_str.get_data();
signed char ret = file_cmp_common(this_str, that_str);
if (ret) {
return ret;
}
return naturalcasecmp_to_base(this_str, that_str);
}
signed char String::filenocasecmp_to(const String &p_str) const {
const char32_t *this_str = get_data();
const char32_t *that_str = p_str.get_data();
signed char ret = file_cmp_common(this_str, that_str);
if (ret) {
return ret;
}
return naturalnocasecmp_to_base(this_str, that_str);
}
const char32_t *String::get_data() const {
static const char32_t zero = 0;
return size() ? &operator[](0) : &zero;

View file

@ -265,6 +265,9 @@ public:
signed char nocasecmp_to(const String &p_str) const;
signed char naturalcasecmp_to(const String &p_str) const;
signed char naturalnocasecmp_to(const String &p_str) const;
// Special sorting for file names. Names starting with `_` are put before all others except those starting with `.`, otherwise natural comparison is used.
signed char filecasecmp_to(const String &p_str) const;
signed char filenocasecmp_to(const String &p_str) const;
const char32_t *get_data() const;
/* standard size stuff */
@ -499,6 +502,12 @@ struct NaturalNoCaseComparator {
}
};
struct FileNoCaseComparator {
bool operator()(const String &p_a, const String &p_b) const {
return p_a.filenocasecmp_to(p_b) < 0;
}
};
template <typename L, typename R>
_FORCE_INLINE_ bool is_str_less(const L *l_ptr, const R *r_ptr) {
while (true) {

View file

@ -1644,6 +1644,8 @@ static void _register_variant_builtin_methods() {
bind_string_method(nocasecmp_to, sarray("to"), varray());
bind_string_method(naturalcasecmp_to, sarray("to"), varray());
bind_string_method(naturalnocasecmp_to, sarray("to"), varray());
bind_string_method(filecasecmp_to, sarray("to"), varray());
bind_string_method(filenocasecmp_to, sarray("to"), varray());
bind_string_method(length, sarray(), varray());
bind_string_method(substr, sarray("from", "len"), varray(-1));
bind_string_method(get_slice, sarray("delimiter", "slice"), varray());

View file

@ -112,7 +112,7 @@
<description>
Performs a case-sensitive comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" and "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order.
With different string lengths, returns [code]1[/code] if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is [i]always[/i] [code]0[/code].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method nocasecmp_to], [method naturalcasecmp_to], and [method naturalnocasecmp_to].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method nocasecmp_to], [method filecasecmp_to], and [method naturalcasecmp_to].
</description>
</method>
<method name="chr" qualifiers="static">
@ -184,6 +184,22 @@
Returns a string with [param chars] characters erased starting from [param position]. If [param chars] goes beyond the string's length given the specified [param position], fewer characters will be erased from the returned string. Returns an empty string if either [param position] or [param chars] is negative. Returns the original string unmodified if [param chars] is [code]0[/code].
</description>
</method>
<method name="filecasecmp_to" qualifiers="const">
<return type="int" />
<param index="0" name="to" type="String" />
<description>
Like [method naturalcasecmp_to] but prioritises strings that begin with periods ([code].[/code]) and underscores ([code]_[/code]) before any other character. Useful when sorting folders or file names.
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method filenocasecmp_to], [method naturalcasecmp_to], and [method casecmp_to].
</description>
</method>
<method name="filenocasecmp_to" qualifiers="const">
<return type="int" />
<param index="0" name="to" type="String" />
<description>
Like [method naturalnocasecmp_to] but prioritises strings that begin with periods ([code].[/code]) and underscores ([code]_[/code]) before any other character. Useful when sorting folders or file names.
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method filecasecmp_to], [method naturalnocasecmp_to], and [method nocasecmp_to].
</description>
</method>
<method name="find" qualifiers="const">
<return type="int" />
<param index="0" name="what" type="String" />
@ -586,7 +602,7 @@
Performs a [b]case-sensitive[/b], [i]natural order[/i] comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" or "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order.
When used for sorting, natural order comparison orders sequences of numbers by the combined value of each digit as is often expected, instead of the single digit's value. A sorted sequence of numbered strings will be [code]["1", "2", "3", ...][/code], not [code]["1", "10", "2", "3", ...][/code].
With different string lengths, returns [code]1[/code] if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is [i]always[/i] [code]0[/code].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method naturalnocasecmp_to], [method nocasecmp_to], and [method casecmp_to].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method naturalnocasecmp_to], [method filecasecmp_to], and [method nocasecmp_to].
</description>
</method>
<method name="naturalnocasecmp_to" qualifiers="const">
@ -596,7 +612,7 @@
Performs a [b]case-insensitive[/b], [i]natural order[/i] comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" or "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order. Internally, lowercase characters are converted to uppercase for the comparison.
When used for sorting, natural order comparison orders sequences of numbers by the combined value of each digit as is often expected, instead of the single digit's value. A sorted sequence of numbered strings will be [code]["1", "2", "3", ...][/code], not [code]["1", "10", "2", "3", ...][/code].
With different string lengths, returns [code]1[/code] if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is [i]always[/i] [code]0[/code].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method naturalcasecmp_to], [method nocasecmp_to], and [method casecmp_to].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method naturalcasecmp_to], [method filenocasecmp_to], and [method casecmp_to].
</description>
</method>
<method name="nocasecmp_to" qualifiers="const">
@ -605,7 +621,7 @@
<description>
Performs a [b]case-insensitive[/b] comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" or "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order. Internally, lowercase characters are converted to uppercase for the comparison.
With different string lengths, returns [code]1[/code] if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is [i]always[/i] [code]0[/code].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method casecmp_to], [method naturalcasecmp_to], and [method naturalnocasecmp_to].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method casecmp_to], [method filenocasecmp_to], and [method naturalnocasecmp_to].
</description>
</method>
<method name="num" qualifiers="static">

View file

@ -107,7 +107,7 @@
<description>
Performs a case-sensitive comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" and "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order.
With different string lengths, returns [code]1[/code] if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is [i]always[/i] [code]0[/code].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method nocasecmp_to], [method naturalcasecmp_to], and [method naturalnocasecmp_to].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method nocasecmp_to], [method filecasecmp_to], and [method naturalcasecmp_to].
</description>
</method>
<method name="contains" qualifiers="const">
@ -168,6 +168,22 @@
Returns a string with [param chars] characters erased starting from [param position]. If [param chars] goes beyond the string's length given the specified [param position], fewer characters will be erased from the returned string. Returns an empty string if either [param position] or [param chars] is negative. Returns the original string unmodified if [param chars] is [code]0[/code].
</description>
</method>
<method name="filecasecmp_to" qualifiers="const">
<return type="int" />
<param index="0" name="to" type="String" />
<description>
Like [method naturalcasecmp_to] but prioritises strings that begin with periods ([code].[/code]) and underscores ([code]_[/code]) before any other character. Useful when sorting folders or file names.
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method filenocasecmp_to], [method naturalcasecmp_to], and [method casecmp_to].
</description>
</method>
<method name="filenocasecmp_to" qualifiers="const">
<return type="int" />
<param index="0" name="to" type="String" />
<description>
Like [method naturalnocasecmp_to] but prioritises strings that begin with periods ([code].[/code]) and underscores ([code]_[/code]) before any other character. Useful when sorting folders or file names.
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method filecasecmp_to], [method naturalnocasecmp_to], and [method nocasecmp_to].
</description>
</method>
<method name="find" qualifiers="const">
<return type="int" />
<param index="0" name="what" type="String" />
@ -562,7 +578,7 @@
Performs a [b]case-sensitive[/b], [i]natural order[/i] comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" or "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order.
When used for sorting, natural order comparison orders sequences of numbers by the combined value of each digit as is often expected, instead of the single digit's value. A sorted sequence of numbered strings will be [code]["1", "2", "3", ...][/code], not [code]["1", "10", "2", "3", ...][/code].
With different string lengths, returns [code]1[/code] if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is [i]always[/i] [code]0[/code].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method naturalnocasecmp_to], [method nocasecmp_to], and [method casecmp_to].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method naturalnocasecmp_to], [method filecasecmp_to], and [method nocasecmp_to].
</description>
</method>
<method name="naturalnocasecmp_to" qualifiers="const">
@ -572,7 +588,7 @@
Performs a [b]case-insensitive[/b], [i]natural order[/i] comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" or "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order. Internally, lowercase characters are converted to uppercase for the comparison.
When used for sorting, natural order comparison orders sequences of numbers by the combined value of each digit as is often expected, instead of the single digit's value. A sorted sequence of numbered strings will be [code]["1", "2", "3", ...][/code], not [code]["1", "10", "2", "3", ...][/code].
With different string lengths, returns [code]1[/code] if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is [i]always[/i] [code]0[/code].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method naturalcasecmp_to], [method nocasecmp_to], and [method casecmp_to].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method naturalcasecmp_to], [method filenocasecmp_to], and [method casecmp_to].
</description>
</method>
<method name="nocasecmp_to" qualifiers="const">
@ -581,7 +597,7 @@
<description>
Performs a [b]case-insensitive[/b] comparison to another string. Returns [code]-1[/code] if less than, [code]1[/code] if greater than, or [code]0[/code] if equal. "Less than" or "greater than" are determined by the [url=https://en.wikipedia.org/wiki/List_of_Unicode_characters]Unicode code points[/url] of each string, which roughly matches the alphabetical order. Internally, lowercase characters are converted to uppercase for the comparison.
With different string lengths, returns [code]1[/code] if this string is longer than the [param to] string, or [code]-1[/code] if shorter. Note that the length of empty strings is [i]always[/i] [code]0[/code].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method casecmp_to], [method naturalcasecmp_to], and [method naturalnocasecmp_to].
To get a [bool] result from a string comparison, use the [code]==[/code] operator instead. See also [method casecmp_to], [method filenocasecmp_to], and [method naturalnocasecmp_to].
</description>
</method>
<method name="pad_decimals" qualifiers="const">

View file

@ -50,10 +50,6 @@ EditorFileSystem *EditorFileSystem::singleton = nullptr;
//the name is the version, to keep compatibility with different versions of Godot
#define CACHE_FILE_NAME "filesystem_cache8"
void EditorFileSystemDirectory::sort_files() {
files.sort_custom<FileInfoSort>();
}
int EditorFileSystemDirectory::find_file_index(const String &p_file) const {
for (int i = 0; i < files.size(); i++) {
if (files[i]->file == p_file) {
@ -578,7 +574,7 @@ bool EditorFileSystem::_update_scan_actions() {
case ItemAction::ACTION_DIR_ADD: {
int idx = 0;
for (int i = 0; i < ia.dir->subdirs.size(); i++) {
if (ia.new_dir->name.naturalnocasecmp_to(ia.dir->subdirs[i]->name) < 0) {
if (ia.new_dir->name.filenocasecmp_to(ia.dir->subdirs[i]->name) < 0) {
break;
}
idx++;
@ -600,7 +596,7 @@ bool EditorFileSystem::_update_scan_actions() {
case ItemAction::ACTION_FILE_ADD: {
int idx = 0;
for (int i = 0; i < ia.dir->files.size(); i++) {
if (ia.new_file->file.naturalnocasecmp_to(ia.dir->files[i]->file) < 0) {
if (ia.new_file->file.filenocasecmp_to(ia.dir->files[i]->file) < 0) {
break;
}
idx++;
@ -810,8 +806,8 @@ void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, Ref<DirAc
da->list_dir_end();
dirs.sort_custom<NaturalNoCaseComparator>();
files.sort_custom<NaturalNoCaseComparator>();
dirs.sort_custom<FileNoCaseComparator>();
files.sort_custom<FileNoCaseComparator>();
int total = dirs.size() + files.size();
int idx = 0;
@ -832,7 +828,7 @@ void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, Ref<DirAc
int idx2 = 0;
for (int i = 0; i < p_dir->subdirs.size(); i++) {
if (efd->name.naturalnocasecmp_to(p_dir->subdirs[i]->name) < 0) {
if (efd->name.filenocasecmp_to(p_dir->subdirs[i]->name) < 0) {
break;
}
idx2++;
@ -1409,7 +1405,7 @@ bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirector
int idx2 = 0;
for (int j = 0; j < fs->get_subdir_count(); j++) {
if (efsd->name.naturalnocasecmp_to(fs->get_subdir(j)->get_name()) < 0) {
if (efsd->name.filenocasecmp_to(fs->get_subdir(j)->get_name()) < 0) {
break;
}
idx2++;
@ -1766,7 +1762,7 @@ void EditorFileSystem::update_file(const String &p_file) {
String file_name = p_file.get_file();
for (int i = 0; i < fs->files.size(); i++) {
if (p_file.naturalnocasecmp_to(fs->files[i]->file) < 0) {
if (p_file.filenocasecmp_to(fs->files[i]->file) < 0) {
break;
}
idx++;

View file

@ -68,14 +68,6 @@ class EditorFileSystemDirectory : public Object {
String script_class_icon_path;
};
struct FileInfoSort {
bool operator()(const FileInfo *p_a, const FileInfo *p_b) const {
return p_a->file < p_b->file;
}
};
void sort_files();
Vector<FileInfo *> files;
static void _bind_methods();

View file

@ -885,7 +885,7 @@ void FileSystemDock::_search(EditorFileSystemDirectory *p_path, List<FileInfo> *
struct FileSystemDock::FileInfoTypeComparator {
bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
return NaturalNoCaseComparator()(p_a.name.get_extension() + p_a.type + p_a.name.get_basename(), p_b.name.get_extension() + p_b.type + p_b.name.get_basename());
return FileNoCaseComparator()(p_a.name.get_extension() + p_a.type + p_a.name.get_basename(), p_b.name.get_extension() + p_b.type + p_b.name.get_basename());
}
};
@ -2043,7 +2043,7 @@ Vector<String> FileSystemDock::_tree_get_selected(bool remove_self_inclusion, bo
Vector<String> FileSystemDock::_remove_self_included_paths(Vector<String> selected_strings) {
// Remove paths or files that are included into another.
if (selected_strings.size() > 1) {
selected_strings.sort_custom<NaturalNoCaseComparator>();
selected_strings.sort_custom<FileNoCaseComparator>();
String last_path = "";
for (int i = 0; i < selected_strings.size(); i++) {
if (!last_path.is_empty() && selected_strings[i].begins_with(last_path)) {

View file

@ -332,7 +332,7 @@ private:
uint64_t modified_time = 0;
bool operator<(const FileInfo &fi) const {
return NaturalNoCaseComparator()(name, fi.name);
return FileNoCaseComparator()(name, fi.name);
}
};

View file

@ -874,8 +874,8 @@ void EditorFileDialog::update_file_list() {
item = dir_access->get_next();
}
dirs.sort_custom<NaturalNoCaseComparator>();
files.sort_custom<NaturalNoCaseComparator>();
dirs.sort_custom<FileNoCaseComparator>();
files.sort_custom<FileNoCaseComparator>();
while (!dirs.is_empty()) {
const String &dir_name = dirs.front()->get();

View file

@ -1892,7 +1892,7 @@ struct _ScriptEditorItemData {
if (sort_key == id.sort_key) {
return index < id.index;
} else {
return sort_key.naturalnocasecmp_to(id.sort_key) < 0;
return sort_key.filenocasecmp_to(id.sort_key) < 0;
}
} else {
return category < id.category;

View file

@ -671,8 +671,8 @@ void FileDialog::update_file_list() {
item = dir_access->get_next();
}
dirs.sort_custom<NaturalNoCaseComparator>();
files.sort_custom<NaturalNoCaseComparator>();
dirs.sort_custom<FileNoCaseComparator>();
files.sort_custom<FileNoCaseComparator>();
while (!dirs.is_empty()) {
String &dir_name = dirs.front()->get();

View file

@ -336,6 +336,14 @@ TEST_CASE("[String] Natural compare function test") {
CHECK(a.naturalnocasecmp_to("img10.png") < 0);
}
TEST_CASE("[String] File compare function test") {
String a = "_img2.png";
CHECK(a.nocasecmp_to("img10.png") > 0);
CHECK_MESSAGE(a.filenocasecmp_to("img10.png") < 0, "Should sort before letters.");
CHECK_MESSAGE(a.filenocasecmp_to(".img10.png") > 0, "Should sort after period.");
}
TEST_CASE("[String] hex_encode_buffer") {
static const uint8_t u8str[] = { 0x45, 0xE3, 0x81, 0x8A, 0x8F, 0xE3 };
String s = String::hex_encode_buffer(u8str, 6);