Sort by key
This commit is contained in:
36
src/main.rs
36
src/main.rs
@@ -8,7 +8,8 @@ fn main() -> Result<(), Error> {
|
||||
let args = Args::parse();
|
||||
|
||||
let wordlist = read_to_string(&args.filepath)?;
|
||||
let mut list: Vec<&str> = wordlist.split("\n").collect();
|
||||
let list: Vec<&str> = wordlist.split("\n").collect();
|
||||
let mut output = String::from("");
|
||||
|
||||
let lowerbound = list.iter().position(|&x| x.contains("\\begin{acronym}"));
|
||||
let upperbound = list.iter().position(|&x| x.contains("\\end{acronym}"));
|
||||
@@ -16,7 +17,25 @@ fn main() -> Result<(), Error> {
|
||||
match (lowerbound, upperbound) {
|
||||
(Some(lower), Some(upper)) => {
|
||||
println!("Sort range {}-{}", lower, upper);
|
||||
sort_range(lower + 1, upper, &mut list)
|
||||
let mut parsed_list : Vec<Option<(&str, &str, &str)>> = list[lower..upper].iter().map(|&x| extract_acronym(x)).collect();
|
||||
|
||||
sort_range(&mut parsed_list);
|
||||
output.push_str(list[..lower+1].join("\n").as_str());
|
||||
output.push_str("\n");
|
||||
let offset = upper - lower;
|
||||
for i in 0..offset {
|
||||
match parsed_list[i] {
|
||||
Some(tuple) => {
|
||||
output.push_str(format!(" \\acro{{{0}}}[{1}]{{{2}}}\n", tuple.0, tuple.1, tuple.2).as_str());
|
||||
},
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
output.push_str(list[upper..].join("\n").as_str());
|
||||
|
||||
let mut writer = BufWriter::new(File::create(&args.filepath)?);
|
||||
writer.write(output.as_bytes())?;
|
||||
println!("Done.");
|
||||
},
|
||||
_ => {
|
||||
println!("No bounds found.");
|
||||
@@ -24,20 +43,19 @@ fn main() -> Result<(), Error> {
|
||||
}
|
||||
}
|
||||
|
||||
let mut writer = BufWriter::new(File::create(&args.filepath)?);
|
||||
writer.write(list.join("\n").as_bytes())?;
|
||||
println!("Done.");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sort_range(lowerbound: usize, upperbound: usize, list: &mut Vec<&str>) {
|
||||
list[lowerbound..upperbound].sort_unstable();
|
||||
fn sort_range(list: &mut Vec<Option<(&str, &str, &str)>>) {
|
||||
list.sort_unstable_by_key(|x| match x {
|
||||
Some(tuple) => tuple.0,
|
||||
None => ""
|
||||
});
|
||||
}
|
||||
|
||||
fn extract_acronym(line: &str) -> Option<(&str, &str, &str)> {
|
||||
lazy_static! {
|
||||
static ref RE: Regex = Regex::new(r"\{(\w+)\}\[(\w+)\]\{(.+)\}").unwrap();
|
||||
static ref RE: Regex = Regex::new(r"\{(.+)\}\[(.+)\]\{(.+)\}").unwrap();
|
||||
}
|
||||
let caps = RE.captures(line);
|
||||
match caps {
|
||||
|
Reference in New Issue
Block a user