Skip to content

Commit 572315a

Browse files
committed
uefi(data-types): allow is_ascii function on Char16 and CStr16
Offers a way to know if a certain UTF-16 string contains only ASCII characters.
1 parent 06c300e commit 572315a

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Diff for: uefi/src/data_types/chars.rs

+7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ impl Char16 {
8383
pub const unsafe fn from_u16_unchecked(val: u16) -> Self {
8484
Self(val)
8585
}
86+
87+
/// Checks if the value is within the ASCII range.
88+
#[must_use]
89+
pub const fn is_ascii(&self) -> bool {
90+
self.0 <= 127
91+
}
92+
}
8693
}
8794

8895
impl TryFrom<char> for Char16 {

Diff for: uefi/src/data_types/strs.rs

+6
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ impl CStr16 {
415415
self.0.len() * 2
416416
}
417417

418+
/// Checks if all characters in this string are within the ASCII range.
419+
#[must_use]
420+
pub fn is_ascii(&self) -> bool {
421+
self.0.iter().all(|c| c.is_ascii())
422+
}
423+
418424
/// Writes each [`Char16`] as a [`char`] (4 bytes long in Rust language) into the buffer.
419425
/// It is up to the implementer of [`core::fmt::Write`] to convert the char to a string
420426
/// with proper encoding/charset. For example, in the case of [`alloc::string::String`]

0 commit comments

Comments
 (0)