Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix versionsort chunk split on non-ASCII numerics #6407

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<'a> VersionChunkIter<'a> {
break;
}

if !c.is_numeric() {
if !c.is_ascii_digit() {
continue;
}

Expand Down Expand Up @@ -283,6 +283,10 @@ mod test {
source: "009"
})
);

// '๙' = U+0E59 THAI DIGIT NINE, General Category Nd
let mut iter = VersionChunkIter::new("x๙v");
assert_eq!(iter.next(), Some(VersionChunk::Str("x๙v")));
}

#[test]
Expand All @@ -297,6 +301,11 @@ mod test {
input.sort_by(|a, b| version_sort(a, b));
assert_eq!(input, expected);

let mut input = vec!["x๙x", "xéx", "x0x"];
let expected = vec!["x0x", "xéx", "x๙x"];
input.sort_by(|a, b| version_sort(a, b));
assert_eq!(input, expected);

let mut input = vec!["applesauce", "apple"];
let expected = vec!["apple", "applesauce"];
input.sort_by(|a, b| version_sort(a, b));
Expand Down
33 changes: 33 additions & 0 deletions tests/source/versionsort_non_ascii_numerics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::cmp::Ordering;
use print๙msg::print as first_print;
use print0msg::print as second_print;
use printémsg::print as third_print;
Copy link
Contributor

@ytmimi ytmimi Nov 30, 2024

Choose a reason for hiding this comment

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

version sort is only be applied when using style_edition=2024. You'll need to configure that for this test as follows:

Suggested change
use std::cmp::Ordering;
use print๙msg::print as first_print;
use print0msg::print as second_print;
use printémsg::print as third_print;
// rustfmt-style_edition: 2024
use std::cmp::Ordering;
use print๙msg::print as first_print;
use print0msg::print as second_print;
use printémsg::print as third_print;

You can read more about the comment configuration in test cases here.

Also, can you also add a style_edition=2015 import sorting test case so that we can compare the pre version sort ordering.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added. The 2015 edition actually sorts the same way since U+0030 < U+00E9 < U+0E59, so the bug only affected 2024 edition (and can't be reproduced on earlier versions because ASCII digits are the earliest Unicode numerics).

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding that additional test case!


fn main() {
first_print();
second_print();
third_print();

assert_eq!("print๙msg".cmp("printémsg"), Ordering::Greater);
}

/// '๙' = 0E59;THAI DIGIT NINE;Nd;
mod print๙msg {
pub fn print() {
println!("Non-ASCII Decimal_Number")
}
}

/// '0' = 0030;DIGIT ZERO;Nd;
mod print0msg {
pub fn print() {
println!("ASCII Decimal_Number")
}
}

/// 'é' = 00E9;LATIN SMALL LETTER E WITH ACUTE;Ll;
mod printémsg {
pub fn print() {
println!("Lowercase_Letter")
}
}
Copy link
Contributor

@ytmimi ytmimi Nov 30, 2024

Choose a reason for hiding this comment

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

Let's remove any code that isn't an import since it's not needed for the test case. Though, It would be great to keep the explanatory comments you added about '๙', '0', and 'é' to help document the test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated and expanded on the comments to explain why they sort in that order.

33 changes: 33 additions & 0 deletions tests/target/versionsort_non_ascii_numerics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use print0msg::print as second_print;
use printémsg::print as third_print;
use print๙msg::print as first_print;
use std::cmp::Ordering;

fn main() {
first_print();
second_print();
third_print();

assert_eq!("print๙msg".cmp("printémsg"), Ordering::Greater);
}

/// '๙' = 0E59;THAI DIGIT NINE;Nd;
mod print๙msg {
pub fn print() {
println!("Non-ASCII Decimal_Number")
}
}

/// '0' = 0030;DIGIT ZERO;Nd;
mod print0msg {
pub fn print() {
println!("ASCII Decimal_Number")
}
}

/// 'é' = 00E9;LATIN SMALL LETTER E WITH ACUTE;Ll;
mod printémsg {
pub fn print() {
println!("Lowercase_Letter")
}
}
Loading