summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Redmond <adredmond@gmail.com>2021-03-04 22:17:38 -0500
committerAndrew Redmond <adredmond@gmail.com>2021-03-04 22:17:38 -0500
commit78b7a3718a7d5c1ae75241bce1144fd723de3954 (patch)
tree4acf431663f4b7a37506cb5dec4d9fa393a0c626
parentb230c913ede87dd5ea0f445cb35f63d714037436 (diff)
downloadtitler-78b7a3718a7d5c1ae75241bce1144fd723de3954.tar.xz
titler-78b7a3718a7d5c1ae75241bce1144fd723de3954.zip
changes to args collection processing.
ignorables converted to HashSet.
-rw-r--r--src/main.rs41
1 files 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<String> = args().skip(1).collect();
+ let mut ignorables: HashSet<String> = 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::<Vec<String>>());
+ 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>) -> 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>) -> 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<String> = 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<String> = 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<String> = ["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<String> = HashSet::new();
+ assert_eq!(correct_line("", &ignores), String::from(""));
}
}