Repository navigation
rev: reverse by character in UTF-8 locales - #657
Open
SichenLiang wants to merge 1 commit into
Open
SichenLiang wants to merge 1 commit into
SichenLiang wants to merge 1 commit into
Conversation
sylvestre
reviewed
Nov 8, 2026
| .then_some(ascii_prefix + remaining_prefix) | ||
| } | ||
|
|
||
| fn reverse_record(buf: &mut [u8], utf8: bool) { |
Contributor
There was a problem hiding this comment.
this is a lot of complexity just to reverse a line.
could you please start with the simple version: from_utf8, reverse the bytes, then fix the multibyte sequences?
we can add fast paths later in a separate PR, with benchmarks
| pub const ZERO: &str = "zero"; | ||
| } | ||
|
|
||
| // uucore's get_locale_encoding uses LC_COLLATE and accepts empty values. |
Contributor
There was a problem hiding this comment.
if get_locale_encoding is wrong, could you please fix it in uucore instead of adding our own parser here?
|
|
||
| // Checking while swapping avoids a separate scan in ASCII-only mode. | ||
| // The caller handles the middle so the loop can use complete words. | ||
| #[clippy::msrv = "1.70.0"] |
Contributor
There was a problem hiding this comment.
why is this needed? rust-version is already 1.70 in the workspace
| let (front, back) = buf.split_at_mut(len / 2); | ||
| let mut done = 0; | ||
| for (a, b) in front.chunks_exact_mut(8).zip(back.rchunks_exact_mut(8)) { | ||
| let x = u64::from_ne_bytes(a.try_into().unwrap()); |
| .is_some_and(|name| { | ||
| let codeset = match name.split_once('.') { | ||
| Some((_, codeset)) => codeset, | ||
| None if cfg!(target_vendor = "apple") => name, |
When the first nonempty value of LC_ALL, LC_CTYPE and LANG names a UTF-8 codeset, reverse valid UTF-8 records by code point. Keep byte reversal in other locales and for records that are not valid UTF-8.
sylvestre
force-pushed
the
rev-utf8-characters
branch
from
November 27, 2026 17:20
7fc95bc to
a9a2fd7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In a UTF-8 locale, reverse valid UTF-8 records by code point instead of by byte. Other locales keep the existing byte-reversal behavior.
The locale is taken from the first nonempty value of
LC_ALL,LC_CTYPE, andLANG. The manual now documents that rule.Records that are not valid UTF-8 are still reversed byte by byte and exit 0; util-linux reports an
fgetwc()error instead.On an x86_64 Linux machine, 64 MiB of records made entirely of multibyte characters took 5.9 to 7.2 times as much CPU as main, depending on code alignment, because they are now validated and reversed on character boundaries. Long ASCII records in a UTF-8 locale were 13% to 22% faster. In the C locale the code path is unchanged; a 7% difference there went away when both binaries were rebuilt with branch alignment. Short ASCII lines were about 3% slower.
Tests cover locale precedence, valid and invalid records, files, NUL-delimited input, and long records.
Fixes #656