Skip to content

Commit 6d50fc4

Browse files
committed
feat(sort): add locale-aware numeric sorting support
Implement NumericLocaleSettings to handle thousands separators and decimal points based on locale. Update tokenization logic to accommodate blank thousands separators for numeric and human-numeric modes, improving parsing of locale-specific numbers. Also refactor numeric locale detection for safety/readability and clean up related initialization/spell-checker ignore.
1 parent 5c459a1 commit 6d50fc4

File tree

1 file changed

+4
-38
lines changed

1 file changed

+4
-38
lines changed

src/uu/sort/src/sort.rs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ use uucore::{format_usage, i18n};
6363
use crate::buffer_hint::automatic_buffer_size;
6464
use crate::tmp_dir::TmpDirWrapper;
6565

66-
#[cfg(unix)]
67-
use nix::libc;
68-
#[cfg(unix)]
69-
use std::ffi::CStr;
70-
7166
mod options {
7267
pub mod modes {
7368
pub const SORT: &str = "sort";
@@ -1326,44 +1321,15 @@ impl FieldSelector {
13261321
}
13271322
}
13281323

1329-
#[cfg(unix)]
13301324
fn detect_numeric_locale() -> NumericLocaleSettings {
13311325
let mut settings = NumericLocaleSettings::default();
1332-
unsafe {
1333-
libc::setlocale(libc::LC_NUMERIC, c"".as_ptr());
1334-
}
1335-
let conv = unsafe { libc::localeconv() };
1336-
if conv.is_null() {
1337-
return settings;
1338-
}
1339-
let (decimal_ptr, thousands_ptr) = unsafe {
1340-
let conv = &*conv;
1341-
(conv.decimal_point, conv.thousands_sep)
1326+
settings.decimal_pt = Some(locale_decimal_pt());
1327+
settings.thousands_sep = match i18n::decimal::locale_grouping_separator().as_bytes() {
1328+
[b] => Some(*b),
1329+
_ => None,
13421330
};
1343-
1344-
settings.decimal_pt = single_byte_from_locale_ptr(decimal_ptr);
1345-
settings.thousands_sep = single_byte_from_locale_ptr(thousands_ptr);
13461331
settings
13471332
}
1348-
1349-
#[cfg(not(unix))]
1350-
fn detect_numeric_locale() -> NumericLocaleSettings {
1351-
NumericLocaleSettings::default()
1352-
}
1353-
1354-
#[cfg(unix)]
1355-
fn single_byte_from_locale_ptr(ptr: *const libc::c_char) -> Option<u8> {
1356-
if ptr.is_null() {
1357-
return None;
1358-
}
1359-
let bytes = unsafe { CStr::from_ptr(ptr).to_bytes() };
1360-
if bytes.len() == 1 {
1361-
Some(bytes[0])
1362-
} else {
1363-
None
1364-
}
1365-
}
1366-
13671333
/// Creates an `Arg` for a sort mode flag.
13681334
fn make_sort_mode_arg(mode: &'static str, short: char, help: String) -> Arg {
13691335
Arg::new(mode)

0 commit comments

Comments
 (0)