mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #679 from jbcrail/stabilize-5
Remove unstable features from ptx.
This commit is contained in:
commit
f98a704604
1 changed files with 86 additions and 85 deletions
|
@ -1,5 +1,4 @@
|
||||||
#![crate_name = "ptx"]
|
#![crate_name = "ptx"]
|
||||||
#![feature(convert, vec_push_all)]
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
|
@ -150,7 +149,6 @@ fn print_usage(opts: &Options) {
|
||||||
println!("{}\n{}", opts.usage(&brief), explaination);
|
println!("{}\n{}", opts.usage(&brief), explaination);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn get_config(matches: &Matches) -> Config {
|
fn get_config(matches: &Matches) -> Config {
|
||||||
let mut config: Config = Default::default();
|
let mut config: Config = Default::default();
|
||||||
let err_msg = "parsing options failed";
|
let err_msg = "parsing options failed";
|
||||||
|
@ -179,12 +177,12 @@ fn get_config(matches: &Matches) -> Config {
|
||||||
if matches.opt_present("w") {
|
if matches.opt_present("w") {
|
||||||
let width_str = matches.opt_str("w").expect(err_msg);
|
let width_str = matches.opt_str("w").expect(err_msg);
|
||||||
config.line_width = crash_if_err!(
|
config.line_width = crash_if_err!(
|
||||||
1, usize::from_str_radix(width_str.as_str(), 10));
|
1, usize::from_str_radix(&width_str, 10));
|
||||||
}
|
}
|
||||||
if matches.opt_present("g") {
|
if matches.opt_present("g") {
|
||||||
let gap_str = matches.opt_str("g").expect(err_msg);
|
let gap_str = matches.opt_str("g").expect(err_msg);
|
||||||
config.gap_size = crash_if_err!(
|
config.gap_size = crash_if_err!(
|
||||||
1, usize::from_str_radix(gap_str.as_str(), 10));
|
1, usize::from_str_radix(&gap_str, 10));
|
||||||
}
|
}
|
||||||
if matches.opt_present("O") {
|
if matches.opt_present("O") {
|
||||||
config.format = OutFormat::Roff;
|
config.format = OutFormat::Roff;
|
||||||
|
@ -195,7 +193,7 @@ fn get_config(matches: &Matches) -> Config {
|
||||||
config
|
config
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_input(input_files: Vec<&str>, config: &Config) ->
|
fn read_input(input_files: &Vec<String>, config: &Config) ->
|
||||||
HashMap<String, (Vec<String>, usize)> {
|
HashMap<String, (Vec<String>, usize)> {
|
||||||
let mut file_map : HashMap<String, (Vec<String>, usize)> =
|
let mut file_map : HashMap<String, (Vec<String>, usize)> =
|
||||||
HashMap::new();
|
HashMap::new();
|
||||||
|
@ -204,9 +202,11 @@ fn read_input(input_files: Vec<&str>, config: &Config) ->
|
||||||
files.push("-");
|
files.push("-");
|
||||||
} else {
|
} else {
|
||||||
if config.gnu_ext {
|
if config.gnu_ext {
|
||||||
files.push_all(input_files.as_slice());
|
for file in input_files {
|
||||||
|
files.push(&file);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
files.push(input_files[0]);
|
files.push(&input_files[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut lines_so_far: usize = 0;
|
let mut lines_so_far: usize = 0;
|
||||||
|
@ -230,8 +230,8 @@ fn read_input(input_files: Vec<&str>, config: &Config) ->
|
||||||
fn create_word_set(config: &Config, filter: &WordFilter,
|
fn create_word_set(config: &Config, filter: &WordFilter,
|
||||||
file_map: &HashMap<String, (Vec<String>, usize)>)->
|
file_map: &HashMap<String, (Vec<String>, usize)>)->
|
||||||
BTreeSet<WordRef> {
|
BTreeSet<WordRef> {
|
||||||
let reg = Regex::new(filter.word_regex.as_str()).unwrap();
|
let reg = Regex::new(&filter.word_regex).unwrap();
|
||||||
let ref_reg = Regex::new(config.context_regex.as_str()).unwrap();
|
let ref_reg = Regex::new(&config.context_regex).unwrap();
|
||||||
let mut word_set: BTreeSet<WordRef> = BTreeSet::new();
|
let mut word_set: BTreeSet<WordRef> = BTreeSet::new();
|
||||||
for (file, lines) in file_map.iter() {
|
for (file, lines) in file_map.iter() {
|
||||||
let mut count: usize = 0;
|
let mut count: usize = 0;
|
||||||
|
@ -261,7 +261,7 @@ fn create_word_set(config: &Config, filter: &WordFilter,
|
||||||
}
|
}
|
||||||
word_set.insert(WordRef{
|
word_set.insert(WordRef{
|
||||||
word: word,
|
word: word,
|
||||||
filename: String::from(file.as_str()),
|
filename: String::from(file.clone()),
|
||||||
global_line_nr: offs + count,
|
global_line_nr: offs + count,
|
||||||
local_line_nr: count,
|
local_line_nr: count,
|
||||||
position: beg,
|
position: beg,
|
||||||
|
@ -279,7 +279,7 @@ fn get_reference(config: &Config, word_ref: &WordRef, line: &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.as_str()).unwrap();
|
let reg = Regex::new(&config.context_regex).unwrap();
|
||||||
let (beg, end) = match reg.find(line) {
|
let (beg, end) = match reg.find(line) {
|
||||||
Some(x) => x,
|
Some(x) => x,
|
||||||
None => (0,0)
|
None => (0,0)
|
||||||
|
@ -334,12 +334,11 @@ fn trim_idx(s: &Vec<char>, beg: usize, end: usize) -> (usize, usize) {
|
||||||
(b,e)
|
(b,e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn get_output_chunks(all_before: &String, keyword: &String, all_after: &String,
|
fn get_output_chunks(all_before: &String, keyword: &String, all_after: &String,
|
||||||
config: &Config) -> (String, String, String, String) {
|
config: &Config) -> (String, String, String, String) {
|
||||||
assert!(all_before.trim() == all_before.as_str());
|
assert_eq!(all_before.trim().to_string(), *all_before);
|
||||||
assert!(keyword.trim() == keyword.as_str());
|
assert_eq!(keyword.trim().to_string(), *keyword);
|
||||||
assert!(all_after.trim() == all_after.as_str());
|
assert_eq!(all_after.trim().to_string(), *all_after);
|
||||||
let mut head = String::new();
|
let mut head = String::new();
|
||||||
let mut before = String::new();
|
let mut before = String::new();
|
||||||
let mut after = String::new();
|
let mut after = String::new();
|
||||||
|
@ -388,9 +387,9 @@ fn get_output_chunks(all_before: &String, keyword: &String, all_after: &String,
|
||||||
|
|
||||||
// put right context truncation string if needed
|
// put right context truncation string if needed
|
||||||
if after_end != all_after.len() && tail_beg == tail_end {
|
if after_end != all_after.len() && tail_beg == tail_end {
|
||||||
after.push_str(config.trunc_str.as_str());
|
after.push_str(&config.trunc_str);
|
||||||
} else if after_end != all_after.len() && tail_end != all_after.len() {
|
} else if after_end != all_after.len() && tail_end != all_after.len() {
|
||||||
tail.push_str(config.trunc_str.as_str());
|
tail.push_str(&config.trunc_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
// put left context truncation string if needed
|
// put left context truncation string if needed
|
||||||
|
@ -421,7 +420,9 @@ fn adjust_tex_str(context: &str) -> String {
|
||||||
let ws_reg = Regex::new(r"[\t\n\v\f\r ]").unwrap();
|
let ws_reg = Regex::new(r"[\t\n\v\f\r ]").unwrap();
|
||||||
let mut fix: String = ws_reg.replace_all(context, " ").trim().to_string();
|
let mut fix: String = ws_reg.replace_all(context, " ").trim().to_string();
|
||||||
let mapped_chunks: Vec<String> = fix.chars().map(tex_mapper).collect();
|
let mapped_chunks: Vec<String> = fix.chars().map(tex_mapper).collect();
|
||||||
fix = mapped_chunks.join("");
|
// NB: Using deprecated connect() until Rust 1.3 becomes stable.
|
||||||
|
// When 1.3 is released, replace connect() with join().
|
||||||
|
fix = mapped_chunks.connect("");
|
||||||
fix
|
fix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -441,8 +442,8 @@ fn format_tex_line(config: &Config, word_ref: &WordRef, line: &String,
|
||||||
&line[word_ref.position_end .. line.len()]);
|
&line[word_ref.position_end .. line.len()]);
|
||||||
let (tail, before, after, head) =
|
let (tail, before, after, head) =
|
||||||
get_output_chunks(&all_before, &keyword, &all_after, &config);
|
get_output_chunks(&all_before, &keyword, &all_after, &config);
|
||||||
output.push_str(format!("{5}{0}{6}{5}{1}{6}{5}{2}{6}{5}{3}{6}{5}{4}{6}",
|
output.push_str(&format!("{5}{0}{6}{5}{1}{6}{5}{2}{6}{5}{3}{6}{5}{4}{6}",
|
||||||
tail, before, keyword, after, head, "{", "}").as_str());
|
tail, before, keyword, after, head, "{", "}"));
|
||||||
if config.auto_ref || config.input_ref {
|
if config.auto_ref || config.input_ref {
|
||||||
output.push_str(
|
output.push_str(
|
||||||
&format!("{}{}{}", "{", adjust_tex_str(&reference), "}"));
|
&format!("{}{}{}", "{", adjust_tex_str(&reference), "}"));
|
||||||
|
@ -471,8 +472,8 @@ fn format_roff_line(config: &Config, word_ref: &WordRef, line: &str,
|
||||||
&line[word_ref.position_end .. line.len()]);
|
&line[word_ref.position_end .. line.len()]);
|
||||||
let (tail, before, after, head) =
|
let (tail, before, after, head) =
|
||||||
get_output_chunks(&all_before, &keyword, &all_after, &config);
|
get_output_chunks(&all_before, &keyword, &all_after, &config);
|
||||||
output.push_str(format!(" \"{}\" \"{}\" \"{}{}\" \"{}\"",
|
output.push_str(&format!(" \"{}\" \"{}\" \"{}{}\" \"{}\"",
|
||||||
tail, before, keyword, after, head).as_str());
|
tail, before, keyword, after, head));
|
||||||
if config.auto_ref || config.input_ref {
|
if config.auto_ref || config.input_ref {
|
||||||
output.push_str(&format!(" \"{}\"", adjust_roff_str(&reference)));
|
output.push_str(&format!(" \"{}\"", adjust_roff_str(&reference)));
|
||||||
}
|
}
|
||||||
|
@ -481,7 +482,7 @@ fn format_roff_line(config: &Config, word_ref: &WordRef, line: &str,
|
||||||
|
|
||||||
fn write_traditional_output(config: &Config,
|
fn write_traditional_output(config: &Config,
|
||||||
file_map: &HashMap<String, (Vec<String>,usize)>,
|
file_map: &HashMap<String, (Vec<String>,usize)>,
|
||||||
words: &BTreeSet<WordRef>, output_filename: &str) {
|
words: &BTreeSet<WordRef>, output_filename: &String) {
|
||||||
let mut writer: BufWriter<Box<Write>> = BufWriter::new(
|
let mut writer: BufWriter<Box<Write>> = BufWriter::new(
|
||||||
if output_filename == "-" {
|
if output_filename == "-" {
|
||||||
Box::new(stdout())
|
Box::new(stdout())
|
||||||
|
@ -553,13 +554,13 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let config = get_config(&matches);
|
let config = get_config(&matches);
|
||||||
let word_filter = WordFilter::new(&matches, &config);
|
let word_filter = WordFilter::new(&matches, &config);
|
||||||
let file_map =
|
let file_map =
|
||||||
read_input(matches.free.iter().map(|x| x.as_str()).collect(), &config);
|
read_input(&matches.free, &config);
|
||||||
let word_set = create_word_set(&config, &word_filter, &file_map);
|
let word_set = create_word_set(&config, &word_filter, &file_map);
|
||||||
let output_file = if !config.gnu_ext && matches.free.len() == 2 {
|
let output_file = if !config.gnu_ext && matches.free.len() == 2 {
|
||||||
matches.free[1].as_str()
|
matches.free[1].clone()
|
||||||
} else {
|
} else {
|
||||||
"-"
|
"-".to_string()
|
||||||
};
|
};
|
||||||
write_traditional_output(&config, &file_map, &word_set, output_file);
|
write_traditional_output(&config, &file_map, &word_set, &output_file);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue