Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/uucore/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,12 @@ fn embed_static_utility_locales(
/// It always includes "en-US" to ensure that a fallback is available if the
/// system locale's translation file is missing or if `LANG` is not set.
fn get_locales_to_embed(env: Option<String>) -> (String, Option<String>) {
let system_locale = env.and_then(|lang| {
let locale = lang.split('.').next()?.replace('_', "-");
if locale != "en-US" && !locale.is_empty() {
Some(locale)
} else {
None
}
});
let system_locale = env
.as_deref()
.and_then(|s| s.split('.').next())
.map(|s| s.replace('_', "-"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it's not worth the complexity, but in theory we could avoiding allocating for this kind of operation:

fn replace_underscores(s: &mut String) {
    // SAFETY:
    // '_' and '-' are both single-byte ASCII characters,
    // so this preserves UTF-8 validity and string length.
    unsafe {
        s.as_mut_vec()
            .iter_mut()
            .filter(|b| **b == b'_')
            .for_each(|b| *b = b'-');
    }
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it's not worth the complexity

build.rs is not a production.

.filter(|s| !s.is_empty() && s != "en-US");

("en-US".to_string(), system_locale)
}

Expand Down
Loading