Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
100 changes: 67 additions & 33 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/uu/sort/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ self_cell = { workspace = true }
tempfile = { workspace = true }
thiserror = { workspace = true }
unicode-width = { workspace = true }
uucore = { workspace = true, features = ["fs", "parser-size", "version-cmp"] }
uucore = { workspace = true, features = [
"fs",
"parser-size",
"version-cmp",
"i18n-decimal",
] }
fluent = { workspace = true }

[target.'cfg(unix)'.dependencies]
Expand Down
46 changes: 39 additions & 7 deletions src/uu/sort/src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use uucore::error::{FromIo, strip_errno};
use uucore::error::{UError, UResult, USimpleError, UUsageError};
use uucore::extendedbigdecimal::ExtendedBigDecimal;
use uucore::format_usage;
use uucore::i18n::decimal::locale_decimal_separator;
use uucore::line_ending::LineEnding;
use uucore::parser::num_parser::{ExtendedParser, ExtendedParserError};
use uucore::parser::parse_size::{ParseSizeError, Parser};
Expand Down Expand Up @@ -113,6 +114,24 @@ mod options {

const DECIMAL_PT: u8 = b'.';

fn locale_decimal_pt() -> u8 {
match locale_decimal_separator().as_bytes().first().copied() {
Some(b'.') => b'.',
Some(b',') => b',',
_ => DECIMAL_PT,
}
}

fn effective_decimal_pt(input: &[u8], locale_decimal: u8) -> u8 {
if locale_decimal == b',' {
let has_comma = input.contains(&b',');
if !has_comma && input.contains(&b'.') {
return b'.';
}
}
locale_decimal
}

const NEGATIVE: &u8 = &b'-';
const POSITIVE: &u8 = &b'+';

Expand Down Expand Up @@ -637,8 +656,9 @@ impl<'a> Line<'a> {
}
SortMode::GeneralNumeric => {
let initial_selection = &self.line[selection.clone()];

let leading = get_leading_gen(initial_selection);
let locale_decimal = locale_decimal_pt();
let decimal_pt = effective_decimal_pt(initial_selection, locale_decimal);
let leading = get_leading_gen(initial_selection, decimal_pt);

// Shorten selection to leading.
selection.start += leading.start;
Expand Down Expand Up @@ -965,7 +985,12 @@ impl FieldSelector {
Selection::WithNumInfo(range_str, info)
} else if self.settings.mode == SortMode::GeneralNumeric {
// Parse this number as BigDecimal, as this is the requirement for general numeric sorting.
Selection::AsBigDecimal(general_bd_parse(&range_str[get_leading_gen(range_str)]))
let locale_decimal = locale_decimal_pt();
let decimal_pt = effective_decimal_pt(range_str, locale_decimal);
Selection::AsBigDecimal(general_bd_parse(
&range_str[get_leading_gen(range_str, decimal_pt)],
decimal_pt,
))
} else {
// This is not a numeric sort, so we don't need a NumCache.
Selection::Str(range_str)
Expand Down Expand Up @@ -2020,7 +2045,7 @@ fn ascii_case_insensitive_cmp(a: &[u8], b: &[u8]) -> Ordering {
// scientific notation, so we strip those lines only after the end of the following numeric string.
// For example, 5e10KFD would be 5e10 or 5x10^10 and +10000HFKJFK would become 10000.
#[allow(clippy::cognitive_complexity)]
fn get_leading_gen(inp: &[u8]) -> Range<usize> {
fn get_leading_gen(inp: &[u8], decimal_pt: u8) -> Range<usize> {
let trimmed = inp.trim_ascii_start();
let leading_whitespace_len = inp.len() - trimmed.len();

Expand Down Expand Up @@ -2058,7 +2083,7 @@ fn get_leading_gen(inp: &[u8]) -> Range<usize> {
continue;
}

if c == DECIMAL_PT && !had_decimal_pt && !had_e_notation {
if c == decimal_pt && !had_decimal_pt && !had_e_notation {
had_decimal_pt = true;
continue;
}
Expand Down Expand Up @@ -2101,9 +2126,16 @@ pub enum GeneralBigDecimalParseResult {
/// Parse the beginning string into a [`GeneralBigDecimalParseResult`].
/// Using a [`GeneralBigDecimalParseResult`] instead of [`ExtendedBigDecimal`] is necessary to correctly order floats.
#[inline(always)]
fn general_bd_parse(a: &[u8]) -> GeneralBigDecimalParseResult {
fn general_bd_parse(a: &[u8], decimal_pt: u8) -> GeneralBigDecimalParseResult {
let parsed_bytes = (decimal_pt != DECIMAL_PT).then(|| {
a.iter()
.map(|&b| if b == decimal_pt { DECIMAL_PT } else { b })
.collect::<Vec<_>>()
});
let input = parsed_bytes.as_deref().unwrap_or(a);

// The string should be valid ASCII to be parsed.
let Ok(a) = std::str::from_utf8(a) else {
let Ok(a) = std::str::from_utf8(input) else {
return GeneralBigDecimalParseResult::Invalid;
};

Expand Down
Loading