Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions crates/edit/src/icu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,13 +813,15 @@ fn compare_strings_ascii(a: &[u8], b: &[u8]) -> Ordering {
// case-insensitive equal, because then we use that as a fallback.
while let Some((&a, &b)) = iter.next() {
if a != b {
let mut order = a.cmp(&b);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value for order must always be la.cmp(&lb) (high weight: case-insensitive). Only if it's Equal must it fall back to a.cmp(&b) (low weight: case-sensitive).

let la = a.to_ascii_lowercase();
let lb = b.to_ascii_lowercase();
let mut order = la.cmp(&lb);

if order == Ordering::Equal {
// High weight: Find the first character which differs case-insensitively.
// Otherwise, it falls back to (or rather: defaults to) a case-sensitive comparison.
order = a.cmp(&b);

if la == lb {
// High weight: Find the first character which
// differs case-insensitively.
for (a, b) in iter {
let la = a.to_ascii_lowercase();
let lb = b.to_ascii_lowercase();
Expand Down Expand Up @@ -1378,6 +1380,9 @@ mod tests {
assert_eq!(compare_strings_ascii(b"abcd", b"abc"), Ordering::Greater);
// Same chars, different cases - 1st char wins
assert_eq!(compare_strings_ascii(b"AbC", b"aBc"), Ordering::Less);
// Different chars, different cases
assert_eq!(compare_strings_ascii(b"a", b"B"), Ordering::Less);
assert_eq!(compare_strings_ascii(b"B", b"a"), Ordering::Greater);
// Different chars, different cases - 2nd char wins, because it differs
assert_eq!(compare_strings_ascii(b"hallo", b"Hello"), Ordering::Less);
assert_eq!(compare_strings_ascii(b"Hello", b"hallo"), Ordering::Greater);
Expand Down
Loading