mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-02 05:57:46 +00:00
ptx: remove unneeded regex and reduce repetitive regex compilations
This commit is contained in:
parent
351d1fc068
commit
b387565ba3
1 changed files with 6 additions and 8 deletions
|
@ -211,11 +211,7 @@ fn read_input(input_files: &[String], config: &Config) -> FileMap {
|
||||||
Box::new(file)
|
Box::new(file)
|
||||||
});
|
});
|
||||||
let lines: Vec<String> = reader.lines().map(|x| crash_if_err!(1, x)).collect();
|
let lines: Vec<String> = reader.lines().map(|x| crash_if_err!(1, x)).collect();
|
||||||
let ws_reg = Regex::new(r"[\t\n\v\f\r]").unwrap();
|
let chars_lines: Vec<Vec<char>> = lines.iter().map(|x| x.chars().collect()).collect();
|
||||||
let chars_lines: Vec<Vec<char>> = lines
|
|
||||||
.iter()
|
|
||||||
.map(|x| ws_reg.replace_all(x, " ").chars().collect())
|
|
||||||
.collect();
|
|
||||||
let size = lines.len();
|
let size = lines.len();
|
||||||
file_map.insert(
|
file_map.insert(
|
||||||
filename.to_owned(),
|
filename.to_owned(),
|
||||||
|
@ -274,12 +270,11 @@ fn create_word_set(config: &Config, filter: &WordFilter, file_map: &FileMap) ->
|
||||||
word_set
|
word_set
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_reference(config: &Config, word_ref: &WordRef, line: &str) -> String {
|
fn get_reference(config: &Config, word_ref: &WordRef, line: &str, context_reg: &Regex) -> String {
|
||||||
if config.auto_ref {
|
if config.auto_ref {
|
||||||
format!("{}:{}", word_ref.filename, word_ref.local_line_nr + 1)
|
format!("{}:{}", word_ref.filename, word_ref.local_line_nr + 1)
|
||||||
} else if config.input_ref {
|
} else if config.input_ref {
|
||||||
let reg = Regex::new(&config.context_regex).unwrap();
|
let (beg, end) = match context_reg.find(line) {
|
||||||
let (beg, end) = match reg.find(line) {
|
|
||||||
Some(x) => (x.start(), x.end()),
|
Some(x) => (x.start(), x.end()),
|
||||||
None => (0, 0),
|
None => (0, 0),
|
||||||
};
|
};
|
||||||
|
@ -524,6 +519,8 @@ fn write_traditional_output(
|
||||||
Box::new(file)
|
Box::new(file)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let context_reg = Regex::new(&config.context_regex).unwrap();
|
||||||
|
|
||||||
for word_ref in words.iter() {
|
for word_ref in words.iter() {
|
||||||
let file_map_value: &FileContent = file_map
|
let file_map_value: &FileContent = file_map
|
||||||
.get(&(word_ref.filename))
|
.get(&(word_ref.filename))
|
||||||
|
@ -537,6 +534,7 @@ fn write_traditional_output(
|
||||||
config,
|
config,
|
||||||
word_ref,
|
word_ref,
|
||||||
&lines[word_ref.local_line_nr],
|
&lines[word_ref.local_line_nr],
|
||||||
|
&context_reg,
|
||||||
);
|
);
|
||||||
let output_line: String = match config.format {
|
let output_line: String = match config.format {
|
||||||
OutFormat::Tex => format_tex_line(
|
OutFormat::Tex => format_tex_line(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue