Skip to content

tr: classify character classes by the current locale - #15963

Open
Dikshant-Neupane wants to merge 7 commits into
uutils:mainfrom
Dikshant-Neupane:tr-locale-classes-15815
Open

Dikshant-Neupane wants to merge 7 commits into
uutils:mainfrom
Dikshant-Neupane:tr-locale-classes-15815

Conversation

@Dikshant-Neupane

Copy link
Copy Markdown

Closes #15815

In a single byte locale such as en_US.ISO-8859-1, tr -d '[:cntrl:]' kept the C1 bytes 0x80 to 0x9F because every class expanded from a fixed ASCII table and nothing read LC_CTYPE.

This keeps those ASCII tables unchanged and chains the bytes 128 to 255 classified by the C library in the current locale after setlocale, following the approach dd already uses. A set2 upper/lower class paired with the opposite case class in set1 now expands through the matching case conversion, so unequal locale class sizes do not break translation, and the set1 longer error now applies only when translating.

Test evidence:

cargo test --features tr --test tests test_tr::
test result: ok. 185 passed; 0 failed
cargo clippy -p uu_tr --all-targets -- -D warnings
Finished dev profile, exit 0

Compared against GNU tr over all 256 bytes in Latin-1, C, and C.UTF-8 for all twelve classes with -d and -dc, plus case translations: no differences.

Copilot AI balanced review requested due to automatic review settings October 28, 2026 21:41

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@cakebaker
cakebaker force-pushed the tr-locale-classes-15815 branch from cf0f639 to 14a2de8 Compare October 29, 2026 06:22
Copilot AI balanced review requested due to automatic review settings October 29, 2026 06:22

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codspeed

codspeed Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 0.69%

1 warning

⚠️ 64 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚡ 1 improved benchmark
❌ 1 regressed benchmark
✅ 115 untouched benchmarks
⏩ 368 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ df_deep_directory 894.3 µs 937.2 µs -4.58%
⚡ ls_recursive_deep_tree[(200, 2)] 3.1 ms 3 ms +3.35%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Dikshant-Neupane:tr-locale-classes-15815 (7f0b37d) with main (ab69b86)

Open in CodSpeed

Footnotes

  1. 368 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Copilot AI balanced review requested due to automatic review settings October 29, 2026 07:15

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Dikshant-Neupane

Copy link
Copy Markdown
Author

Hi cakebaker, a quick note on the CodSpeed report so it does not block review. I checked whether this change could reach the four regressed benchmarks. The diff touches tr only: operation.rs, the tr manifest, tr tests, and lockfile lines adding libc to tr. No shared code changed, no other utility depends on tr, and each regressed benchmark runs its own utility in process, so tr code is not linked into those bench binaries. The four regressions are all in cp, ls, sort and du, and CodSpeed flags 120 benchmarks here as syscall dominated, which fits tree walks and file sorting. On that basis the regressions look unrelated to this change. Happy to leave the call with you, or acknowledge them on CodSpeed if you prefer that.

@sylvestre
sylvestre force-pushed the tr-locale-classes-15815 branch from 212ee78 to ee5dfe0 Compare November 4, 2026 14:26
Copilot AI balanced review requested due to automatic review settings November 4, 2026 14:26

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for the work on this!
However, this is way too big for a PR :(
On macOS/BSD the libc classification also applies to high bytes in UTF-8 locales, so this would break multibyte chars there (see the inline comment).
Codspeed shows a 12% regression, probably from rebuilding the locale classes on every flatten(). Please have a look at it too.

Comment thread src/uu/tr/src/operation.rs Outdated
/// left out on purpose: there setlocale with an empty string selects the
/// user's ANSI code page, which makes the C library classify high bytes
/// and changes long standing ASCII only behaviour, breaking existing
/// tests. Windows therefore keeps the previous behaviour.

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.

this is only true with glibc. on macOS/FreeBSD, isalpha(0xe9) is true in a UTF-8 locale, so tr -d '[:alpha:]' would cut é (c3 a9) in half, no?
maybe only use the locale when uucore::i18n::charmap::locale_encoding() is SingleByte?

Comment thread src/uu/tr/src/operation.rs Outdated
),
Self::Print => Box::new(
(48..=57) // digit
.chain(65..=90) // uppercase

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.

this runs 128 libc calls on every flatten(), and flatten is called again and again by expanded_len / expanded_len_of in the loops.
maybe compute the 12 tables once after init_locale()? it probably explains the codspeed regression

Comment thread src/uu/tr/src/operation.rs Outdated
CharStar(u8),
CharRepeat(u8, usize),
Class(Class),
/// A set2 upper/lower class paired with an opposite-case class in set1.

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.

could you please make this comment shorter? :)

