Repository navigation
tr: classify character classes by the current locale - #15963
Dikshant-Neupane wants to merge 7 commits into
Conversation
There was a problem hiding this comment.
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.
cf0f639 to
14a2de8
Compare
There was a problem hiding this comment.
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.
Merging this PR will degrade performance by 0.69%1 warning
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
There was a problem hiding this comment.
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.
|
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. |
212ee78 to
ee5dfe0
Compare
There was a problem hiding this comment.
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.
|
thanks for the work on this! |
| /// 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. |
There was a problem hiding this comment.
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?
| ), | ||
| Self::Print => Box::new( | ||
| (48..=57) // digit | ||
| .chain(65..=90) // uppercase |
There was a problem hiding this comment.
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
| CharStar(u8), | ||
| CharRepeat(u8, usize), | ||
| Class(Class), | ||
| /// A set2 upper/lower class paired with an opposite-case class in set1. |
There was a problem hiding this comment.
could you please make this comment shorter? :)
There was a problem hiding this comment.
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.
|
Thanks for the review, I have addressed all three points in 74fc695.
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? |
|
thanks for the work on this! |
| Class::Upper => Class::Lower, | ||
| _ => Class::Upper, | ||
| }; | ||
| for (set1_pos, set1_item) in set1.iter().enumerate() { |
There was a problem hiding this comment.
this is almost the same loop as the class check just below, could be dedup, no?
| impl Class { | ||
| /// Position of this class in [`LocaleTables::members`], matching | ||
| /// [`ALL_CLASSES`]. | ||
| fn index(self) -> usize { |
There was a problem hiding this comment.
this duplicates the order of ALL_CLASSES, self as usize would do, no?
| /// | ||
| /// `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. |
There was a problem hiding this comment.
no need to tell the codspeed story in the code :) please keep only the first line
| (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!(), |
There was a problem hiding this comment.
could CaseConverted just carry the target case, so we don't need this unreachable!()?
| 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())); |
There was a problem hiding this comment.
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
74fc695 to
b5990c1
Compare
There was a problem hiding this comment.
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.
|
GNU testsuite comparison: |
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.
b5990c1 to
7f0b37d
Compare
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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:
Compared against GNU tr over all 256 bytes in Latin-1, C, and C.UTF-8 for all twelve classes with
-dand-dc, plus case translations: no differences.