@@ -1038,12 +1038,31 @@ fn get_rlimit() -> UResult<usize> {
10381038
10391039const STDIN_FILE : & str = "-" ;
10401040
1041+ /// Check if locale-aware collation will be needed based on sort settings and locale
1042+ fn will_need_locale_collation ( settings : & GlobalSettings ) -> bool {
1043+ // First check if we're using the C locale (DEFAULT_LOCALE), which doesn't need collator
1044+ let ( locale, _) = uucore:: i18n:: get_collating_locale ( ) ;
1045+ if * locale == uucore:: i18n:: DEFAULT_LOCALE {
1046+ return false ;
1047+ }
1048+
1049+ // Check each selector to see if any would use locale comparison
1050+ for selector in & settings. selectors {
1051+ let key_settings = & selector. settings ;
1052+ if key_settings. mode == SortMode :: Default
1053+ && !key_settings. ignore_case
1054+ && !key_settings. ignore_non_printing
1055+ && !key_settings. dictionary_order
1056+ {
1057+ return true ;
1058+ }
1059+ }
1060+ false
1061+ }
1062+
10411063#[ uucore:: main]
10421064#[ allow( clippy:: cognitive_complexity) ]
10431065pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
1044- // Initialize locale-aware collator for string comparisons
1045- uucore:: i18n:: collator:: try_init_collator ( CollatorOptions :: default ( ) ) ;
1046-
10471066 let mut settings = GlobalSettings :: default ( ) ;
10481067
10491068 let matches = uucore:: clap_localization:: handle_clap_result_with_exit_code ( uu_app ( ) , args, 2 ) ?;
@@ -1322,6 +1341,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
13221341
13231342 settings. init_precomputed ( ) ;
13241343
1344+ // Initialize locale-aware collator only if needed for string comparisons
1345+ if will_need_locale_collation ( & settings) {
1346+ uucore:: i18n:: collator:: try_init_collator ( CollatorOptions :: default ( ) ) ;
1347+ }
1348+
13251349 let result = exec ( & mut files, & settings, output, & mut tmp_dir) ;
13261350 // Wait here if `SIGINT` was received,
13271351 // for signal handler to do its work and terminate the program.
0 commit comments