diff options
| author | bors <bors@rust-lang.org> | 2025-08-30 14:18:21 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-08-30 14:18:21 +0000 |
| commit | 0f506968010fa987b0d134034d0ccab9eecd9120 (patch) | |
| tree | 53abfd8e7b5abaa7f36904768540805de617850c /library | |
| parent | e95db591a4550e28ad92660b753ad85b89271882 (diff) | |
| parent | 1bb9b151c9f9b5116254827f04add845aff33408 (diff) | |
| download | rust-0f506968010fa987b0d134034d0ccab9eecd9120.tar.gz rust-0f506968010fa987b0d134034d0ccab9eecd9120.zip | |
Auto merge of #145479 - Kmeakin:km/hardcode-char-is-control, r=joboet
Hard-code `char::is_control` Split off from https://github.com/rust-lang/rust/pull/145219 According to https://www.unicode.org/policies/stability_policy.html#Property_Value, the set of codepoints in `Cc` will never change. So we can hard-code the patterns to match against instead of using a table. This doesn't change the generated assembly, since the lookup table is small enough that[ LLVM is able to inline the whole search](https://godbolt.org/z/bG8dM37YG). But this does reduce the chance of regressions if LLVM's heuristics change in the future, and means less generated Rust code checked in to `unicode-data.rs`.
Diffstat (limited to 'library')
| -rw-r--r-- | library/core/src/char/methods.rs | 6 | ||||
| -rw-r--r-- | library/core/src/unicode/mod.rs | 1 | ||||
| -rw-r--r-- | library/core/src/unicode/unicode_data.rs | 25 |
3 files changed, 5 insertions, 27 deletions
diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 985e669c92d..3336d028e27 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -950,7 +950,11 @@ impl char { #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn is_control(self) -> bool { - unicode::Cc(self) + // According to + // https://www.unicode.org/policies/stability_policy.html#Property_Value, + // the set of codepoints in `Cc` will never change. + // So we can just hard-code the patterns to match against instead of using a table. + matches!(self, '\0'..='\x1f' | '\x7f'..='\u{9f}') } /// Returns `true` if this `char` has the `Grapheme_Extend` property. diff --git a/library/core/src/unicode/mod.rs b/library/core/src/unicode/mod.rs index 191fe7711f9..c71fa754e68 100644 --- a/library/core/src/unicode/mod.rs +++ b/library/core/src/unicode/mod.rs @@ -10,7 +10,6 @@ pub use unicode_data::conversions; #[rustfmt::skip] pub(crate) use unicode_data::alphabetic::lookup as Alphabetic; -pub(crate) use unicode_data::cc::lookup as Cc; pub(crate) use unicode_data::grapheme_extend::lookup as Grapheme_Extend; pub(crate) use unicode_data::lowercase::lookup as Lowercase; pub(crate) use unicode_data::n::lookup as N; diff --git a/library/core/src/unicode/unicode_data.rs b/library/core/src/unicode/unicode_data.rs index b57234bbee9..55f64f1e96e 100644 --- a/library/core/src/unicode/unicode_data.rs +++ b/library/core/src/unicode/unicode_data.rs @@ -359,31 +359,6 @@ pub mod cased { } #[rustfmt::skip] -pub mod cc { - use super::ShortOffsetRunHeader; - - static SHORT_OFFSET_RUNS: [ShortOffsetRunHeader; 1] = [ - ShortOffsetRunHeader::new(0, 1114272), - ]; - static OFFSETS: [u8; 5] = [ - 0, 32, 95, 33, 0, - ]; - pub fn lookup(c: char) -> bool { - const { - assert!(SHORT_OFFSET_RUNS.last().unwrap().0 > char::MAX as u32); - let mut i = 0; - while i < SHORT_OFFSET_RUNS.len() { - assert!(SHORT_OFFSET_RUNS[i].start_index() < OFFSETS.len()); - i += 1; - } - } - // SAFETY: We just ensured the last element of `SHORT_OFFSET_RUNS` is greater than `std::char::MAX` - // and the start indices of all elements in `SHORT_OFFSET_RUNS` are smaller than `OFFSETS.len()`. - unsafe { super::skip_search(c, &SHORT_OFFSET_RUNS, &OFFSETS) } - } -} - -#[rustfmt::skip] pub mod grapheme_extend { use super::ShortOffsetRunHeader; |
