use crate::analysis::{CasingRule, CasingSpan, Confidence, TitleCaseAnalysis};
use crate::casing::{lowercase_word, push_styled, StyleOutcome};
use crate::config::{
AllCapsPolicy, HyphenStyle, LocaleProfile, NameParticlePolicy, SmallWordPolicy,
TitleCaseOptions, UnknownWordCasing,
};
use crate::context::{
first_significant_word, followed_by_hyphen, follows_subtitle_boundary, is_contracted_and,
last_significant_word, likely_adverbial_particle, likely_name_particle_context,
part_of_hyphenated_compound, preceded_by_hyphen, precedes_subtitle_boundary,
};
use crate::lexicon::{
abbreviation_spelling, built_in_protected_spelling, is_name_particle_for_locale, is_small_word,
};
use crate::token::Token;
use crate::util::normalize::{lookup_key, normalized_key};
use crate::util::unicode::{append_uppercase, push_lowercased};
pub(crate) fn apply(input: &str, tokens: &[Token<'_>], options: &TitleCaseOptions<'_>) -> String {
let mut output = String::new();
let mut spans = Vec::new();
run::<false>(input, &mut output, &mut spans, tokens, options);
output
}
pub(crate) fn apply_into(
output: &mut String,
input: &str,
tokens: &[Token<'_>],
options: &TitleCaseOptions<'_>,
) {
let mut spans = Vec::new();
run::<false>(input, output, &mut spans, tokens, options);
}
pub(crate) fn analyze(
input: &str,
tokens: &[Token<'_>],
options: &TitleCaseOptions<'_>,
) -> TitleCaseAnalysis {
let mut output = String::new();
let mut spans = Vec::new();
run::<true>(input, &mut output, &mut spans, tokens, options);
let confidence =
spans.iter().fold(Confidence::Solid, |worst, span| worst.most_concerning(span.confidence));
TitleCaseAnalysis { output, confidence, spans }
}
fn run<const RECORD: bool>(
input: &str,
output: &mut String,
spans: &mut Vec<CasingSpan>,
tokens: &[Token<'_>],
options: &TitleCaseOptions<'_>,
) {
let first = first_significant_word(tokens);
let last = last_significant_word(tokens);
let shouting = input_is_all_caps(tokens);
output.reserve(tokens.iter().map(|token| token.text.len()).sum());
if shouting && options.all_caps_policy == AllCapsPolicy::Preserve {
for token in tokens {
output.push_str(token.text);
}
return;
}
let base = input.as_ptr() as usize;
let mut index = 0_usize;
while index < tokens.len() {
let token = &tokens[index];
if !token.is_word() {
output.push_str(token.text);
index += 1;
continue;
}
let out_start = output.len();
if let Some((end_index, canonical_phrase)) = options
.external_lexicons
.and_then(|lexicons| lexicons.multiword_spelling(tokens, index))
.filter(|&(end_index, _)| !phrase_contains_protected(tokens, index, end_index, options))
{
let capitalize_first = first == Some(index)
|| (options.capitalize_after_subtitle_boundary
&& follows_subtitle_boundary(tokens, index));
if capitalize_first {
push_phrase_capitalized(output, canonical_phrase, options.locale);
} else {
output.push_str(canonical_phrase);
}
if RECORD {
let source_start = token.text.as_ptr() as usize - base;
let end_token = &tokens[end_index];
let source_end = end_token.text.as_ptr() as usize - base + end_token.text.len();
record_span(
spans,
input,
output,
source_start..source_end,
out_start,
CasingRule::MultiwordLexicon,
);
}
index = end_index + 1;
continue;
}
let rule = emit_word::<RECORD>(output, tokens, index, first, last, shouting, options);
if RECORD {
let source_start = token.text.as_ptr() as usize - base;
record_span(
spans,
input,
output,
source_start..source_start + token.text.len(),
out_start,
rule,
);
}
index += 1;
}
}
fn phrase_contains_protected(
tokens: &[Token<'_>],
start: usize,
end: usize,
options: &TitleCaseOptions<'_>,
) -> bool {
tokens[start..=end].iter().any(|token| {
token.is_word()
&& protected_spelling(token.text, &normalized_key(token.text), options).is_some()
})
}
fn push_phrase_capitalized(output: &mut String, phrase: &str, locale: LocaleProfile) {
let mut chars = phrase.chars();
if let Some(first) = chars.next() {
append_uppercase(output, first, locale);
output.push_str(chars.as_str());
}
}
fn record_span(
spans: &mut Vec<CasingSpan>,
input: &str,
output: &str,
source: core::ops::Range<usize>,
out_start: usize,
rule: CasingRule,
) {
let source_text = &input[source.clone()];
let output_text = &output[out_start..];
let changed = source_text != output_text;
let confidence = refine_confidence(rule, source_text);
spans.push(CasingSpan { source, output: out_start..output.len(), rule, confidence, changed });
}
fn refine_confidence(rule: CasingRule, source_text: &str) -> Confidence {
if rule == CasingRule::SmallWord
&& matches!(
normalized_key(source_text).as_ref(),
"after" | "before" | "since" | "till" | "until"
)
{
return Confidence::Heuristic;
}
rule.confidence()
}
fn emit_word<const RECORD: bool>(
output: &mut String,
tokens: &[Token<'_>],
index: usize,
first: Option<usize>,
last: Option<usize>,
shouting: bool,
options: &TitleCaseOptions<'_>,
) -> CasingRule {
let token = &tokens[index];
let key = normalized_key(token.text);
let is_first = first == Some(index);
let is_last = last == Some(index);
let at_subtitle_boundary = options.capitalize_after_subtitle_boundary
&& (follows_subtitle_boundary(tokens, index) || precedes_subtitle_boundary(tokens, index));
let hyphenated = part_of_hyphenated_compound(tokens, index);
let starts_hyphenated_compound =
followed_by_hyphen(tokens, index) && !preceded_by_hyphen(tokens, index);
let capitalize_hyphen = starts_hyphenated_compound
|| (hyphenated && matches!(options.hyphen_style, HyphenStyle::CapitalizeBoth));
let should_capitalize = is_first || is_last || at_subtitle_boundary || capitalize_hyphen;
if let Some(protected) = protected_spelling(token.text, &key, options) {
output.push_str(protected);
return CasingRule::ProtectedSpelling;
}
if !should_capitalize && is_contracted_and(tokens, index, &key) {
push_lowercased(output, token.text, LocaleProfile::English);
return CasingRule::ContractedAnd;
}
if is_lowerable_name_particle(&key, should_capitalize, tokens, index, options) {
push_lowercased(output, token.text, LocaleProfile::English);
return CasingRule::NameParticle;
}
if should_force_lowercase(&key, should_capitalize, tokens, index, options) {
push_lowercased(output, token.text, LocaleProfile::English);
return CasingRule::SmallWord;
}
if let Some(abbreviation) = abbreviation_spelling(&key) {
output.push_str(abbreviation);
return CasingRule::Abbreviation;
}
if let Some(canonical) =
options.external_lexicons.and_then(|lexicons| lexicons.canonical_spelling(token.text))
{
output.push_str(canonical);
return CasingRule::CanonicalLexicon;
}
if shouting && normalize_all_caps_word(&key, options) {
let lowered = lowercase_word(token.text, options.locale);
let _ = push_styled(output, &lowered, true, should_capitalize, options);
return CasingRule::AllCapsNormalized;
}
let outcome = push_styled(output, token.text, true, should_capitalize, options);
if RECORD {
rule_for_style(
outcome,
shouting,
is_first,
is_last,
at_subtitle_boundary,
capitalize_hyphen,
tokens,
index,
&key,
options,
)
} else {
CasingRule::Capitalized
}
}
#[allow(clippy::too_many_arguments)]
fn rule_for_style(
outcome: StyleOutcome,
shouting: bool,
is_first: bool,
is_last: bool,
at_subtitle_boundary: bool,
capitalize_hyphen: bool,
tokens: &[Token<'_>],
index: usize,
key: &str,
options: &TitleCaseOptions<'_>,
) -> CasingRule {
match outcome {
StyleOutcome::AcronymPreserved if shouting => CasingRule::AllCapsPreservedAcronym,
StyleOutcome::AcronymPreserved => CasingRule::AcronymPreserved,
StyleOutcome::DottedAbbreviation => CasingRule::DottedAbbreviation,
StyleOutcome::MixedCasePreserved => CasingRule::MixedCasePreserved,
StyleOutcome::Lowercased | StyleOutcome::Capitalized => {
if is_first {
CasingRule::FirstWord
} else if is_last {
CasingRule::LastWord
} else if at_subtitle_boundary {
CasingRule::SubtitleBoundary
} else if capitalize_hyphen {
CasingRule::HyphenatedCompound
} else if options.capitalize_phrasal_particles
&& likely_adverbial_particle(tokens, index, key)
{
CasingRule::AdverbialParticle
} else {
CasingRule::Capitalized
}
}
}
}
fn normalize_all_caps_word(key: &str, options: &TitleCaseOptions<'_>) -> bool {
match options.all_caps_policy {
AllCapsPolicy::Normalize => true,
AllCapsPolicy::NormalizeKnownWords { unknown } => {
match options.external_lexicons.and_then(|lexicons| lexicons.dictionary_contains(key)) {
None => true,
Some(true) => true,
Some(false) => match unknown {
UnknownWordCasing::Preserve => false,
UnknownWordCasing::TitleCase => true,
UnknownWordCasing::PreserveShortAcronyms { max_acronym_len } => {
letter_count(key) > max_acronym_len
}
},
}
}
AllCapsPolicy::Preserve => false,
}
}
fn letter_count(word: &str) -> usize {
word.chars().filter(|ch| ch.is_alphabetic()).count()
}
fn input_is_all_caps(tokens: &[Token<'_>]) -> bool {
let mut cased_words = 0_usize;
for token in tokens {
if !token.is_word() {
continue;
}
let mut has_cased_letter = false;
for ch in token.text.chars() {
if ch.is_lowercase() {
return false;
}
if ch.is_uppercase() {
has_cased_letter = true;
}
}
if has_cased_letter {
cased_words += 1;
}
}
cased_words > 1
}
fn protected_spelling<'a>(
original: &str,
key: &str,
options: &'a TitleCaseOptions<'a>,
) -> Option<&'a str> {
options
.protected_words
.iter()
.copied()
.find(|candidate| lookup_key(candidate) == key)
.or_else(|| built_in_protected_spelling(key))
.or_else(|| {
options.external_lexicons.and_then(|lexicons| lexicons.protected_spelling(original))
})
}
fn is_lowerable_name_particle(
key: &str,
should_capitalize: bool,
tokens: &[Token<'_>],
index: usize,
options: &TitleCaseOptions<'_>,
) -> bool {
!should_capitalize
&& options.name_particle_policy == NameParticlePolicy::Heuristic
&& is_name_particle_for_locale(key, options.locale)
&& likely_name_particle_context(tokens, index)
}
fn should_force_lowercase(
key: &str,
should_capitalize: bool,
tokens: &[Token<'_>],
index: usize,
options: &TitleCaseOptions<'_>,
) -> bool {
if should_capitalize {
return false;
}
if options.capitalize_phrasal_particles && likely_adverbial_particle(tokens, index, key) {
return false;
}
match options.small_word_policy {
SmallWordPolicy::Mla => is_small_word(key),
SmallWordPolicy::AlwaysLowercase => {
is_small_word(key)
|| options.external_lexicons.is_some_and(|lexicons| lexicons.contains_word(key))
}
SmallWordPolicy::NeverLowercase => false,
}
}