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:
parent
11ca4be1aa
commit
e041fda51d
1 changed files with 13 additions and 8 deletions
|
@ -44,9 +44,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
raw_separator
|
||||
};
|
||||
|
||||
let files: Vec<String> = match matches.values_of(options::FILE) {
|
||||
Some(v) => v.map(|v| v.to_owned()).collect(),
|
||||
None => vec!["-".to_owned()],
|
||||
let files: Vec<&str> = match matches.values_of(options::FILE) {
|
||||
Some(v) => v.collect(),
|
||||
None => vec!["-"],
|
||||
};
|
||||
|
||||
tac(files, before, regex, separator)
|
||||
|
@ -102,7 +102,7 @@ pub fn uu_app() -> App<'static, 'static> {
|
|||
/// returns [`std::io::Error`].
|
||||
fn buffer_tac_regex(
|
||||
data: &[u8],
|
||||
pattern: regex::bytes::Regex,
|
||||
pattern: ®ex::bytes::Regex,
|
||||
before: bool,
|
||||
) -> std::io::Result<()> {
|
||||
let mut out = stdout();
|
||||
|
@ -208,10 +208,16 @@ fn buffer_tac(data: &[u8], before: bool, separator: &str) -> std::io::Result<()>
|
|||
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;
|
||||
|
||||
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 == "-" {
|
||||
Box::new(stdin()) as Box<dyn Read>
|
||||
} else {
|
||||
|
@ -244,8 +250,7 @@ fn tac(filenames: Vec<String>, before: bool, regex: bool, separator: &str) -> i3
|
|||
exit_code = 1;
|
||||
continue;
|
||||
};
|
||||
if regex {
|
||||
let pattern = crash_if_err!(1, regex::bytes::Regex::new(separator));
|
||||
if let Some(pattern) = &pattern {
|
||||
buffer_tac_regex(&data, pattern, before)
|
||||
} else {
|
||||
buffer_tac(&data, before, separator)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue