Skip to content

Commit 3015053

Browse files
Add char::is_titlecase
1 parent ea8bc6f commit 3015053

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

library/core/src/char/methods.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,39 @@ impl char {
822822
}
823823
}
824824

825+
/// Returns `true` if this `char` has the general category for titlecase letters.
826+
///
827+
/// Titlecase letters (code points with the general category of `Lt`) are described in Chapter 4
828+
/// (Character Properties) of the [Unicode Standard] and specified in the [Unicode Character
829+
/// Database][ucd] [`UnicodeData.txt`].
830+
///
831+
/// [Unicode Standard]: https://www.unicode.org/versions/latest/
832+
/// [ucd]: https://www.unicode.org/reports/tr44/
833+
/// [`UnicodeData.txt`]: https://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt
834+
///
835+
/// # Examples
836+
///
837+
/// Basic usage:
838+
///
839+
/// ```
840+
/// #![feature(titlecase)]
841+
/// assert!('Dž'.is_titlecase());
842+
/// assert!('ᾨ'.is_titlecase());
843+
/// assert!(!'D'.is_titlecase());
844+
/// assert!(!'z'.is_titlecase());
845+
/// assert!(!'中'.is_titlecase());
846+
/// assert!(!' '.is_titlecase());
847+
/// ```
848+
#[must_use]
849+
#[unstable(feature = "titlecase", issue = "none")]
850+
#[inline]
851+
pub fn is_titlecase(self) -> bool {
852+
match self {
853+
'\0'..='\u{01C4}' => false,
854+
_ => self.is_cased() && !self.is_lowercase() && !self.is_uppercase(),
855+
}
856+
}
857+
825858
/// Returns `true` if this `char` has the `Uppercase` property.
826859
///
827860
/// `Uppercase` is described in Chapter 4 (Character Properties) of the [Unicode Standard] and

library/core/tests/char.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ fn test_is_lowercase() {
6161
assert!(!'P'.is_lowercase());
6262
}
6363

64+
#[test]
65+
fn test_is_titlecase() {
66+
assert!('Dž'.is_titlecase());
67+
assert!('ᾨ'.is_titlecase());
68+
assert!(!'h'.is_titlecase());
69+
assert!(!'ä'.is_titlecase());
70+
assert!(!'ß'.is_titlecase());
71+
assert!(!'Ö'.is_titlecase());
72+
assert!(!'T'.is_titlecase());
73+
}
74+
6475
#[test]
6576
fn test_is_uppercase() {
6677
assert!(!'h'.is_uppercase());
@@ -70,6 +81,26 @@ fn test_is_uppercase() {
7081
assert!('T'.is_uppercase());
7182
}
7283

84+
#[test]
85+
fn titlecase_fast_path() {
86+
for c in '\0'..='\u{01C4}' {
87+
assert!(!(c.is_cased() && !c.is_lowercase() && !c.is_uppercase()))
88+
}
89+
}
90+
91+
#[test]
92+
fn at_most_one_case() {
93+
for c in '\0'..='\u{10FFFF}' {
94+
assert_eq!(
95+
!c.is_cased() as u8
96+
+ c.is_lowercase() as u8
97+
+ c.is_uppercase() as u8
98+
+ c.is_titlecase() as u8,
99+
1
100+
);
101+
}
102+
}
103+
73104
#[test]
74105
fn test_is_whitespace() {
75106
assert!(' '.is_whitespace());

0 commit comments

Comments
 (0)