Skip to content

Commit

Permalink
wrap casemap options in auto
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Feb 7, 2025
1 parent 7e6ef30 commit dbee46a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
4 changes: 2 additions & 2 deletions components/casemap/src/casemapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl CaseMapper {
char_is_lead: impl Fn(&CaseMap, char) -> bool,
) -> StringAndWriteable<'a, FullCaseWriteable<'a, true>> {
let data = self.data.get();
let (head, rest) = match options.leading_adjustment {
let (head, rest) = match options.leading_adjustment.unwrap_or_default() {
LeadingAdjustment::Auto | LeadingAdjustment::ToCased => {
let first_cased = src.char_indices().find(|(_i, ch)| char_is_lead(data, *ch));
if let Some((first_cased, _ch)) = first_cased {
Expand All @@ -200,7 +200,7 @@ impl CaseMapper {
rest,
CaseMapLocale::from_langid(langid),
MappingKind::Title,
options.trailing_case,
options.trailing_case.unwrap_or_default(),
);
StringAndWriteable {
string: head,
Expand Down
20 changes: 12 additions & 8 deletions components/casemap/src/titlecase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use writeable::Writeable;
///
/// let default_options = Default::default();
/// let mut preserve_case: TitlecaseOptions = Default::default();
/// preserve_case.trailing_case = TrailingCase::Unchanged;
/// preserve_case.trailing_case = Some(TrailingCase::Unchanged);
///
/// // Exhibits trailing case when set:
/// assert_eq!(
Expand Down Expand Up @@ -74,8 +74,8 @@ pub enum TrailingCase {
/// let default_options = Default::default(); // head adjustment set to Auto
/// let mut no_adjust: TitlecaseOptions = Default::default();
/// let mut adjust_to_cased: TitlecaseOptions = Default::default();
/// no_adjust.leading_adjustment = LeadingAdjustment::None;
/// adjust_to_cased.leading_adjustment = LeadingAdjustment::ToCased;
/// no_adjust.leading_adjustment = Some(LeadingAdjustment::None);
/// adjust_to_cased.leading_adjustment = Some(LeadingAdjustment::ToCased);
///
/// // Exhibits leading adjustment when set:
/// assert_eq!(
Expand Down Expand Up @@ -147,10 +147,14 @@ pub enum LeadingAdjustment {
pub struct TitlecaseOptions {
/// How to handle the rest of the string once the head of the
/// string has been titlecased
pub trailing_case: TrailingCase,
///
/// Default is [`TrailingCase::Lower`]
pub trailing_case: Option<TrailingCase>,
/// Whether to start casing at the beginning of the string or at the first
/// relevant character.
pub leading_adjustment: LeadingAdjustment,
///
/// Default is [`LeadingAdjustment::Auto`]
pub leading_adjustment: Option<LeadingAdjustment>,
}

/// A wrapper around [`CaseMapper`] that can compute titlecasing stuff, and is able to load additional data
Expand Down Expand Up @@ -301,7 +305,7 @@ impl<CM: AsRef<CaseMapper>> TitlecaseMapper<CM> {
langid: &LanguageIdentifier,
options: TitlecaseOptions,
) -> impl Writeable + 'a {
if options.leading_adjustment == LeadingAdjustment::Auto {
if options.leading_adjustment.unwrap_or_default() == LeadingAdjustment::Auto {
// letter, number, symbol, or private use code point
const HEAD_GROUPS: GeneralCategoryGroup = GeneralCategoryGroup::Letter
.union(GeneralCategoryGroup::Number)
Expand Down Expand Up @@ -375,7 +379,7 @@ impl<CM: AsRef<CaseMapper>> TitlecaseMapper<CM> {
///
/// let default_options = Default::default();
/// let mut no_adjust: TitlecaseOptions = Default::default();
/// no_adjust.leading_adjustment = LeadingAdjustment::None;
/// no_adjust.leading_adjustment = Some(LeadingAdjustment::None);
///
/// // Exhibits leading adjustment when set:
/// assert_eq!(
Expand Down Expand Up @@ -415,7 +419,7 @@ impl<CM: AsRef<CaseMapper>> TitlecaseMapper<CM> {
///
/// let default_options = Default::default();
/// let mut preserve_case: TitlecaseOptions = Default::default();
/// preserve_case.trailing_case = TrailingCase::Unchanged;
/// preserve_case.trailing_case = Some(TrailingCase::Unchanged);
///
/// // Exhibits trailing case when set:
/// assert_eq!(
Expand Down
10 changes: 2 additions & 8 deletions ffi/capi/src/casemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,9 @@ impl From<ffi::TitlecaseOptionsV1> for TitlecaseOptions {
fn from(other: ffi::TitlecaseOptionsV1) -> Self {
let mut ret = Self::default();

ret.leading_adjustment = other
.leading_adjustment
.into_converted_option()
.unwrap_or(ret.leading_adjustment);
ret.leading_adjustment = other.leading_adjustment.into_converted_option();

ret.trailing_case = other
.trailing_case
.into_converted_option()
.unwrap_or(ret.trailing_case);
ret.trailing_case = other.trailing_case.into_converted_option();

ret
}
Expand Down
8 changes: 2 additions & 6 deletions ffi/capi/src/segmenter_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,8 @@ pub mod ffi {
impl From<ffi::LineBreakOptionsV2> for icu_segmenter::LineBreakOptions<'_> {
fn from(other: ffi::LineBreakOptionsV2) -> Self {
let mut options = icu_segmenter::LineBreakOptions::default();
options.strictness = other
.strictness
.into_converted_option();
options.word_option = other
.word_option
.into_converted_option();
options.strictness = other.strictness.into_converted_option();
options.word_option = other.word_option.into_converted_option();
options
}
}

0 comments on commit dbee46a

Please # to comment.