Skip to content

Commit

Permalink
Fix sorting of files/dirs in dialogs
Browse files Browse the repository at this point in the history
Sorts leading `_` before other characters except `.`.
  • Loading branch information
AThousandShips committed Mar 20, 2024
1 parent fe01776 commit 2cbf469
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 74 deletions.
127 changes: 88 additions & 39 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,112 +927,161 @@ static _FORCE_INLINE_ signed char natural_cmp_common(const char32_t *&r_this_str
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++ != '.') {
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 (*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 (*p_this_str < *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 (*p_this_str > *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;
}
}

return 0;
}

signed char String::naturalnocasecmp_to(const String &p_str) const {
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 (_find_upper(*this_str) < _find_upper(*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 (_find_upper(*this_str) > _find_upper(*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;
}
}

return 0;
}

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();

return naturalnocasecmp_to_base(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 (*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;
Expand Down
9 changes: 9 additions & 0 deletions core/string/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ class String {
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 */
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
24 changes: 20 additions & 4 deletions doc/classes/String.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -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" />
Expand Down Expand Up @@ -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">
Expand All @@ -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">
Expand All @@ -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">
Expand Down
Loading

0 comments on commit 2cbf469

Please sign in to comment.