Skip to content

Commit 8a02490

Browse files
committed
- sort: fix for - numeric sort (-n) incorrectly parses '+' as a number sign
1 parent 6f955da commit 8a02490

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

src/uu/sort/src/sort.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,12 @@ impl<'a> Line<'a> {
600600
tokenize(line, settings.separator, token_buffer);
601601
}
602602
if settings.mode == SortMode::Numeric {
603-
// exclude inf, nan, scientific notation
604-
let line_num_float = (!line.iter().any(u8::is_ascii_alphabetic))
605-
.then(|| std::str::from_utf8(line).ok())
606-
.flatten()
607-
.and_then(|s| s.parse::<f64>().ok());
603+
// exclude inf, nan, scientific notation, '+' sign
604+
let line_num_float = (!line.starts_with(&[*POSITIVE])
605+
&& !line.iter().any(u8::is_ascii_alphabetic))
606+
.then(|| std::str::from_utf8(line).ok())
607+
.flatten()
608+
.and_then(|s| s.parse::<f64>().ok());
608609
line_data.line_num_floats.push(line_num_float);
609610
}
610611
for (selector, selection) in settings

tests/by-util/test_sort.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,14 @@ fn test_numeric_unsorted_ints() {
414414
);
415415
}
416416

417+
#[test]
418+
fn test_numeric_with_prefix() {
419+
test_helper(
420+
"numeric_with_prefix",
421+
&["-n", "--numeric-sort", "--sort=numeric"],
422+
);
423+
}
424+
417425
#[test]
418426
fn test_human_block_sizes() {
419427
test_helper(
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-10
2+
-2
3+
-1
4+
+1
5+
+10
6+
+2
7+
hello
8+
world
9+
10
10+
50
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-10
2+
___
3+
___
4+
-2
5+
__
6+
__
7+
-1
8+
__
9+
__
10+
+1
11+
^ no match for key
12+
__
13+
+10
14+
^ no match for key
15+
___
16+
+2
17+
^ no match for key
18+
__
19+
hello
20+
^ no match for key
21+
_____
22+
world
23+
^ no match for key
24+
_____
25+
10
26+
__
27+
__
28+
50
29+
__
30+
__
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
+10
2+
+2
3+
+1
4+
-10
5+
-1
6+
-2
7+
world
8+
50
9+
10
10+
hello

0 commit comments

Comments
 (0)