Copilot AI balanced review requested due to automatic review settings November 27, 2026 15:45

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Dikshant-Neupane

Copy link
Copy Markdown
Author

Thanks for the review, I have addressed all three points in 74fc695.

  • The locale classification is now gated on single-byte encodings: I check uucore::i18n::charmap::locale_encoding() and only use the C library classification when it is SingleByte. In UTF-8 and other multibyte locales the classes stay ASCII-only, so a multibyte character is never cut in half. I added a test that deletes [:alpha:] from aéb in a UTF-8 locale and checks the é comes out intact, and the Latin-1 test still passes.
  • The 12 class tables are now computed once after init_locale() and reused; flatten no longer calls into libc at all. I could not run Codspeed locally, so please re-check the regression on this push.
  • Shortened the CaseConverted comment.

I also resolved the conflict by merging main in, so your commits are untouched. Is the narrowed fix acceptable in one PR, or would you still rather split it?

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for the work on this!
this is still way too big for a PR: it grew instead of shrinking. Could you please split it, for example the [:cntrl:]/class tables first, then the case conversion?
Please also squash the commits into one.

Comment thread src/uu/tr/src/operation.rs Outdated
Class::Upper => Class::Lower,
_ => Class::Upper,
};
for (set1_pos, set1_item) in set1.iter().enumerate() {

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.

this is almost the same loop as the class check just below, could be dedup, no?

Comment thread src/uu/tr/src/operation.rs Outdated
impl Class {
/// Position of this class in [`LocaleTables::members`], matching
/// [`ALL_CLASSES`].
fn index(self) -> usize {

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.

this duplicates the order of ALL_CLASSES, self as usize would do, no?

Comment thread src/uu/tr/src/operation.rs Outdated
///
/// `flatten` used to rebuild these on every call by asking the C library
/// about all 128 high bytes, and it is called again and again while the
/// sets are sized and paired, which showed up as a Codspeed regression.

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.

no need to tell the codspeed story in the code :) please keep only the first line

Comment thread src/uu/tr/src/operation.rs Outdated
(Class::Lower, Class::Upper) => &self.lower_to_upper,
(Class::Upper, Class::Lower) => &self.upper_to_lower,
// CaseConverted is only built for upper/lower pairs.
_ => unreachable!(),

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.

could CaseConverted just carry the target case, so we don't need this unreachable!()?

Comment thread tests/by-util/test_tr.rs Outdated
fn test_classes_follow_single_byte_locale() {
// Compile a Latin-1 locale into a scratch directory; return early if
// localedef is unavailable, so the test never fails for a missing tool.
let locale_dir = std::env::temp_dir().join(format!("uutils-tr-locale-{}", std::process::id()));

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.

this dir is never removed, could you please use the test scenario's tmp dir instead?
also, skipping silently means it may never run in CI

Copilot AI balanced review requested due to automatic review settings November 27, 2026 17:38

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/tr/tr-case-class. tests/tr/tr-case-class is passing on 'main'. Maybe you have to rebase?
Congrats! The gnu test tests/env/env is no longer failing!

In a single-byte locale such as en_US.ISO-8859-1, `tr -d '[:cntrl:]'` kept the C1 bytes 0x80 to 0x9F because every class expanded from a fixed ASCII table and nothing read LC_CTYPE.

The ASCII tables are unchanged. Bytes 128 to 255 classified by the C library in the current locale are chained after them, computed once into tables the set paths reuse. In multibyte locales the tables stay ASCII-only, so multibyte characters are never split.
The glibc-style localedef invocation fails on FreeBSD and macOS: their localedef takes source-file pathnames and no locale sources are installed. Use the installed en_US.ISO8859-1 / en_US.UTF-8 locales there instead, and skip the single-byte test on macOS, which ships no single-byte locale.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 18:45

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Translating between [:lower:] and [:upper:] now converts each character with tolower/toupper in the current locale, so unequal locale class sizes do not break translation. A set2 upper/lower class paired with the opposite-case class in set1 is replaced by the case conversion of that set1 class.
Bionic only knows the C and C.UTF-8 locales, so en_US.ISO8859-1 can never load there.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 20:08

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

FreeBSD's Latin-1 cntrl class holds only C0 and DEL, so 0x9b is not a
control character there and the test failed. 0xe9 is alpha in Latin-1
on both glibc and FreeBSD, and not in the C locale, so the test still
fails loudly if the locale does not load.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 20:44

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Copilot AI balanced review requested due to automatic review settings November 27, 2026 22:35

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tr: [:cntrl:] misses C1 control bytes in 8-bit locales, unlike GNU

3 participants