1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

sort: fix debug output for zeros / invalid numbers

We were reporting "no match" when sorting something like "0 ". This is
because we don't distinguish between 0 and invalid lines when sorting.
For debug output we have to get this information back.
This commit is contained in:
Michael Debertol 2021-06-01 18:06:18 +02:00
parent 3625d98fc3
commit 06b3092f5f
9 changed files with 75 additions and 14 deletions

View file

@ -46,7 +46,12 @@ impl Default for NumInfoParseSettings {
impl NumInfo {
/// Parse NumInfo for this number.
/// Also returns the range of num that should be passed to numeric_str_cmp later
/// Also returns the range of num that should be passed to numeric_str_cmp later.
///
/// Leading zeros will be excluded from the returned range. If the number consists of only zeros,
/// an empty range (idx..idx) is returned so that idx is the char after the last zero.
/// If the input is not a number (which has to be treated as zero), the returned empty range
/// will be 0..0.
pub fn parse(num: &str, parse_settings: NumInfoParseSettings) -> (Self, Range<usize>) {
let mut exponent = -1;
let mut had_decimal_pt = false;
@ -105,7 +110,15 @@ impl NumInfo {
sign: if had_digit { sign } else { Sign::Positive },
exponent: 0,
},
0..0,
if had_digit {
// In this case there were only zeroes.
// For debug output to work properly, we have to match the character after the last zero.
idx..idx
} else {
// This was no number at all.
// For debug output to work properly, we have to match 0..0.
0..0
},
)
};
}

View file

@ -414,19 +414,29 @@ impl<'a> Line<'a> {
selection.start += num_range.start;
selection.end = selection.start + num_range.len();
// include a trailing si unit
if selector.settings.mode == SortMode::HumanNumeric
&& self.line[selection.end..initial_selection.end]
.starts_with(&['k', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'][..])
{
selection.end += 1;
}
if num_range != (0..0) {
// include a trailing si unit
if selector.settings.mode == SortMode::HumanNumeric
&& self.line[selection.end..initial_selection.end]
.starts_with(&['k', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'][..])
{
selection.end += 1;
}
// include leading zeroes, a leading minus or a leading decimal point
while self.line[initial_selection.start..selection.start]
.ends_with(&['-', '0', '.'][..])
{
selection.start -= 1;
// include leading zeroes, a leading minus or a leading decimal point
while self.line[initial_selection.start..selection.start]
.ends_with(&['-', '0', '.'][..])
{
selection.start -= 1;
}
} else {
// This was not a valid number.
// Report no match at the first non-whitespace character.
let leading_whitespace = self.line[selection.clone()]
.find(|c: char| !c.is_whitespace())
.unwrap_or(0);
selection.start += leading_whitespace;
selection.end += leading_whitespace;
}
}
SortMode::GeneralNumeric => {

View file

@ -123,6 +123,14 @@ fn test_multiple_decimals_numeric() {
)
}
#[test]
fn test_numeric_with_trailing_invalid_chars() {
test_helper(
"numeric_trailing_chars",
&["-n", "--numeric-sort", "--sort=numeric"],
)
}
#[test]
fn test_check_zero_terminated_failure() {
new_ucmd!()

View file

@ -1,3 +1,4 @@
K
844K
981K
11M

View file

@ -1,3 +1,6 @@
K
^ no match for key
_
844K
____
____

View file

@ -9,3 +9,4 @@
844K
981K
13M
K

View file

@ -0,0 +1,5 @@
-.05,
-x
0.abc
0foo
100

View file

@ -0,0 +1,15 @@
-.05,
____
_____
-x
^ no match for key
__
0.abc
__
_____
0foo
_
____
100
___
____

View file

@ -0,0 +1,5 @@
0foo
0.abc
100
-.05,
-x