Skip to content

Commit 07d3530

Browse files
tr: call setlocale once in uumain and simplify case conversion
Move the setlocale call out of Sequence::solve_set_characters into uumain, so a parsing function no longer changes the process locale. Collapse the replacement option and the two branches in the case-conversion loop into a single let item.
1 parent 3121443 commit 07d3530

2 files changed

Lines changed: 12 additions & 15 deletions

File tree

‎src/uu/tr/src/operation.rs‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,17 @@ pub enum Sequence {
241241

242242
/// Initialise the process locale from the environment, as `dd` does.
243243
/// Windows is excluded: setlocale selects the ANSI code page, classifying high bytes.
244+
/// Called once from `uumain`; parsing functions must not change the process locale.
244245
#[cfg(not(any(target_os = "wasi", target_os = "windows")))]
245-
fn init_locale() {
246+
pub(crate) fn init_locale() {
246247
// SAFETY: setlocale is called with a valid C string, as in `dd`.
247248
unsafe {
248249
libc::setlocale(libc::LC_CTYPE, c"".as_ptr());
249250
}
250251
}
251252

252253
#[cfg(any(target_os = "wasi", target_os = "windows"))]
253-
fn init_locale() {}
254+
pub(crate) fn init_locale() {}
254255

255256
/// Whether a high byte belongs to a class in the current locale.
256257
/// Only asked about bytes 128 and above; ASCII is handled separately.
@@ -525,8 +526,7 @@ impl Sequence {
525526
truncate_set1_flag: bool,
526527
translating: bool,
527528
) -> Result<(Vec<u8>, Vec<u8>), SequenceError> {
528-
// The class tables are built once here, after the locale is initialised.
529-
init_locale();
529+
// The class tables are built once here; the locale was initialised in uumain.
530530
let tables = LocaleTables::build();
531531

532532
let is_char_star = |s: &&Self| -> bool { matches!(s, Self::CharStar(_)) };
@@ -610,20 +610,16 @@ impl Sequence {
610610
let mut converted_set2 = Vec::with_capacity(set2.len());
611611
let mut set2_prefix_len: u128 = 0;
612612
for set2_item in set2 {
613-
let mut replacement = None;
614-
if let Self::Class(to) = set2_item
613+
let item = if let Self::Class(to) = set2_item
615614
&& matches!(to, Class::Upper | Class::Lower)
616615
&& Self::class_at(&set1, set2_prefix_len, &tables) == Some(to.opposite_case())
617616
{
618-
replacement = Some(Self::CaseConverted(to));
619-
}
620-
if let Some(converted) = replacement {
621-
set2_prefix_len += converted.expanded_len(&tables) as u128;
622-
converted_set2.push(converted);
617+
Self::CaseConverted(to)
623618
} else {
624-
set2_prefix_len += set2_item.expanded_len(&tables) as u128;
625-
converted_set2.push(set2_item);
626-
}
619+
set2_item
620+
};
621+
set2_prefix_len += item.expanded_len(&tables) as u128;
622+
converted_set2.push(item);
627623
}
628624
set2 = converted_set2;
629625
}

‎src/uu/tr/src/tr.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod unicode_table;
1111
use clap::{Arg, ArgAction, Command, value_parser};
1212
use operation::{
1313
DeleteOperation, Sequence, SqueezeOperation, SymbolTranslator, TranslateOperation,
14-
flush_output, translate_input,
14+
flush_output, init_locale, translate_input,
1515
};
1616
use simd::process_input;
1717
use std::ffi::OsString;
@@ -36,6 +36,7 @@ mod options {
3636

3737
#[uucore::main]
3838
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
39+
init_locale();
3940
let args: Vec<OsString> = args.collect();
4041
// Kept for the caret in set diagnostics, which needs the sets as typed.
4142
let set_args = uucore::diagnostics::capture(&args);

0 commit comments

Comments
 (0)