Skip to content

Commit ea8bc6f

Browse files
Add char::is_cased
1 parent 18be4ff commit ea8bc6f

File tree

6 files changed

+48
-5
lines changed

6 files changed

+48
-5
lines changed

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
#![feature(std_internals)]
161161
#![feature(str_internals)]
162162
#![feature(strict_provenance)]
163+
#![feature(titlecase)]
163164
#![feature(trusted_fused)]
164165
#![feature(trusted_len)]
165166
#![feature(trusted_random_access)]

library/alloc/src/str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,9 @@ impl str {
410410
}
411411

412412
fn case_ignorable_then_cased<I: Iterator<Item = char>>(iter: I) -> bool {
413-
use core::unicode::{Case_Ignorable, Cased};
413+
use core::unicode::Case_Ignorable;
414414
match iter.skip_while(|&c| Case_Ignorable(c)).next() {
415-
Some(c) => Cased(c),
415+
Some(c) => c.is_cased(),
416416
None => false,
417417
}
418418
}

library/core/src/char/methods.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,37 @@ impl char {
748748
}
749749
}
750750

751+
/// Returns `true` if this `char` has the `Cased` property.
752+
/// A character is cased if and only if it is uppercase, lowercase, or titlecase.
753+
///
754+
/// `Cased` is described in Chapter 3 (Conformance) of the [Unicode Standard] and
755+
/// specified in the [Unicode Character Database][ucd] [`DerivedCoreProperties.txt`].
756+
///
757+
/// [Unicode Standard]: https://www.unicode.org/versions/latest/
758+
/// [ucd]: https://www.unicode.org/reports/tr44/
759+
/// [`DerivedCoreProperties.txt`]: https://www.unicode.org/Public/UCD/latest/ucd/DerivedCoreProperties.txt
760+
///
761+
/// # Examples
762+
///
763+
/// Basic usage:
764+
///
765+
/// ```
766+
/// #![feature(titlecase)]
767+
/// assert!('A'.is_cased());
768+
/// assert!('a'.is_cased());
769+
/// assert!(!'京'.is_cased());
770+
/// ```
771+
#[must_use]
772+
#[unstable(feature = "titlecase", issue = "none")]
773+
#[inline]
774+
pub fn is_cased(self) -> bool {
775+
match self {
776+
'A'..='Z' | 'a'..='z' => true,
777+
'\0'..='\u{A9}' => false,
778+
_ => unicode::Cased(self),
779+
}
780+
}
781+
751782
/// Returns `true` if this `char` has the `Lowercase` property.
752783
///
753784
/// `Lowercase` is described in Chapter 4 (Character Properties) of the [Unicode Standard] and

library/core/src/unicode/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ mod unicode_data;
1818
pub const UNICODE_VERSION: (u8, u8, u8) = unicode_data::UNICODE_VERSION;
1919

2020
// For use in alloc, not re-exported in std.
21-
pub use unicode_data::{
22-
case_ignorable::lookup as Case_Ignorable, cased::lookup as Cased, conversions,
23-
};
21+
pub use unicode_data::{case_ignorable::lookup as Case_Ignorable, conversions};
2422

2523
pub(crate) use unicode_data::alphabetic::lookup as Alphabetic;
24+
pub(crate) use unicode_data::cased::lookup as Cased;
2625
pub(crate) use unicode_data::cc::lookup as Cc;
2726
pub(crate) use unicode_data::grapheme_extend::lookup as Grapheme_Extend;
2827
pub(crate) use unicode_data::lowercase::lookup as Lowercase;

library/core/tests/char.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ fn test_from_str() {
4141
assert!(char::from_str("abc").is_err());
4242
}
4343

44+
#[test]
45+
fn test_is_cased() {
46+
assert!('a'.is_cased());
47+
assert!('ö'.is_cased());
48+
assert!('ß'.is_cased());
49+
assert!('Ü'.is_cased());
50+
assert!('P'.is_cased());
51+
assert!('ª'.is_cased());
52+
assert!(!'攂'.is_cased());
53+
}
54+
4455
#[test]
4556
fn test_is_lowercase() {
4657
assert!('a'.is_lowercase());

library/core/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#![feature(str_internals)]
7171
#![feature(std_internals)]
7272
#![feature(test)]
73+
#![feature(titlecase)]
7374
#![feature(trusted_len)]
7475
#![feature(try_blocks)]
7576
#![feature(try_trait_v2)]

0 commit comments

Comments
 (0)