1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
use std::env::args;
use std::io::{stdin, BufRead, BufReader};
/// Words that will not be capitalized unless they are at the start or end of a title.
const EXCEPTIONS: &[&str] = &[
"vs", "above", "across", "against", "at", "between", "by", "along", "among", "down", "in",
"once", "around", "of", "off", "on", "to", "with", "before", "behind", "below", "beneath",
"down", "from", "near", "toward", "upon", "within", "a", "an", "the", "and", "but", "for",
"or", "nor", "so", "till", "when", "yet", "that", "than", "in", "into", "like", "onto", "over",
"past", "as", "if",
];
/// Here are the rules:
///
/// - Every line is its own title.
/// - All words are capitalized, except...
/// - 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();
while let Some(arg) = args.next() {
match arg.as_str() {
"--ignore" | "-i" => {
ignorables = args.collect();
break;
}
"--help" | "-h" => {
println!("Title-Case Tool.\n-i | --ignore [word...]\tIgnores a list of words that should not be altered.");
}
unrecognized => {
panic!(format!("Unrecognized option: {}", unrecognized));
}
}
}
for line in BufReader::new(stdin()).lines() {
let line = line.expect("IO error");
println!("{}", correct_line(&line, &ignorables));
}
}
/// Corrects a single line (title) based on placement and exceptions.
fn correct_line(line: &str, ignorables: &[String]) -> String {
let mut line = line.split_whitespace().peekable();
let mut result = Vec::new();
if let Some(first) = line.next() {
// always correct the first word
result.push(correct_word(first, true, ignorables));
} else {
// handle empty lines
return String::new();
}
// handle every other word
while let Some(word) = line.next() {
result.push(correct_word(word, line.peek().is_none(), ignorables));
}
// join words into title
result.join(" ")
}
/// 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) {
// leave ignorables untouched
word.to_owned()
} else if first_or_last || !EXCEPTIONS.iter().any(|s| **s == word.to_lowercase()) {
// capitalize words that are first, last, or aren't exceptions
capitalize_word(&word.to_lowercase())
} else {
// lowercase all other words
word.to_lowercase()
}
}
// correct with just ignorables
// correct with ignorables and exceptions
/// Capitalizes the first letter of a single word.
fn capitalize_word(word: &str) -> String {
let mut chars = word.chars();
let first = chars.next().unwrap_or_default();
first.to_uppercase().chain(chars).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_capitalize_word() {
assert_eq!(capitalize_word("word"), String::from("Word"));
assert_eq!(capitalize_word("as"), String::from("As"));
}
#[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"));
assert_eq!(
correct_word("computers", false, &[]),
String::from("Computers")
);
}
#[test]
fn test_correct_line() {
assert_eq!(
correct_line("this is a test", &[]),
String::from("This Is a Test")
);
assert_eq!(
correct_line("similEs: lIke Or As", &[]),
String::from("Similes: like or As")
);
}
#[test]
fn test_proper_nouns_acronyms() {
assert_eq!(
correct_line("FreeBSD", &[String::from("FreeBSD")]),
String::from("FreeBSD")
);
}
#[test]
fn test_empty_lines() {
assert_eq!(correct_line("", &[]), String::from(""));
}
}
|