split_vec optimized
This commit is contained in:
parent
15b98ff5aa
commit
d6ce684ef9
1 changed files with 28 additions and 15 deletions
43
src/main.rs
43
src/main.rs
|
@ -72,7 +72,7 @@ struct CLIArguments {
|
||||||
|
|
||||||
|
|
||||||
/// return whether or not user gave confirmation
|
/// return whether or not user gave confirmation
|
||||||
fn prompt_confirm(run_targets: &Vec<Vec<String>>) -> bool {
|
fn prompt_confirm(run_targets: &Vec<Vec<&String>>) -> bool {
|
||||||
println!("Are you sure you want to link all duplicates in each of these sets of targets?");
|
println!("Are you sure you want to link all duplicates in each of these sets of targets?");
|
||||||
for spaths in run_targets {
|
for spaths in run_targets {
|
||||||
println!(" {}", shlex::join(spaths.iter().map(|string| string.as_str())));
|
println!(" {}", shlex::join(spaths.iter().map(|string| string.as_str())));
|
||||||
|
@ -128,7 +128,7 @@ fn process_args() -> (Vec<Vec<PathBuf>>, Config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let run_targets: Vec<Vec<String>> = split_vec(&args.targets, &args.separator.unwrap_or(s_default_target_separator!().to_string()));
|
let run_targets: Vec<Vec<&String>> = split_vec(&args.targets, &args.separator.unwrap_or(s_default_target_separator!().to_string()));
|
||||||
|
|
||||||
if run_targets.is_empty() {
|
if run_targets.is_empty() {
|
||||||
if verbosity > 0 {
|
if verbosity > 0 {
|
||||||
|
@ -410,21 +410,34 @@ fn common_suffix<'a>(s1: &'a str, s2: &'a str) -> &'a str {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn split_vec(input: &[String], delimiter: &String) -> Vec<Vec<String>> {
|
fn split_vec<'a, T: std::cmp::PartialEq>(input: &'a [T], delimiter: &T) -> Vec<Vec<&'a T>> {
|
||||||
let mut result: Vec<Vec<String>> = Vec::new();
|
let mut result: Vec<Vec<&T>> = Vec::new();
|
||||||
let mut current_vec: Vec<String> = Vec::new();
|
|
||||||
for item in input.iter() {
|
let mut chunk_start = 0;
|
||||||
if item == delimiter {
|
for (i,item) in input.iter().enumerate() {
|
||||||
if !current_vec.is_empty() {
|
if item != delimiter {
|
||||||
result.push(current_vec);
|
continue
|
||||||
}
|
|
||||||
current_vec = Vec::new();
|
|
||||||
} else {
|
|
||||||
current_vec.push(item.to_string());
|
|
||||||
}
|
}
|
||||||
|
if i == chunk_start { // zero size chunk
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result.push(input[chunk_start..i].iter().collect::<Vec<&T>>());
|
||||||
|
chunk_start = i+1; // next chunk starts on next index
|
||||||
}
|
}
|
||||||
if !current_vec.is_empty() {
|
if chunk_start < input.len() {
|
||||||
result.push(current_vec);
|
result.push(input[chunk_start..].iter().collect::<Vec<&T>>());
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn _split_vec() {
|
||||||
|
let v: Vec<_> = vec![";", "hi", "bye", ";", "1", ";", ";", "2", "2", ";"].into_iter().map(|s| s.to_string()).collect();
|
||||||
|
let res = split_vec(&v[..], &";".to_string());
|
||||||
|
println!("{:?}", v);
|
||||||
|
println!("{:?}", res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue