Skip to content

Commit

Permalink
correctly remove leading '0's
Browse files Browse the repository at this point in the history
  • Loading branch information
nikstur committed May 22, 2024
1 parent df0e125 commit 1d15afc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,12 @@ pub fn strverscmp(a: &str, b: &str) -> Ordering {

// Step 7: Handle numerical prefix
if left.is_some_and(|c| c.is_ascii_digit()) || right.is_some_and(|c| c.is_ascii_digit()) {
// Skip leading '0's
while left.is_some_and(|c| c == '0') {
left = left_iter.next();
}
while right.is_some_and(|c| c == '0') {
right = right_iter.next();
}

let mut left_digit_prefix = String::new();
while left.is_some_and(|c| c.is_ascii_digit()) {
if let Some(char) = left {
left_digit_prefix.push(char);
if char != '0' {
left_digit_prefix.push(char);
}
}
if !left_iter.peek().is_some_and(char::is_ascii_digit) {
break;
Expand All @@ -210,7 +204,9 @@ pub fn strverscmp(a: &str, b: &str) -> Ordering {
let mut right_digit_prefix = String::new();
while right.is_some_and(|c| c.is_ascii_digit()) {
if let Some(char) = right {
right_digit_prefix.push(char);
if char != '0' {
right_digit_prefix.push(char);
}
}
if !right_iter.peek().is_some_and(char::is_ascii_digit) {
break;
Expand Down
1 change: 1 addition & 0 deletions tests/it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fn pre_releases() {
("123~rc1-99.99", "123~rc2-67.89"),
("123~rc1-99.99", "123^aa2-67.89"),
("123~rc1-99.99", "123aa2-67.89"),
("1.0.0~rc1", "1.0.0"),
]);
}

Expand Down

0 comments on commit 1d15afc

Please sign in to comment.