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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
use crate::internals::{CaseMapLocale, FoldOptions, FullCaseWriteable, StringAndWriteable};
use crate::provider::data::MappingKind;
use crate::provider::CaseMapV1;
use crate::provider::CaseMapV1Marker;
use crate::set::ClosureSink;
use crate::titlecase::{LeadingAdjustment, TitlecaseOptions, TrailingCase};
use alloc::string::String;
use icu_locid::LanguageIdentifier;
use icu_provider::prelude::*;
use writeable::Writeable;
/// A struct with the ability to convert characters and strings to uppercase or lowercase,
/// or fold them to a normalized form for case-insensitive comparison.
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
/// use icu::locid::langid;
///
/// let cm = CaseMapper::new();
///
/// assert_eq!(
/// cm.uppercase_to_string("hello world", &langid!("und")),
/// "HELLO WORLD"
/// );
/// assert_eq!(
/// cm.lowercase_to_string("Γειά σου Κόσμε", &langid!("und")),
/// "γειά σου κόσμε"
/// );
/// ```
#[derive(Clone, Debug)]
pub struct CaseMapper {
pub(crate) data: DataPayload<CaseMapV1Marker>,
}
#[cfg(feature = "compiled_data")]
impl Default for CaseMapper {
fn default() -> Self {
Self::new()
}
}
impl AsRef<CaseMapper> for CaseMapper {
fn as_ref(&self) -> &CaseMapper {
self
}
}
impl CaseMapper {
/// Creates a [`CaseMapper`] using compiled data.
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
///
/// [📚 Help choosing a constructor](icu_provider::constructors)
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
/// use icu::locid::langid;
///
/// let cm = CaseMapper::new();
///
/// assert_eq!(
/// cm.uppercase_to_string("hello world", &langid!("und")),
/// "HELLO WORLD"
/// );
/// ```
#[cfg(feature = "compiled_data")]
pub const fn new() -> Self {
Self {
data: DataPayload::from_static_ref(crate::provider::Baked::SINGLETON_PROPS_CASEMAP_V1),
}
}
icu_provider::gen_any_buffer_data_constructors!(locale: skip, options: skip, error: DataError,
#[cfg(skip)]
functions: [
new,
try_new_with_any_provider,
try_new_with_buffer_provider,
try_new_unstable,
Self,
]);
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::new)]
pub fn try_new_unstable<P>(provider: &P) -> Result<CaseMapper, DataError>
where
P: DataProvider<CaseMapV1Marker> + ?Sized,
{
let data = provider.load(Default::default())?.take_payload()?;
Ok(Self { data })
}
/// Returns the full lowercase mapping of the given string as a [`Writeable`].
/// This function is context and language sensitive. Callers should pass the text's language
/// as a `LanguageIdentifier` (usually the `id` field of the `Locale`) if available, or
/// `Default::default()` for the root locale.
///
/// See [`Self::lowercase_to_string()`] for the equivalent convenience function that returns a String,
/// as well as for an example.
pub fn lowercase<'a>(
&'a self,
src: &'a str,
langid: &LanguageIdentifier,
) -> impl Writeable + 'a {
self.data.get().full_helper_writeable::<false>(
src,
CaseMapLocale::from_langid(langid),
MappingKind::Lower,
TrailingCase::default(),
)
}
/// Returns the full uppercase mapping of the given string as a [`Writeable`].
/// This function is context and language sensitive. Callers should pass the text's language
/// as a `LanguageIdentifier` (usually the `id` field of the `Locale`) if available, or
/// `Default::default()` for the root locale.
///
/// See [`Self::uppercase_to_string()`] for the equivalent convenience function that returns a String,
/// as well as for an example.
pub fn uppercase<'a>(
&'a self,
src: &'a str,
langid: &LanguageIdentifier,
) -> impl Writeable + 'a {
self.data.get().full_helper_writeable::<false>(
src,
CaseMapLocale::from_langid(langid),
MappingKind::Upper,
TrailingCase::default(),
)
}
/// Returns the full titlecase mapping of the given string as a [`Writeable`], treating
/// the string as a single segment (and thus only titlecasing the beginning of it). Performs
/// the specified leading adjustment behavior from the options without loading additional data.
///
/// This should typically be used as a lower-level helper to construct the titlecasing operation desired
/// by the application, for example one can titlecase on a per-word basis by mixing this with
/// a `WordSegmenter`.
///
/// This function is context and language sensitive. Callers should pass the text's language
/// as a `LanguageIdentifier` (usually the `id` field of the `Locale`) if available, or
/// `Default::default()` for the root locale.
///
/// This function performs "adjust to cased" leading adjustment behavior when [`LeadingAdjustment::Auto`] or [`LeadingAdjustment::ToCased`]
/// is set. Auto mode is not able to pick the "adjust to letter/number/symbol" behavior as this type does not load
/// the data to do so, use [`TitlecaseMapper`] if such behavior is desired. See
/// the docs of [`TitlecaseMapper`] for more information on what this means. There is no difference between
/// the behavior of this function and the equivalent ones on [`TitlecaseMapper`] when the head adjustment mode
/// is [`LeadingAdjustment::None`].
///
/// See [`Self::titlecase_segment_with_only_case_data_to_string()`] for the equivalent convenience function that returns a String,
/// as well as for an example.
///
/// [`TitlecaseMapper`]: crate::TitlecaseMapper
pub fn titlecase_segment_with_only_case_data<'a>(
&'a self,
src: &'a str,
langid: &LanguageIdentifier,
options: TitlecaseOptions,
) -> impl Writeable + 'a {
self.titlecase_segment_with_adjustment(src, langid, options, |data, ch| data.is_cased(ch))
}
/// Helper to support different leading adjustment behaviors,
/// `char_is_lead` is a function that returns true for a character that is allowed to be the
/// first relevant character in a titlecasing string, when `leading_adjustment != None`
///
/// We return a concrete type instead of `impl Trait` so the return value can be mixed with that of other calls
/// to this function with different closures
pub(crate) fn titlecase_segment_with_adjustment<'a>(
&'a self,
src: &'a str,
langid: &LanguageIdentifier,
options: TitlecaseOptions,
char_is_lead: impl Fn(&CaseMapV1, char) -> bool,
) -> StringAndWriteable<FullCaseWriteable<'a, true>> {
let data = self.data.get();
let (head, rest) = match options.leading_adjustment {
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 {
(
src.get(..first_cased).unwrap_or(""),
src.get(first_cased..).unwrap_or(""),
)
} else {
(src, "")
}
}
LeadingAdjustment::None => ("", src),
};
let writeable = data.full_helper_writeable::<true>(
rest,
CaseMapLocale::from_langid(langid),
MappingKind::Title,
options.trailing_case,
);
StringAndWriteable {
string: head,
writeable,
}
}
/// Case-folds the characters in the given string as a [`Writeable`].
/// This function is locale-independent and context-insensitive.
///
/// Can be used to test if two strings are case-insensitively equivalent.
///
/// See [`Self::fold_string()`] for the equivalent convenience function that returns a String,
/// as well as for an example.
pub fn fold<'a>(&'a self, src: &'a str) -> impl Writeable + 'a {
self.data.get().full_helper_writeable::<false>(
src,
CaseMapLocale::Root,
MappingKind::Fold,
TrailingCase::default(),
)
}
/// Case-folds the characters in the given string as a [`Writeable`],
/// using Turkic (T) mappings for dotted/dotless I.
/// This function is locale-independent and context-insensitive.
///
/// Can be used to test if two strings are case-insensitively equivalent.
///
/// See [`Self::fold_turkic_string()`] for the equivalent convenience function that returns a String,
/// as well as for an example.
pub fn fold_turkic<'a>(&'a self, src: &'a str) -> impl Writeable + 'a {
self.data.get().full_helper_writeable::<false>(
src,
CaseMapLocale::Turkish,
MappingKind::Fold,
TrailingCase::default(),
)
}
/// Returns the full lowercase mapping of the given string as a String.
///
/// This function is context and language sensitive. Callers should pass the text's language
/// as a `LanguageIdentifier` (usually the `id` field of the `Locale`) if available, or
/// `Default::default()` for the root locale.
///
/// See [`Self::lowercase()`] for the equivalent lower-level function that returns a [`Writeable`]
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
/// use icu::locid::langid;
///
/// let cm = CaseMapper::new();
/// let root = langid!("und");
///
/// assert_eq!(cm.lowercase_to_string("hEllO WorLd", &root), "hello world");
/// assert_eq!(cm.lowercase_to_string("Γειά σου Κόσμε", &root), "γειά σου κόσμε");
/// assert_eq!(cm.lowercase_to_string("नमस्ते दुनिया", &root), "नमस्ते दुनिया");
/// assert_eq!(cm.lowercase_to_string("Привет мир", &root), "привет мир");
///
/// // Some behavior is language-sensitive
/// assert_eq!(cm.lowercase_to_string("CONSTANTINOPLE", &root), "constantinople");
/// assert_eq!(cm.lowercase_to_string("CONSTANTINOPLE", &langid!("tr")), "constantınople");
/// ```
pub fn lowercase_to_string(&self, src: &str, langid: &LanguageIdentifier) -> String {
self.lowercase(src, langid).write_to_string().into_owned()
}
/// Returns the full uppercase mapping of the given string as a String.
///
/// This function is context and language sensitive. Callers should pass the text's language
/// as a `LanguageIdentifier` (usually the `id` field of the `Locale`) if available, or
/// `Default::default()` for the root locale.
///
/// See [`Self::uppercase()`] for the equivalent lower-level function that returns a [`Writeable`]
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
/// use icu::locid::langid;
///
/// let cm = CaseMapper::new();
/// let root = langid!("und");
///
/// assert_eq!(cm.uppercase_to_string("hEllO WorLd", &root), "HELLO WORLD");
/// assert_eq!(cm.uppercase_to_string("Γειά σου Κόσμε", &root), "ΓΕΙΆ ΣΟΥ ΚΌΣΜΕ");
/// assert_eq!(cm.uppercase_to_string("नमस्ते दुनिया", &root), "नमस्ते दुनिया");
/// assert_eq!(cm.uppercase_to_string("Привет мир", &root), "ПРИВЕТ МИР");
///
/// // Some behavior is language-sensitive
/// assert_eq!(cm.uppercase_to_string("istanbul", &root), "ISTANBUL");
/// assert_eq!(cm.uppercase_to_string("istanbul", &langid!("tr")), "İSTANBUL"); // Turkish dotted i
///
/// assert_eq!(cm.uppercase_to_string("և Երևանի", &root), "ԵՒ ԵՐԵՒԱՆԻ");
/// assert_eq!(cm.uppercase_to_string("և Երևանի", &langid!("hy")), "ԵՎ ԵՐԵՎԱՆԻ"); // Eastern Armenian ech-yiwn ligature
/// ```
pub fn uppercase_to_string(&self, src: &str, langid: &LanguageIdentifier) -> String {
self.uppercase(src, langid).write_to_string().into_owned()
}
/// Returns the full titlecase mapping of the given string as a [`Writeable`], treating
/// the string as a single segment (and thus only titlecasing the beginning of it). Performs
/// the specified leading adjustment behavior from the options without loading additional data.
///
/// Note that [`TitlecaseMapper`] has better behavior, most users should consider using
/// it instead. This method primarily exists for people who care about the amount of data being loaded.
///
/// This should typically be used as a lower-level helper to construct the titlecasing operation desired
/// by the application, for example one can titlecase on a per-word basis by mixing this with
/// a `WordSegmenter`.
///
/// This function is context and language sensitive. Callers should pass the text's language
/// as a `LanguageIdentifier` (usually the `id` field of the `Locale`) if available, or
/// `Default::default()` for the root locale.
///
/// This function performs "adjust to cased" leading adjustment behavior when [`LeadingAdjustment::Auto`] or [`LeadingAdjustment::ToCased`]
/// is set. Auto mode is not able to pick the "adjust to letter/number/symbol" behavior as this type does not load
/// the data to do so, use [`TitlecaseMapper`] if such behavior is desired. See
/// the docs of [`TitlecaseMapper`] for more information on what this means. There is no difference between
/// the behavior of this function and the equivalent ones on [`TitlecaseMapper`] when the head adjustment mode
/// is [`LeadingAdjustment::None`].
///
/// See [`Self::titlecase_segment_with_only_case_data()`] for the equivalent lower-level function that returns a [`Writeable`]
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
/// use icu::locid::langid;
///
/// let cm = CaseMapper::new();
/// let root = langid!("und");
///
/// let default_options = Default::default();
///
/// // note that the subsequent words are not titlecased, this function assumes
/// // that the entire string is a single segment and only titlecases at the beginning.
/// assert_eq!(cm.titlecase_segment_with_only_case_data_to_string("hEllO WorLd", &root, default_options), "Hello world");
/// assert_eq!(cm.titlecase_segment_with_only_case_data_to_string("Γειά σου Κόσμε", &root, default_options), "Γειά σου κόσμε");
/// assert_eq!(cm.titlecase_segment_with_only_case_data_to_string("नमस्ते दुनिया", &root, default_options), "नमस्ते दुनिया");
/// assert_eq!(cm.titlecase_segment_with_only_case_data_to_string("Привет мир", &root, default_options), "Привет мир");
///
/// // Some behavior is language-sensitive
/// assert_eq!(cm.titlecase_segment_with_only_case_data_to_string("istanbul", &root, default_options), "Istanbul");
/// assert_eq!(cm.titlecase_segment_with_only_case_data_to_string("istanbul", &langid!("tr"), default_options), "İstanbul"); // Turkish dotted i
///
/// assert_eq!(cm.titlecase_segment_with_only_case_data_to_string("և Երևանի", &root, default_options), "Եւ երևանի");
/// assert_eq!(cm.titlecase_segment_with_only_case_data_to_string("և Երևանի", &langid!("hy"), default_options), "Եվ երևանի"); // Eastern Armenian ech-yiwn ligature
///
/// assert_eq!(cm.titlecase_segment_with_only_case_data_to_string("ijkdijk", &root, default_options), "Ijkdijk");
/// assert_eq!(cm.titlecase_segment_with_only_case_data_to_string("ijkdijk", &langid!("nl"), default_options), "IJkdijk"); // Dutch IJ digraph
/// ```
///
/// [`TitlecaseMapper`]: crate::TitlecaseMapper
pub fn titlecase_segment_with_only_case_data_to_string(
&self,
src: &str,
langid: &LanguageIdentifier,
options: TitlecaseOptions,
) -> String {
self.titlecase_segment_with_only_case_data(src, langid, options)
.write_to_string()
.into_owned()
}
/// Case-folds the characters in the given string as a String.
/// This function is locale-independent and context-insensitive.
///
/// Can be used to test if two strings are case-insensitively equivalent.
///
/// See [`Self::fold()`] for the equivalent lower-level function that returns a [`Writeable`]
///s s
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
///
/// let cm = CaseMapper::new();
///
/// // Check if two strings are equivalent case insensitively
/// assert_eq!(cm.fold_string("hEllO WorLd"), cm.fold_string("HELLO worlD"));
///
/// assert_eq!(cm.fold_string("hEllO WorLd"), "hello world");
/// assert_eq!(cm.fold_string("Γειά σου Κόσμε"), "γειά σου κόσμε");
/// assert_eq!(cm.fold_string("नमस्ते दुनिया"), "नमस्ते दुनिया");
/// assert_eq!(cm.fold_string("Привет мир"), "привет мир");
/// ```
pub fn fold_string(&self, src: &str) -> String {
self.fold(src).write_to_string().into_owned()
}
/// Case-folds the characters in the given string as a String,
/// using Turkic (T) mappings for dotted/dotless I.
/// This function is locale-independent and context-insensitive.
///
/// Can be used to test if two strings are case-insensitively equivalent.
///
/// See [`Self::fold_turkic()`] for the equivalent lower-level function that returns a [`Writeable`]
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
///
/// let cm = CaseMapper::new();
///
/// // Check if two strings are equivalent case insensitively
/// assert_eq!(cm.fold_turkic_string("İstanbul"), cm.fold_turkic_string("iSTANBUL"));
///
/// assert_eq!(cm.fold_turkic_string("İstanbul not Constantinople"), "istanbul not constantinople");
/// assert_eq!(cm.fold_turkic_string("Istanbul not Constantınople"), "ıstanbul not constantınople");
///
/// assert_eq!(cm.fold_turkic_string("hEllO WorLd"), "hello world");
/// assert_eq!(cm.fold_turkic_string("Γειά σου Κόσμε"), "γειά σου κόσμε");
/// assert_eq!(cm.fold_turkic_string("नमस्ते दुनिया"), "नमस्ते दुनिया");
/// assert_eq!(cm.fold_turkic_string("Привет мир"), "привет мир");
/// ```
pub fn fold_turkic_string(&self, src: &str) -> String {
self.fold_turkic(src).write_to_string().into_owned()
}
/// Adds all simple case mappings and the full case folding for `c` to `set`.
/// Also adds special case closure mappings.
///
/// Identical to [`CaseMapCloser::add_case_closure_to()`], see docs there for more information.
/// This method is duplicated so that one does not need to load extra unfold data
/// if they only need this and not also [`CaseMapCloser::add_string_case_closure_to()`].
///
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
/// use icu::collections::codepointinvlist::CodePointInversionListBuilder;
///
/// let cm = CaseMapper::new();
/// let mut builder = CodePointInversionListBuilder::new();
/// cm.add_case_closure_to('s', &mut builder);
///
/// let set = builder.build();
///
/// assert!(set.contains('S'));
/// assert!(set.contains('ſ'));
/// assert!(!set.contains('s')); // does not contain itself
/// ```
///
/// [`CaseMapCloser::add_case_closure_to()`]: crate::CaseMapCloser::add_case_closure_to
/// [`CaseMapCloser::add_string_case_closure_to()`]: crate::CaseMapCloser::add_string_case_closure_to
pub fn add_case_closure_to<S: ClosureSink>(&self, c: char, set: &mut S) {
self.data.get().add_case_closure_to(c, set);
}
/// Returns the lowercase mapping of the given `char`.
/// This function only implements simple and common mappings. Full mappings,
/// which can map one `char` to a string, are not included.
/// For full mappings, use [`CaseMapper::lowercase`].
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
///
/// let cm = CaseMapper::new();
///
/// assert_eq!(cm.simple_lowercase('C'), 'c');
/// assert_eq!(cm.simple_lowercase('c'), 'c');
/// assert_eq!(cm.simple_lowercase('Ć'), 'ć');
/// assert_eq!(cm.simple_lowercase('Γ'), 'γ');
/// ```
pub fn simple_lowercase(&self, c: char) -> char {
self.data.get().simple_lower(c)
}
/// Returns the uppercase mapping of the given `char`.
/// This function only implements simple and common mappings. Full mappings,
/// which can map one `char` to a string, are not included.
/// For full mappings, use [`CaseMapper::uppercase`].
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
///
/// let cm = CaseMapper::new();
///
/// assert_eq!(cm.simple_uppercase('c'), 'C');
/// assert_eq!(cm.simple_uppercase('C'), 'C');
/// assert_eq!(cm.simple_uppercase('ć'), 'Ć');
/// assert_eq!(cm.simple_uppercase('γ'), 'Γ');
///
/// assert_eq!(cm.simple_uppercase('dz'), 'DZ');
/// ```
pub fn simple_uppercase(&self, c: char) -> char {
self.data.get().simple_upper(c)
}
/// Returns the titlecase mapping of the given `char`.
/// This function only implements simple and common mappings. Full mappings,
/// which can map one `char` to a string, are not included.
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
///
/// let cm = CaseMapper::new();
///
/// assert_eq!(cm.simple_titlecase('dz'), 'Dz');
///
/// assert_eq!(cm.simple_titlecase('c'), 'C');
/// assert_eq!(cm.simple_titlecase('C'), 'C');
/// assert_eq!(cm.simple_titlecase('ć'), 'Ć');
/// assert_eq!(cm.simple_titlecase('γ'), 'Γ');
/// ```
pub fn simple_titlecase(&self, c: char) -> char {
self.data.get().simple_title(c)
}
/// Returns the simple case folding of the given char.
/// For full mappings, use [`CaseMapper::fold`].
///
/// This function can be used to perform caseless matches on
/// individual characters.
/// > *Note:* With Unicode 15.0 data, there are three
/// > pairs of characters for which equivalence under this
/// > function is inconsistent with equivalence of the
/// > one-character strings under [`CaseMapper::fold`].
/// > This is resolved in Unicode 15.1 and later.
///
/// For compatibility applications where simple case folding
/// of strings is required, this function can be applied to
/// each character of a string. Note that the resulting
/// equivalence relation is different from that obtained
/// by [`CaseMapper::fold`]:
/// The strings "Straße" and "STRASSE" are distinct
/// under simple case folding, but are equivalent under
/// default (full) case folding.
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
///
/// let cm = CaseMapper::new();
///
/// // perform case insensitive checks
/// assert_eq!(cm.simple_fold('σ'), cm.simple_fold('ς'));
/// assert_eq!(cm.simple_fold('Σ'), cm.simple_fold('ς'));
///
/// assert_eq!(cm.simple_fold('c'), 'c');
/// assert_eq!(cm.simple_fold('Ć'), 'ć');
/// assert_eq!(cm.simple_fold('Γ'), 'γ');
/// assert_eq!(cm.simple_fold('ς'), 'σ');
///
/// assert_eq!(cm.simple_fold('ß'), 'ß');
/// assert_eq!(cm.simple_fold('I'), 'i');
/// assert_eq!(cm.simple_fold('İ'), 'İ');
/// assert_eq!(cm.simple_fold('ı'), 'ı');
/// ```
pub fn simple_fold(&self, c: char) -> char {
self.data.get().simple_fold(c, FoldOptions::default())
}
/// Returns the simple case folding of the given char, using Turkic (T) mappings for
/// dotted/dotless i. This function does not fold `i` and `I` to the same character. Instead,
/// `I` will fold to `ı`, and `İ` will fold to `i`. Otherwise, this is the same as
/// [`CaseMapper::fold()`].
///
/// You can use the case folding to perform Turkic caseless matches on characters
/// provided they don't full-casefold to strings. To avoid that situation,
/// convert to a string and use [`CaseMapper::fold_turkic`].
///
///
/// # Examples
///
/// ```rust
/// use icu::casemap::CaseMapper;
///
/// let cm = CaseMapper::new();
///
/// assert_eq!(cm.simple_fold_turkic('I'), 'ı');
/// assert_eq!(cm.simple_fold_turkic('İ'), 'i');
/// ```
pub fn simple_fold_turkic(&self, c: char) -> char {
self.data
.get()
.simple_fold(c, FoldOptions::with_turkic_mappings())
}
}
#[cfg(test)]
mod tests {
use super::*;
use icu_locid::langid;
#[test]
/// Tests for SpecialCasing.txt. Some of the special cases are data-driven, some are code-driven
fn test_special_cases() {
let cm = CaseMapper::new();
let root = langid!("und");
let default_options = Default::default();
// Ligatures
// U+FB00 LATIN SMALL LIGATURE FF
assert_eq!(cm.uppercase_to_string("ff", &root), "FF");
// U+FB05 LATIN SMALL LIGATURE LONG S T
assert_eq!(cm.uppercase_to_string("ſt", &root), "ST");
// No corresponding uppercased character
// U+0149 LATIN SMALL LETTER N PRECEDED BY APOSTROPHE
assert_eq!(cm.uppercase_to_string("ʼn", &root), "ʼN");
// U+1F50 GREEK SMALL LETTER UPSILON WITH PSILI
assert_eq!(cm.uppercase_to_string("ὐ", &root), "Υ̓");
// U+1FF6 GREEK SMALL LETTER OMEGA WITH PERISPOMENI
assert_eq!(cm.uppercase_to_string("ῶ", &root), "Ω͂");
// YPOGEGRAMMENI / PROSGEGRAMMENI special cases
// E.g. <alpha><iota_subscript><acute> is uppercased to <ALPHA><acute><IOTA>
assert_eq!(
cm.uppercase_to_string("α\u{0313}\u{0345}", &root),
"Α\u{0313}Ι"
);
// but the YPOGEGRAMMENI should not titlecase
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string(
"α\u{0313}\u{0345}",
&root,
default_options
),
"Α\u{0313}\u{0345}"
);
// U+1F80 GREEK SMALL LETTER ALPHA WITH PSILI AND YPOGEGRAMMENI
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("ᾀ", &root, default_options),
"ᾈ"
);
assert_eq!(cm.uppercase_to_string("ᾀ", &root), "ἈΙ");
// U+1FFC GREEK CAPITAL LETTER OMEGA WITH PROSGEGRAMMENI
assert_eq!(cm.lowercase_to_string("ῼ", &root), "ῳ");
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("ῼ", &root, default_options),
"ῼ"
);
assert_eq!(cm.uppercase_to_string("ῼ", &root), "ΩΙ");
// U+1F98 GREEK CAPITAL LETTER ETA WITH PSILI AND PROSGEGRAMMENI
assert_eq!(cm.lowercase_to_string("ᾘ", &root), "ᾐ");
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("ᾘ", &root, default_options),
"ᾘ"
);
assert_eq!(cm.uppercase_to_string("ᾘ", &root), "ἨΙ");
// U+1FB2 GREEK SMALL LETTER ALPHA WITH VARIA AND YPOGEGRAMMENI
assert_eq!(cm.lowercase_to_string("ᾲ", &root), "ᾲ");
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("ᾲ", &root, default_options),
"Ὰ\u{345}"
);
assert_eq!(cm.uppercase_to_string("ᾲ", &root), "ᾺΙ");
// Final sigma test
// U+03A3 GREEK CAPITAL LETTER SIGMA in Final_Sigma context
assert_eq!(cm.lowercase_to_string("ΙΙΙΣ", &root), "ιιις");
// Turkish / Azeri
let tr = langid!("tr");
let az = langid!("az");
// U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE
assert_eq!(cm.lowercase_to_string("İ", &tr), "i");
assert_eq!(cm.lowercase_to_string("İ", &az), "i");
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("İ", &tr, default_options),
"İ"
);
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("İ", &az, default_options),
"İ"
);
assert_eq!(cm.uppercase_to_string("İ", &tr), "İ");
assert_eq!(cm.uppercase_to_string("İ", &az), "İ");
// U+0049 LATIN CAPITAL LETTER I and U+0307 COMBINING DOT ABOVE
assert_eq!(cm.lowercase_to_string("I\u{0307}", &tr), "i");
assert_eq!(cm.lowercase_to_string("I\u{0307}", &az), "i");
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("I\u{0307}", &tr, default_options),
"I\u{0307}"
);
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("I\u{0307}", &az, default_options),
"I\u{0307}"
);
assert_eq!(cm.uppercase_to_string("I\u{0307}", &tr), "I\u{0307}");
assert_eq!(cm.uppercase_to_string("I\u{0307}", &az), "I\u{0307}");
// U+0049 LATIN CAPITAL LETTER I
assert_eq!(cm.lowercase_to_string("I", &tr), "ı");
assert_eq!(cm.lowercase_to_string("I", &az), "ı");
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("I", &tr, default_options),
"I"
);
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("I", &az, default_options),
"I"
);
assert_eq!(cm.uppercase_to_string("I", &tr), "I");
assert_eq!(cm.uppercase_to_string("I", &az), "I");
// U+0069 LATIN SMALL LETTER I
assert_eq!(cm.lowercase_to_string("i", &tr), "i");
assert_eq!(cm.lowercase_to_string("i", &az), "i");
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("i", &tr, default_options),
"İ"
);
assert_eq!(
cm.titlecase_segment_with_only_case_data_to_string("i", &az, default_options),
"İ"
);
assert_eq!(cm.uppercase_to_string("i", &tr), "İ");
assert_eq!(cm.uppercase_to_string("i", &az), "İ");
}
#[test]
fn test_cherokee_case_folding() {
let case_mapping = CaseMapper::new();
assert_eq!(case_mapping.simple_fold('Ꭰ'), 'Ꭰ');
assert_eq!(case_mapping.simple_fold('ꭰ'), 'Ꭰ');
assert_eq!(case_mapping.simple_fold_turkic('Ꭰ'), 'Ꭰ');
assert_eq!(case_mapping.simple_fold_turkic('ꭰ'), 'Ꭰ');
assert_eq!(case_mapping.fold_string("Ꭰ"), "Ꭰ");
assert_eq!(case_mapping.fold_string("ꭰ"), "Ꭰ");
assert_eq!(case_mapping.fold_turkic_string("Ꭰ"), "Ꭰ");
assert_eq!(case_mapping.fold_turkic_string("ꭰ"), "Ꭰ");
}
}