Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify use of endswith #75

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,11 @@ fn join_words<'a, I: Iterator<Item = &'a str>>(mut words: I) -> String {
match words.next() {
None => String::new(),
Some(word) => {
// Closure to determine whether a character ends a sentence.
let is_sentence_end = |c: char| c == '.' || c == '!' || c == '?';
// Punctuation characters which ends a sentence.
let punctuation = &['.', '!', '?'];

let mut sentence = capitalize(word);
let mut needs_cap = sentence.ends_with(is_sentence_end);
let mut needs_cap = sentence.ends_with(punctuation);

// Add remaining words.
for word in words {
Expand All @@ -359,11 +359,11 @@ fn join_words<'a, I: Iterator<Item = &'a str>>(mut words: I) -> String {
sentence.push_str(word);
}

needs_cap = word.ends_with(is_sentence_end);
needs_cap = word.ends_with(punctuation);
}

// Ensure the sentence ends with either one of ".!?".
if !sentence.ends_with(is_sentence_end) {
if !sentence.ends_with(punctuation) {
// Trim all trailing punctuation characters to avoid
// adding '.' after a ',' or similar.
let idx = sentence.trim_end_matches(is_ascii_punctuation).len();
Expand Down