mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-01 05:27:45 +00:00
tac: do not use a buffered read as fs::read is more efficient and io::Stdin is buffered internally
This commit is contained in:
parent
0d583754ca
commit
28b04fa899
1 changed files with 14 additions and 14 deletions
|
@ -12,8 +12,8 @@ extern crate uucore;
|
||||||
|
|
||||||
use clap::{crate_version, App, Arg};
|
use clap::{crate_version, App, Arg};
|
||||||
use memchr::memmem;
|
use memchr::memmem;
|
||||||
use std::io::{stdin, stdout, BufReader, Read, Write};
|
use std::io::{stdin, stdout, Read, Write};
|
||||||
use std::{fs::File, path::Path};
|
use std::{fs::read, path::Path};
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::InvalidEncodingHandling;
|
use uucore::InvalidEncodingHandling;
|
||||||
|
|
||||||
|
@ -220,8 +220,14 @@ fn tac(filenames: Vec<&str>, before: bool, regex: bool, separator: &str) -> i32
|
||||||
};
|
};
|
||||||
|
|
||||||
for &filename in &filenames {
|
for &filename in &filenames {
|
||||||
let mut file = BufReader::new(if filename == "-" {
|
let data = if filename == "-" {
|
||||||
Box::new(stdin()) as Box<dyn Read>
|
let mut data = Vec::new();
|
||||||
|
if let Err(e) = stdin().read_to_end(&mut data) {
|
||||||
|
show_error!("failed to read from stdin: {}", e);
|
||||||
|
exit_code = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
data
|
||||||
} else {
|
} else {
|
||||||
let path = Path::new(filename);
|
let path = Path::new(filename);
|
||||||
if path.is_dir() || path.metadata().is_err() {
|
if path.is_dir() || path.metadata().is_err() {
|
||||||
|
@ -236,22 +242,16 @@ fn tac(filenames: Vec<&str>, before: bool, regex: bool, separator: &str) -> i32
|
||||||
exit_code = 1;
|
exit_code = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
match File::open(path) {
|
match read(path) {
|
||||||
Ok(f) => Box::new(f) as Box<dyn Read>,
|
Ok(data) => data,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
show_error!("failed to open {} for reading: {}", filename.quote(), e);
|
show_error!("failed to read {}: {}", filename.quote(), e);
|
||||||
exit_code = 1;
|
exit_code = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
let mut data = Vec::new();
|
|
||||||
if let Err(e) = file.read_to_end(&mut data) {
|
|
||||||
show_error!("failed to read {}: {}", filename.quote(), e);
|
|
||||||
exit_code = 1;
|
|
||||||
continue;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(pattern) = &pattern {
|
if let Some(pattern) = &pattern {
|
||||||
buffer_tac_regex(&data, pattern, before)
|
buffer_tac_regex(&data, pattern, before)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue