1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-02 05:57:46 +00:00

tac: do not re-compile regular expression for each file

This commit is contained in:
Adam Reichold 2021-10-06 20:33:13 +02:00
parent 11ca4be1aa
commit e041fda51d

View file

@ -44,9 +44,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
raw_separator raw_separator
}; };
let files: Vec<String> = match matches.values_of(options::FILE) { let files: Vec<&str> = match matches.values_of(options::FILE) {
Some(v) => v.map(|v| v.to_owned()).collect(), Some(v) => v.collect(),
None => vec!["-".to_owned()], None => vec!["-"],
}; };
tac(files, before, regex, separator) tac(files, before, regex, separator)
@ -102,7 +102,7 @@ pub fn uu_app() -> App<'static, 'static> {
/// returns [`std::io::Error`]. /// returns [`std::io::Error`].
fn buffer_tac_regex( fn buffer_tac_regex(
data: &[u8], data: &[u8],
pattern: regex::bytes::Regex, pattern: &regex::bytes::Regex,
before: bool, before: bool,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let mut out = stdout(); let mut out = stdout();
@ -208,10 +208,16 @@ fn buffer_tac(data: &[u8], before: bool, separator: &str) -> std::io::Result<()>
Ok(()) Ok(())
} }
fn tac(filenames: Vec<String>, before: bool, regex: bool, separator: &str) -> i32 { fn tac(filenames: Vec<&str>, before: bool, regex: bool, separator: &str) -> i32 {
let mut exit_code = 0; let mut exit_code = 0;
for filename in &filenames { let pattern = if regex {
Some(crash_if_err!(1, regex::bytes::Regex::new(separator)))
} else {
None
};
for &filename in &filenames {
let mut file = BufReader::new(if filename == "-" { let mut file = BufReader::new(if filename == "-" {
Box::new(stdin()) as Box<dyn Read> Box::new(stdin()) as Box<dyn Read>
} else { } else {
@ -244,8 +250,7 @@ fn tac(filenames: Vec<String>, before: bool, regex: bool, separator: &str) -> i3
exit_code = 1; exit_code = 1;
continue; continue;
}; };
if regex { if let Some(pattern) = &pattern {
let pattern = crash_if_err!(1, regex::bytes::Regex::new(separator));
buffer_tac_regex(&data, pattern, before) buffer_tac_regex(&data, pattern, before)
} else { } else {
buffer_tac(&data, before, separator) buffer_tac(&data, before, separator)