From 712629bdc33afaf0fce2663846808b1e6c5b4afb Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Wed, 8 Nov 2023 20:13:56 +0100 Subject: [PATCH] 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. --- uefi/src/data_types/chars.rs | 6 ++++++ uefi/src/data_types/strs.rs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/uefi/src/data_types/chars.rs b/uefi/src/data_types/chars.rs index a92c57067..ab270a82f 100644 --- a/uefi/src/data_types/chars.rs +++ b/uefi/src/data_types/chars.rs @@ -83,6 +83,12 @@ impl Char16 { pub const unsafe fn from_u16_unchecked(val: u16) -> Self { Self(val) } + + /// Checks if the value is within the ASCII range. + #[must_use] + pub const fn is_ascii(&self) -> bool { + self.0 <= 127 + } } impl TryFrom for Char16 { diff --git a/uefi/src/data_types/strs.rs b/uefi/src/data_types/strs.rs index 4cfd2f7c4..9e22be51a 100644 --- a/uefi/src/data_types/strs.rs +++ b/uefi/src/data_types/strs.rs @@ -415,6 +415,12 @@ impl CStr16 { self.0.len() * 2 } + /// Checks if all characters in this string are within the ASCII range. + #[must_use] + pub fn is_ascii(&self) -> bool { + self.0.iter().all(|c| c.is_ascii()) + } + /// Writes each [`Char16`] as a [`char`] (4 bytes long in Rust language) into the buffer. /// It is up to the implementer of [`core::fmt::Write`] to convert the char to a string /// with proper encoding/charset. For example, in the case of [`alloc::string::String`]