Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/uu/rev/rev.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
rev [options] [file ...]
```

Reverse lines characterwise.
Reverse lines characterwise.

A record reverses by code point when it is valid UTF-8 and the first nonempty locale setting in LC_ALL, LC_CTYPE, LANG order specifies UTF-8; otherwise the whole record reverses bytewise.
103 changes: 98 additions & 5 deletions src/uu/rev/src/rev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,106 @@ mod options {
pub const ZERO: &str = "zero";
}

// uucore's get_locale_encoding uses LC_COLLATE and accepts empty values.

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.

if get_locale_encoding is wrong, could you please fix it in uucore instead of adding our own parser here?

fn is_utf8_locale() -> bool {
let locale = ["LC_ALL", "LC_CTYPE", "LANG"]
.into_iter()
.filter_map(env::var_os)
.find(|value| !value.is_empty());
locale
.as_deref()
.and_then(|name| name.to_str())
.is_some_and(|name| {
let codeset = match name.split_once('.') {
Some((_, codeset)) => codeset,
None if cfg!(target_vendor = "apple") => name,

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.

why only apple?

None => return false,
};
let codeset = codeset.split_once('@').map_or(codeset, |(code, _)| code);
codeset.eq_ignore_ascii_case("UTF-8") || codeset.eq_ignore_ascii_case("UTF8")
})
}

// 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"]

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.

why is this needed? rust-version is already 1.70 in the workspace

fn reverse_blocks(buf: &mut [u8], ascii_only: bool) -> &mut [u8] {
let len = buf.len();
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());

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.

please avoid unwrap

let y = u64::from_ne_bytes(b.try_into().unwrap());
if ascii_only && (x | y) & 0x8080_8080_8080_8080 != 0 {
break;
}
a.copy_from_slice(&y.swap_bytes().to_ne_bytes());
b.copy_from_slice(&x.swap_bytes().to_ne_bytes());
done += 8;
}
&mut buf[done..len - done]
}

#[clippy::msrv = "1.70.0"]
fn utf8_ascii_prefix(buf: &[u8]) -> Option<usize> {
// Reject invalid trailing sequences before scanning a long ASCII prefix.
if buf.len() >= 128 {
let tail = buf.len() - 8;
if let Some(i) = buf[tail..].iter().rposition(|b| !b.is_ascii()) {
// A valid UTF-8 character ending here occupies at most four bytes.
let end = tail + i + 1;
let lead = buf[end - 4..end].iter().rposition(|&b| b & 0xc0 != 0x80);
if std::str::from_utf8(&buf[end - 4 + lead.unwrap_or(0)..end]).is_err() {
return None;
}
}
}
// Skip validated ASCII blocks in the remaining ASCII and UTF-8 checks.
let ascii_prefix = buf
.chunks_exact(64)
.take_while(|block| block.is_ascii())
.count()
* 64;
let buf = &buf[ascii_prefix..];
let remaining_prefix = buf.iter().position(|b| !b.is_ascii())?;
std::str::from_utf8(&buf[remaining_prefix..])
.is_ok()
.then_some(ascii_prefix + remaining_prefix)
}

fn reverse_record(buf: &mut [u8], utf8: bool) {

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 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

// A non-ASCII byte at either end would stop the block path immediately.
let buf = if utf8 && buf.len() >= 128 && buf[0].is_ascii() && buf[buf.len() - 1].is_ascii() {
reverse_blocks(buf, true)
} else {
buf
};
if utf8 {
if let Some(ascii_prefix) = utf8_ascii_prefix(buf) {
reverse_blocks(buf, false).reverse();
// The ASCII prefix needs no character repair after byte reversal.
let repair_end = buf.len() - ascii_prefix;
let mut start = 0;
for end in 0..repair_end {
if buf[end] & 0xc0 != 0x80 {
buf[start..=end].reverse();
start = end + 1;
}
}
return;
}
}
buf.reverse();
}

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches: clap::ArgMatches = uu_app().try_get_matches_from(args)?;
let files = matches.get_many::<String>(options::FILE);
let zero = matches.get_flag(options::ZERO);

let sep = if zero { b'\0' } else { b'\n' };
let utf8 = is_utf8_locale();

if let Some(files) = files {
for path in files {
Expand All @@ -32,20 +125,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
uucore::show_error!("cannot open {path}: No such file or directory");
continue;
};
if let Err(err) = rev_stream(file, sep) {
if let Err(err) = rev_stream(file, sep, utf8) {
uucore::error::set_exit_code(1);
uucore::show_error!("cannot read {path}: {err}");
}
}
} else {
let stdin = std::io::stdin().lock();
let _ = rev_stream(stdin, sep);
let _ = rev_stream(stdin, sep, utf8);
}

Ok(())
}

fn rev_stream(stream: impl Read, sep: u8) -> std::io::Result<()> {
fn rev_stream(stream: impl Read, sep: u8, utf8: bool) -> std::io::Result<()> {
let mut stdout = std::io::stdout().lock();
let mut stream = BufReader::new(stream);
let mut buf = Vec::with_capacity(4096);
Expand All @@ -54,11 +147,11 @@ fn rev_stream(stream: impl Read, sep: u8) -> std::io::Result<()> {
stream.read_until(sep, &mut buf)?;
if buf.last().copied() == Some(sep) {
buf.pop();
buf.reverse();
reverse_record(&mut buf, utf8);
buf.push(sep);
stdout.write_all(&buf)?;
} else {
buf.reverse();
reverse_record(&mut buf, utf8);
stdout.write_all(&buf)?;
break;
}
Expand Down
Loading
Loading