Skip to content

Commit 9a6930f

Browse files
Add CodePointInversionListAndStringList::contains_utf8 (#7363)
For #7358
1 parent 51836a8 commit 9a6930f

File tree

8 files changed

+37
-8
lines changed

8 files changed

+37
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/collections/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ yoke = { workspace = true, features = ["derive"] }
2525
zerofrom = { workspace = true, features = ["derive"] }
2626
zerovec = { workspace = true, features = ["derive", "yoke"] }
2727
potential_utf = { workspace = true, features = ["zerovec"] }
28+
utf8_iter = { workspace = true }
2829

2930
serde = { workspace = true, features = ["derive"], optional = true }
3031
databake = { workspace = true, features = ["derive"], optional = true }

components/collections/src/codepointinvliststringlist/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ impl<'data> CodePointInversionListAndStringList<'data> {
173173
self.str_list.binary_search(s).is_ok()
174174
}
175175

176+
/// See [`Self::contains_str`]
177+
pub fn contains_utf8(&self, s: &[u8]) -> bool {
178+
use utf8_iter::Utf8CharsEx;
179+
let mut chars = s.chars();
180+
if let Some(first_char) = chars.next() {
181+
if chars.next().is_none() {
182+
return self.contains(first_char);
183+
}
184+
}
185+
self.str_list
186+
.binary_search_by(|t| t.as_bytes().cmp(s))
187+
.is_ok()
188+
}
189+
176190
///
177191
/// # Examples
178192
/// ```

components/properties/src/emoji.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ impl EmojiSetDataBorrowed<'_> {
118118
self.set.contains_str(s)
119119
}
120120

121+
/// See [`Self::contains_str`].
122+
#[inline]
123+
pub fn contains_utf8(self, s: &[u8]) -> bool {
124+
self.set.contains_utf8(s)
125+
}
126+
121127
/// Check if the set contains the code point.
122128
#[inline]
123129
pub fn contains(self, ch: char) -> bool {

components/properties/src/provider.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,13 @@ impl<'data> PropertyUnicodeSet<'data> {
11131113
}
11141114
}
11151115

1116+
#[inline]
1117+
pub(crate) fn contains_utf8(&self, s: &[u8]) -> bool {
1118+
match *self {
1119+
Self::CPInversionListStrList(ref l) => l.contains_utf8(s),
1120+
}
1121+
}
1122+
11161123
#[inline]
11171124
pub(crate) fn contains32(&self, cp: u32) -> bool {
11181125
match *self {

ffi/capi/src/exemplar_chars.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ pub mod ffi {
2727
)]
2828
#[diplomat::attr(supports = method_overloading, rename = "contains")]
2929
pub fn contains_str(&self, s: &DiplomatStr) -> bool {
30-
let Ok(s) = core::str::from_utf8(s) else {
31-
return false;
32-
};
33-
self.0.as_borrowed().contains_str(s)
30+
self.0.as_borrowed().contains_utf8(s)
3431
}
3532
/// Checks whether the code point is in the set.
3633
#[diplomat::rust_link(

ffi/capi/src/properties_unisets.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ pub mod ffi {
2424
impl EmojiSetData {
2525
/// Checks whether the string is in the set.
2626
#[diplomat::rust_link(icu::properties::EmojiSetDataBorrowed::contains_str, FnInStruct)]
27+
#[diplomat::rust_link(
28+
icu::properties::EmojiSetDataBorrowed::contains_utf8,
29+
FnInStruct,
30+
hidden
31+
)]
2732
#[diplomat::attr(supports = method_overloading, rename = "contains")]
2833
pub fn contains_str(&self, s: &DiplomatStr) -> bool {
29-
let Ok(s) = core::str::from_utf8(s) else {
30-
return false;
31-
};
32-
self.0.as_borrowed().contains_str(s)
34+
self.0.as_borrowed().contains_utf8(s)
3335
}
3436
/// Checks whether the code point is in the set.
3537
#[diplomat::rust_link(icu::properties::EmojiSetDataBorrowed::contains, FnInStruct)]

utils/host_info/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)