From 2cbf469912ae0e40049d3827db0c21cdf17c5a79 Mon Sep 17 00:00:00 2001 From: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> Date: Fri, 2 Feb 2024 16:50:23 +0100 Subject: [PATCH] Fix sorting of files/dirs in dialogs Sorts leading `_` before other characters except `.`. --- core/string/ustring.cpp | 165 +++++++++++++++--------- core/string/ustring.h | 9 ++ core/variant/variant_call.cpp | 2 + doc/classes/String.xml | 24 +++- doc/classes/StringName.xml | 24 +++- editor/editor_file_system.cpp | 18 +-- editor/editor_file_system.h | 8 -- editor/filesystem_dock.cpp | 4 +- editor/filesystem_dock.h | 2 +- editor/gui/editor_file_dialog.cpp | 4 +- editor/plugins/script_editor_plugin.cpp | 2 +- scene/gui/file_dialog.cpp | 4 +- tests/core/string/test_string.h | 8 ++ 13 files changed, 181 insertions(+), 93 deletions(-) diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index a7e12138f2f..dbc283946e5 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -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; diff --git a/core/string/ustring.h b/core/string/ustring.h index 0fb72fccd2a..fa904c8200a 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -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 _FORCE_INLINE_ bool is_str_less(const L *l_ptr, const R *r_ptr) { while (true) { diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 5f04c425361..ba7c44e4056 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -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()); diff --git a/doc/classes/String.xml b/doc/classes/String.xml index 17f953f48ff..75923426023 100644 --- a/doc/classes/String.xml +++ b/doc/classes/String.xml @@ -112,7 +112,7 @@ 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]. @@ -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]. + + + + + 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]. + + + + + + + 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]. + + @@ -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]. @@ -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]. @@ -605,7 +621,7 @@ 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]. diff --git a/doc/classes/StringName.xml b/doc/classes/StringName.xml index 41763489f1b..e837b65199c 100644 --- a/doc/classes/StringName.xml +++ b/doc/classes/StringName.xml @@ -107,7 +107,7 @@ 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]. @@ -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]. + + + + + 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]. + + + + + + + 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]. + + @@ -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]. @@ -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]. @@ -581,7 +597,7 @@ 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]. diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index ff42b824359..516b8f3d745 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -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(); -} - 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, Reflist_dir_end(); - dirs.sort_custom(); - files.sort_custom(); + dirs.sort_custom(); + files.sort_custom(); int total = dirs.size() + files.size(); int idx = 0; @@ -832,7 +828,7 @@ void EditorFileSystem::_scan_new_dir(EditorFileSystemDirectory *p_dir, Refsubdirs.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++; diff --git a/editor/editor_file_system.h b/editor/editor_file_system.h index 818d7252814..e45d6f8be37 100644 --- a/editor/editor_file_system.h +++ b/editor/editor_file_system.h @@ -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 files; static void _bind_methods(); diff --git a/editor/filesystem_dock.cpp b/editor/filesystem_dock.cpp index 0f83e109fa9..c1a2faeca82 100644 --- a/editor/filesystem_dock.cpp +++ b/editor/filesystem_dock.cpp @@ -885,7 +885,7 @@ void FileSystemDock::_search(EditorFileSystemDirectory *p_path, List * 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 FileSystemDock::_tree_get_selected(bool remove_self_inclusion, bo Vector FileSystemDock::_remove_self_included_paths(Vector selected_strings) { // Remove paths or files that are included into another. if (selected_strings.size() > 1) { - selected_strings.sort_custom(); + selected_strings.sort_custom(); String last_path = ""; for (int i = 0; i < selected_strings.size(); i++) { if (!last_path.is_empty() && selected_strings[i].begins_with(last_path)) { diff --git a/editor/filesystem_dock.h b/editor/filesystem_dock.h index 15a43dc6f28..b9500759289 100644 --- a/editor/filesystem_dock.h +++ b/editor/filesystem_dock.h @@ -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); } }; diff --git a/editor/gui/editor_file_dialog.cpp b/editor/gui/editor_file_dialog.cpp index fe86ac442b1..df1f026f78f 100644 --- a/editor/gui/editor_file_dialog.cpp +++ b/editor/gui/editor_file_dialog.cpp @@ -874,8 +874,8 @@ void EditorFileDialog::update_file_list() { item = dir_access->get_next(); } - dirs.sort_custom(); - files.sort_custom(); + dirs.sort_custom(); + files.sort_custom(); while (!dirs.is_empty()) { const String &dir_name = dirs.front()->get(); diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp index edec4af0941..43afc6f7d5a 100644 --- a/editor/plugins/script_editor_plugin.cpp +++ b/editor/plugins/script_editor_plugin.cpp @@ -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; diff --git a/scene/gui/file_dialog.cpp b/scene/gui/file_dialog.cpp index a53dffd0d29..40ac87160c4 100644 --- a/scene/gui/file_dialog.cpp +++ b/scene/gui/file_dialog.cpp @@ -671,8 +671,8 @@ void FileDialog::update_file_list() { item = dir_access->get_next(); } - dirs.sort_custom(); - files.sort_custom(); + dirs.sort_custom(); + files.sort_custom(); while (!dirs.is_empty()) { String &dir_name = dirs.front()->get(); diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h index da742d0183e..027dcac92d2 100644 --- a/tests/core/string/test_string.h +++ b/tests/core/string/test_string.h @@ -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);