diff --git a/src/uucore/src/lib/mods/version_cmp.rs b/src/uucore/src/lib/mods/version_cmp.rs index 2bbaffd25..828b7f2a6 100644 --- a/src/uucore/src/lib/mods/version_cmp.rs +++ b/src/uucore/src/lib/mods/version_cmp.rs @@ -106,7 +106,7 @@ pub fn version_cmp(mut a: &str, mut b: &str) -> Ordering { // 1. Compare leading non-numerical part // 2. Compare leading numerical part // 3. Repeat - loop { + while !a.is_empty() || !b.is_empty() { let a_numerical_start = a.find(|c: char| c.is_ascii_digit()).unwrap_or(a.len()); let b_numerical_start = b.find(|c: char| c.is_ascii_digit()).unwrap_or(b.len()); @@ -139,11 +139,9 @@ pub fn version_cmp(mut a: &str, mut b: &str) -> Ordering { a = &a[a_numerical_end..]; b = &b[b_numerical_end..]; - - if a.is_empty() && b.is_empty() { - return std::cmp::Ordering::Equal; - } } + + Ordering::Equal } #[cfg(test)] diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index e66a405ab..0c8af8969 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -134,6 +134,25 @@ fn test_version_empty_lines() { test_helper("version-empty-lines", &["-V", "--version-sort"]); } +#[test] +fn test_version_sort_unstable() { + new_ucmd!() + .arg("--sort=version") + .pipe_in("0.1\n0.02\n0.2\n0.002\n0.3\n") + .succeeds() + .stdout_is("0.1\n0.002\n0.02\n0.2\n0.3\n"); +} + +#[test] +fn test_version_sort_stable() { + new_ucmd!() + .arg("--stable") + .arg("--sort=version") + .pipe_in("0.1\n0.02\n0.2\n0.002\n0.3\n") + .succeeds() + .stdout_is("0.1\n0.02\n0.2\n0.002\n0.3\n"); +} + #[test] fn test_human_numeric_whitespace() { test_helper(