-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
sort: add locale-aware month parsing using ICU #9722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChrisDryden
wants to merge
5
commits into
uutils:main
Choose a base branch
from
ChrisDryden:sort-locale-month
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−66
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7257dec
sort: add locale-aware month parsing using ICU
ChrisDryden 539e8a4
CI: add ja_JP.UTF-8 locale for sort-month test
ChrisDryden 66e7f8c
deny.toml: skip hashbrown 0.15.5 duplicate
ChrisDryden c556ee0
sort: fix month parsing for C/POSIX locale
ChrisDryden 85627b7
Merge branch 'main' into sort-locale-month
sylvestre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| janv | ||
| AFAICT | ||
| asimd | ||
| ASIMD | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // This file is part of the uutils coreutils package. | ||
| // | ||
| // For the full copyright and license information, please view the LICENSE | ||
| // file that was distributed with this source code. | ||
|
|
||
| use std::sync::OnceLock; | ||
|
|
||
| use icu_datetime::provider::neo::{DatetimeNamesMonthGregorianV1, MonthNames}; | ||
| use icu_locale::{Locale, locale}; | ||
| use icu_provider::prelude::*; | ||
|
|
||
| use crate::i18n::get_time_locale; | ||
|
|
||
| fn load_month_names(loc: &Locale) -> Option<Vec<(String, u8)>> { | ||
| let data_locale = DataLocale::from(loc.clone()); | ||
| let abbr_attr = DataMarkerAttributes::from_str_or_panic("3"); | ||
| let request = DataRequest { | ||
| id: DataIdentifierBorrowed::for_marker_attributes_and_locale(abbr_attr, &data_locale), | ||
| metadata: DataRequestMetadata::default(), | ||
| }; | ||
|
|
||
| let response: DataResponse<DatetimeNamesMonthGregorianV1> = | ||
| icu_datetime::provider::Baked.load(request).ok()?; | ||
|
|
||
| if let MonthNames::Linear(names) = response.payload.get() { | ||
| let mut result = Vec::new(); | ||
| for (i, name) in names.iter().take(12).enumerate() { | ||
| let month = (i + 1) as u8; | ||
| let upper = name.to_uppercase(); | ||
| // Some locales use trailing periods in abbreviated months (e.g., "janv." in French). | ||
| // Store both with and without the period so we can match either format. | ||
| let stripped = upper.trim_end_matches('.'); | ||
| if stripped != upper { | ||
| result.push((stripped.to_string(), month)); | ||
| } | ||
| result.push((upper, month)); | ||
| } | ||
| return Some(result); | ||
| } | ||
| None | ||
| } | ||
|
|
||
| fn get_month_names() -> &'static Vec<(String, u8)> { | ||
| static MONTH_NAMES: OnceLock<Vec<(String, u8)>> = OnceLock::new(); | ||
| MONTH_NAMES.get_or_init(|| { | ||
| let loc = get_time_locale().0.clone(); | ||
| // For undefined locale (C/POSIX), ICU returns generic month names like "M01", "M02" | ||
| // which aren't useful for matching. Skip directly to English fallback. | ||
| let result = if loc == locale!("und") { | ||
| None | ||
| } else { | ||
| load_month_names(&loc) | ||
| }; | ||
| result | ||
| .or_else(|| load_month_names(&locale!("en"))) | ||
| .expect("ICU should always have English month data") | ||
| }) | ||
| } | ||
|
|
||
| /// Parse a month name from the beginning of the input bytes. | ||
| /// Returns month number (1-12) or 0 if not recognized. | ||
| pub fn month_parse(input: &[u8]) -> u8 { | ||
| let input = input.trim_ascii_start(); | ||
|
|
||
| // Convert bytes to string for comparison. For valid UTF-8, use it directly. | ||
| // For non-UTF-8 (e.g., Latin-1 locales), treat each byte as a Unicode codepoint. | ||
| // This handles legacy encodings like ISO-8859-1 where byte 0xE9 = 'é'. | ||
| let input_upper = std::str::from_utf8(input).map_or_else( | ||
| |_| { | ||
| input | ||
| .iter() | ||
| .map(|&b| b as char) | ||
| .collect::<String>() | ||
| .to_uppercase() | ||
| }, | ||
| |s| s.to_uppercase(), | ||
| ); | ||
|
|
||
| for (name, month) in get_month_names() { | ||
| if input_upper.starts_with(name) { | ||
| return *month; | ||
| } | ||
| } | ||
| 0 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.