From 78b7a3718a7d5c1ae75241bce1144fd723de3954 Mon Sep 17 00:00:00 2001 From: Andrew Redmond Date: Thu, 4 Mar 2021 22:17:38 -0500 Subject: changes to args collection processing. ignorables converted to HashSet. --- src/main.rs | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index b060ed1..c5b5b83 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::iter::FromIterator; +use std::collections::HashSet; use std::env::args; use std::io::{stdin, BufRead, BufReader}; @@ -17,14 +19,14 @@ const EXCEPTIONS: &[&str] = &[ /// - Words are not capitalized if they are EXCEPTIONS, unless... /// - Words are either the first or the last word. fn main() { - let mut args = args(); - args.next(); // ignore self arg - let mut ignorables = Vec::new(); + let args: Vec = args().skip(1).collect(); + let mut ignorables: HashSet = HashSet::new(); - while let Some(arg) = args.next() { + for arg in args.iter() { match arg.as_str() { "--ignore" | "-i" => { - ignorables = args.collect(); + ignorables = HashSet::from_iter(args.iter().cloned().skip(1).collect::>()); + println!("{:?}", ignorables); break; } "--help" | "-h" => { @@ -43,7 +45,7 @@ fn main() { } /// Corrects a single line (title) based on placement and exceptions. -fn correct_line(line: &str, ignorables: &[String]) -> String { +fn correct_line(line: &str, ignorables: &HashSet) -> String { let mut line = line.split_whitespace().peekable(); let mut result = Vec::new(); @@ -65,8 +67,8 @@ fn correct_line(line: &str, ignorables: &[String]) -> String { } /// Corrects a single word based on exceptions and ignorables. -fn correct_word(word: &str, first_or_last: bool, ignorables: &[String]) -> String { - if ignorables.iter().any(|s| s == word) { +fn correct_word(word: &str, first_or_last: bool, ignorables: &HashSet) -> String { + if ignorables.contains(word) { // leave ignorables untouched word.to_owned() } else if first_or_last || !EXCEPTIONS.iter().any(|s| **s == word.to_lowercase()) { @@ -100,38 +102,43 @@ mod tests { #[test] fn test_correct_word() { - assert_eq!(correct_word("Like", false, &[]), String::from("like")); - assert_eq!(correct_word("like", false, &[]), String::from("like")); - assert_eq!(correct_word("like", true, &[]), String::from("Like")); - assert_eq!(correct_word("liKe", false, &[]), String::from("like")); + let ignores: HashSet = HashSet::new(); + assert_eq!(correct_word("Like", false, &ignores), String::from("like")); + assert_eq!(correct_word("like", false, &ignores), String::from("like")); + assert_eq!(correct_word("like", true, &ignores), String::from("Like")); + assert_eq!(correct_word("liKe", false, &ignores), String::from("like")); assert_eq!( - correct_word("computers", false, &[]), + correct_word("computers", false, &ignores), String::from("Computers") ); } #[test] fn test_correct_line() { + let ignores: HashSet = HashSet::new(); assert_eq!( - correct_line("this is a test", &[]), + correct_line("this is a test", &ignores), String::from("This Is a Test") ); assert_eq!( - correct_line("similEs: lIke Or As", &[]), + correct_line("similEs: lIke Or As", &ignores), String::from("Similes: like or As") ); } #[test] fn test_proper_nouns_acronyms() { + let ignores: HashSet = ["FreeBSD".to_string()].iter().cloned().collect(); + assert_eq!( - correct_line("FreeBSD", &[String::from("FreeBSD")]), + correct_line("FreeBSD", &ignores), String::from("FreeBSD") ); } #[test] fn test_empty_lines() { - assert_eq!(correct_line("", &[]), String::from("")); + let ignores: HashSet = HashSet::new(); + assert_eq!(correct_line("", &ignores), String::from("")); } } -- cgit v1.2.3