A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.
Rust archive-managerdesktop-appdual-paneexplorer-alternativefile-browserfile-explorerfile-managerfluent-designgpu-acceleratedgpuimacosmicaproductivityquick-lookrustrust-guitabswindowswindows-11windows-explorer
Keep one source of order per name comparison
DeepSky Loongc7bc75b3a2ce
diff --git a/crates/rf-core/src/sort.rs b/crates/rf-core/src/sort.rsindex 25129df4..4eb31ada 100644--- a/crates/rf-core/src/sort.rs+++ b/crates/rf-core/src/sort.rs@@ -174,16 +174,20 @@ impl SortSpec {static COLLATOR: OnceLock<fn(char, char) -> Ordering> = OnceLock::new();/// Gives name sorting the user's collation for the characters the weights below cannot place: on-/// a zh-CN Windows that is pinyin (白菜 before 苹果, as Explorer has it), on any Windows the-/// locale's letters with their accents next to the plain ones. Installed once at startup; where-/// no platform installs one (tests, macOS for now) those characters keep code point order.+/// a zh-CN Windows that is pinyin (白菜 before 苹果, as Explorer has it), on a Japanese one kana+/// among themselves. It orders those characters among themselves, never against a character the+/// weights already place: one source of order per pair, or three names can cycle and+/// `sort_unstable_by` may panic on a comparison that is not a total order. Installed once at+/// startup; where no platform installs one (tests, macOS for now) those characters keep code+/// point order.pub fn set_char_collator(collate: fn(char, char) -> Ordering) {let _ = COLLATOR.set(collate);}/// Explorer-like "logical" comparison: case-insensitive, digit runs compared numerically/// ("file2" < "file10"). Allocation-free on the ASCII path, which is the one a big folder takes-/// ~n·log n times; a non-ASCII character pays a lookup (the platform's collator caches its key).+/// ~n·log n times; a character the weights cannot place pays a lookup (the platform's collator+/// caches its key).pub fn natural_cmp(a: &str, b: &str) -> Ordering {natural_cmp_inner(COLLATOR.get().copied(), a, b)}@@ -222,10 +226,15 @@ fn natural_cmp_inner(collate: Option<fn(char, char) -> Ordering>, a: &str, b: &s} else {let (la, lb) = (rank(ca), rank(cb));if la != lb {- // (in the scripts these weights only know by code point — Han, kana,- // accented Latin — the locale decides, issue 1071)- if let Some(order) = locale_order(collate, ca, cb) {- return order;+ // One source of order for this pair: the weights decide every character+ // they can place, and the collation only those they cannot, among+ // themselves (issue 1071). Asking it about a pair the weights also have+ // an opinion on lets the two disagree, and a < b < c < a is not a total+ // order — `sort_unstable_by` may panic on one.+ if la >= OTHER && lb >= OTHER {+ if let Some(order) = locale_order(collate, ca, cb) {+ return order;+ }}return la.cmp(&lb);}@@ -237,12 +246,9 @@ fn natural_cmp_inner(collate: Option<fn(char, char) -> Ordering>, a: &str, b: &s}}-/// The collation's answer for two characters, when it has one to give: the ASCII weights are-/// already the user's alphabet, everything above them is not.+/// The collation's answer for two characters, when it has one to give. Only characters the+/// weights cannot place reach it (see `rank`), so its order never has to agree with theirs.fn locale_order(collate: Option<fn(char, char) -> Ordering>, a: char, b: char) -> Option<Ordering> {- if a.is_ascii() && b.is_ascii() {- return None;- }let order = collate?(a, b);(order != Ordering::Equal).then_some(order)}@@ -287,6 +293,11 @@ fn natural_ascii(a: &[u8], b: &[u8]) -> Ordering {static TURKISH: AtomicBool = AtomicBool::new(false);+/// First weight of a character the table above has no place for: every character it can place+/// sorts below this, so a pair above it is the collation's and a pair straddling it is the+/// weights' (see `natural_cmp_inner`).+const OTHER: u32 = 0x10000;+/// Turkish collation: capital `I` is the capital of dotless `ı` (as Explorer sorts on Turkish/// Windows). Set once at startup from the user locale.pub fn set_turkish_collation(on: bool) {@@ -381,8 +392,9 @@ fn title_with(s: &str, turkish: bool) -> String {/// - Digits, then letters./// - Latin letters in alphabet order, with the Turkish letters right after their base letter/// (c ç d … g ğ h ı i … o ö p … s ş t u ü v) instead of after `z` by code point.-/// - Everything else after, by code point, unless a collation is installed-/// ([`set_char_collator`]): it decides these, the scripts whose order is not code point.+/// - Everything else after, by code point — or, where a collation is installed+/// ([`set_char_collator`]), in the collation's order among themselves. These are the characters+/// whose place in the alphabet a code point cannot name (Han, kana, accented Latin).#[inline]fn rank(c: char) -> u32 {const LETTERS: u32 = 0x1000;@@ -404,7 +416,7 @@ fn rank(c: char) -> u32 {'ş' => r('s') + 2,'ü' => r('u') + 2,_ if l.is_ascii() => l as u32,- _ => 0x10000 + l as u32,+ _ => OTHER + l as u32,}}@@ -469,6 +481,13 @@ mod tests {}assert_eq!(natural_cmp_with(never, "file2", "file10"), Ordering::Less);assert_eq!(natural_cmp_with(never, "_test.txt", "1_test.txt"), Ordering::Less);+ // in a name that mixes the two, only the pairs the weights cannot place are handed over:+ // a character they place is never compared by the collation, even against one they cannot+ fn placed(_: char, _: char) -> Ordering {+ panic!("a character the weights place must not ask the collation")+ }+ assert_eq!(natural_cmp_with(placed, "苹.txt", "a.txt"), Ordering::Greater);+ assert_eq!(natural_cmp_with(placed, "苹.txt", "1.txt"), Ordering::Greater);}#[test]diff --git a/crates/rf-win/src/collate.rs b/crates/rf-win/src/collate.rsindex 11e2f34d..292faf8b 100644--- a/crates/rf-win/src/collate.rs+++ b/crates/rf-win/src/collate.rs@@ -1,10 +1,12 @@//! The user's own order for characters, from the system's collation tables.//!//! Explorer sorts names with the shell's `StrCmpLogicalW`, whose text half is the locale's-//! collation: pinyin on a zh-CN Windows (白菜 above 苹果), letters with their accents beside the-//! plain ones, and so on. `rf_core::sort` compares names itself — digit runs as numbers, weights-//! for the ASCII signs — and a code point says nothing about that order, so the app installs-//! [`compare_chars`] there at startup (issue 1071).+//! collation: pinyin on a zh-CN Windows (白菜 above 苹果), kana in gojūon, and so on.+//! `rf_core::sort` compares names itself — digit runs as numbers, weights for the ASCII signs —+//! and a code point says nothing about the order of Han or kana, so the app installs+//! [`compare_chars`] there at startup (issue 1071). It is asked only about two characters the+//! weights cannot place: both sources must not have a say in one pair, or three names can form a+//! cycle and the sort may panic on it.use std::cell::RefCell;use std::cmp::Ordering;@@ -71,21 +73,68 @@ mod tests {sort_key(w!("zh-CN"), a).cmp(&sort_key(w!("zh-CN"), b))}+ /// A name order, as the app sorts with this collator installed.+ fn cmp(a: &str, b: &str) -> Ordering {+ rf_core::sort::natural_cmp_with(zh, a, b)+ }+#[test]fn the_scripts_a_code_point_cannot_order() {// pinyin, as Explorer has it on a zh-CN Windows (issue 1071): bai, da, dian, huo, ping- assert_eq!(zh('白', '大'), Ordering::Less);- assert_eq!(zh('大', '电'), Ordering::Less);- assert_eq!(zh('火', '苹'), Ordering::Less);+ assert_eq!(cmp("白", "大"), Ordering::Less);+ assert_eq!(cmp("大", "电"), Ordering::Less);+ assert_eq!(cmp("火", "苹"), Ordering::Less);// the code point order is another one (苹 U+82F9 is above 白 U+767D)- assert_eq!(zh('苹', '白'), Ordering::Greater);- // Han after the Latin letters and the digits, as Explorer has them- assert_eq!(zh('苹', 'Z'), Ordering::Greater);- assert_eq!(zh('苹', '9'), Ordering::Greater);- // a sign before a digit- assert_eq!(zh(':', '3'), Ordering::Less);- // accented letters beside their base letter, not after z- assert_eq!(zh('é', 'f'), Ordering::Less);+ assert_eq!(cmp("苹", "白"), Ordering::Greater);+ // Han after the Latin letters and the digits, as Explorer has them: a pair that straddles+ // the two blocks is the weights' to decide, the collation only hears about Han on both+ // sides+ assert_eq!(cmp("苹", "Z"), Ordering::Greater);+ assert_eq!(cmp("苹", "9"), Ordering::Greater);+ }++ #[test]+ fn a_pair_the_weights_place_is_never_asked_of_the_collation() {+ // Explorer puts a sign before a digit (`会议:` above `会议3`), the weights put the digit+ // first: one source of order per pair, so the weights win here. The sign's own follow-up,+ // not something to mix in (it would let three names form a cycle).+ assert_eq!(cmp(":", "3"), Ordering::Greater);+ // accented letters keep the weights' place as well: after z, not beside e+ assert_eq!(cmp("é", "f"), Ordering::Greater);+ assert_eq!(cmp("é", "e"), Ordering::Greater);+ }++ /// The order has to be total: `sort_unstable_by` is allowed to panic on one that is not, and+ /// three names are enough to show it. One source of order per pair is what keeps it so; this+ /// walks every triple of an alphabet the two sources disagree about, prefixes and digit runs+ /// included.+ #[test]+ fn the_order_is_total_over_every_triple_of_a_mixed_alphabet() {+ const ALPHABET: [&str; 22] = [+ "-", "'", "_", "=", ":", "0", "3", "9", "a", "Z", "é", "ö", "白", "大", "苹", "あ", "。", "a1", "a01", "白1",+ "白-", "a-",+ ];+ for x in ALPHABET {+ for y in ALPHABET {+ let xy = rf_core::sort::natural_cmp_with(compare_chars, x, y);+ assert_eq!(+ xy,+ rf_core::sort::natural_cmp_with(compare_chars, y, x).reverse(),+ "{x} vs {y} disagrees with {y} vs {x}"+ );+ for z in ALPHABET {+ if xy == Ordering::Greater {+ continue;+ }+ let yz = rf_core::sort::natural_cmp_with(compare_chars, y, z);+ if yz == Ordering::Greater {+ continue;+ }+ let xz = rf_core::sort::natural_cmp_with(compare_chars, x, z);+ assert_ne!(xz, Ordering::Greater, "{x} <= {y} <= {z}, but {x} > {z}");+ }+ }+ }}#[test]