Skip to content

Commit 98077ec

Browse files
committed
sort: Use locale-aware month sorting
1 parent 1b5e4e4 commit 98077ec

File tree

2 files changed

+22
-55
lines changed

2 files changed

+22
-55
lines changed

src/uu/sort/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ self_cell = { workspace = true }
3434
tempfile = { workspace = true }
3535
thiserror = { workspace = true }
3636
unicode-width = { workspace = true }
37-
uucore = { workspace = true, features = ["fs", "parser", "version-cmp"] }
3837
fluent = { workspace = true }
38+
uucore = { workspace = true, features = ["fs", "parser", "version-cmp", "i18n-date"] }
3939

4040
[target.'cfg(target_os = "linux")'.dependencies]
4141
nix = { workspace = true }

src/uu/sort/src/sort.rs

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@ mod numeric_str_cmp;
1818
mod tmp_dir;
1919

2020
use bigdecimal::BigDecimal;
21-
use chunks::LineData;
2221
use clap::builder::ValueParser;
2322
use clap::{Arg, ArgAction, Command};
24-
use custom_str_cmp::custom_str_cmp;
25-
use ext_sort::ext_sort;
2623
use fnv::FnvHasher;
2724
#[cfg(target_os = "linux")]
2825
use nix::libc::{RLIMIT_NOFILE, getrlimit, rlimit};
29-
use numeric_str_cmp::{NumInfo, NumInfoParseSettings, human_numeric_str_cmp, numeric_str_cmp};
3026
use rand::{Rng, rng};
3127
use rayon::prelude::*;
28+
use thiserror::Error;
29+
3230
use std::cmp::Ordering;
3331
use std::env;
3432
use std::ffi::{OsStr, OsString};
@@ -37,23 +35,28 @@ use std::hash::{Hash, Hasher};
3735
use std::io::{BufRead, BufReader, BufWriter, Read, Write, stdin, stdout};
3836
use std::num::IntErrorKind;
3937
use std::ops::Range;
40-
use std::path::Path;
41-
use std::path::PathBuf;
38+
use std::path::{Path, PathBuf};
4239
use std::str::Utf8Error;
43-
use thiserror::Error;
40+
4441
use uucore::display::Quotable;
45-
use uucore::error::{FromIo, strip_errno};
46-
use uucore::error::{UError, UResult, USimpleError, UUsageError, set_exit_code};
42+
use uucore::error::{
43+
FromIo, UError, UResult, USimpleError, UUsageError, set_exit_code, strip_errno,
44+
};
4745
use uucore::extendedbigdecimal::ExtendedBigDecimal;
48-
use uucore::format_usage;
46+
use uucore::i18n::date::{self, locale_parse_abbr_month};
4947
use uucore::line_ending::LineEnding;
5048
use uucore::parser::num_parser::{ExtendedParser, ExtendedParserError};
5149
use uucore::parser::parse_size::{ParseSizeError, Parser};
5250
use uucore::parser::shortcut_value_parser::ShortcutValueParser;
53-
use uucore::show_error;
54-
use uucore::translate;
5551
use uucore::version_cmp::version_cmp;
56-
52+
use uucore::{format_usage, show_error, translate};
53+
54+
use crate::chunks::LineData;
55+
use crate::custom_str_cmp::custom_str_cmp;
56+
use crate::ext_sort::ext_sort;
57+
use crate::numeric_str_cmp::{
58+
NumInfo, NumInfoParseSettings, human_numeric_str_cmp, numeric_str_cmp,
59+
};
5760
use crate::tmp_dir::TmpDirWrapper;
5861

5962
mod options {
@@ -599,7 +602,9 @@ impl<'a> Line<'a> {
599602
.enumerate()
600603
.skip_while(|(_, c)| c.is_ascii_whitespace());
601604

602-
let month = if month_parse(initial_selection) == Month::Unknown {
605+
let month = if locale_parse_abbr_month(initial_selection.trim_ascii_start())
606+
== date::Month::Unknown
607+
{
603608
// We failed to parse a month, which is equivalent to matching nothing.
604609
// Add the "no match for key" marker to the first non-whitespace character.
605610
let first_non_whitespace = month_chars.next();
@@ -1871,47 +1876,9 @@ fn random_shuffle(a: &[u8], b: &[u8], salt: &[u8]) -> Ordering {
18711876
da.cmp(&db)
18721877
}
18731878

1874-
#[derive(Eq, Ord, PartialEq, PartialOrd, Clone, Copy)]
1875-
enum Month {
1876-
Unknown,
1877-
January,
1878-
February,
1879-
March,
1880-
April,
1881-
May,
1882-
June,
1883-
July,
1884-
August,
1885-
September,
1886-
October,
1887-
November,
1888-
December,
1889-
}
1890-
1891-
/// Parse the beginning string into a Month, returning [`Month::Unknown`] on errors.
1892-
fn month_parse(line: &[u8]) -> Month {
1893-
let line = line.trim_ascii_start();
1894-
1895-
match line.get(..3).map(|x| x.to_ascii_uppercase()).as_deref() {
1896-
Some(b"JAN") => Month::January,
1897-
Some(b"FEB") => Month::February,
1898-
Some(b"MAR") => Month::March,
1899-
Some(b"APR") => Month::April,
1900-
Some(b"MAY") => Month::May,
1901-
Some(b"JUN") => Month::June,
1902-
Some(b"JUL") => Month::July,
1903-
Some(b"AUG") => Month::August,
1904-
Some(b"SEP") => Month::September,
1905-
Some(b"OCT") => Month::October,
1906-
Some(b"NOV") => Month::November,
1907-
Some(b"DEC") => Month::December,
1908-
_ => Month::Unknown,
1909-
}
1910-
}
1911-
19121879
fn month_compare(a: &[u8], b: &[u8]) -> Ordering {
1913-
let ma = month_parse(a);
1914-
let mb = month_parse(b);
1880+
let ma = locale_parse_abbr_month(a.trim_ascii_start());
1881+
let mb = locale_parse_abbr_month(b.trim_ascii_start());
19151882

19161883
ma.cmp(&mb)
19171884
}

0 commit comments

Comments
 (0)