From bea7fdf7f33079ab24f619db790090c983c6762d Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Wed, 8 Nov 2023 20:13:56 +0100 Subject: [PATCH 1/2] 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 | 26 ++++++++++++++++++++++++++ uefi/src/data_types/owned_strs.rs | 8 +++++++- uefi/src/data_types/strs.rs | 8 +++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/uefi/src/data_types/chars.rs b/uefi/src/data_types/chars.rs index a92c57067..be5612d4f 100644 --- a/uefi/src/data_types/chars.rs +++ b/uefi/src/data_types/chars.rs @@ -5,6 +5,8 @@ use core::fmt::{self, Display, Formatter}; +use alloc::vec::Vec; + /// Character conversion error #[derive(Clone, Copy, Debug)] pub struct CharConversionError; @@ -83,6 +85,30 @@ 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 + } +} + +/// Provides various functions on slice-like container (e.g. Vec) of Char16. +pub trait SliceLikeChar16 { + /// Checks if all char16 in this slice are within the ASCII range. + fn is_ascii(&self) -> bool; +} + +impl SliceLikeChar16 for [Char16] { + fn is_ascii(&self) -> bool { + self.iter().all(|c| c.is_ascii()) + } +} + +impl SliceLikeChar16 for Vec { + fn is_ascii(&self) -> bool { + self.iter().all(|c| c.is_ascii()) + } } impl TryFrom for Char16 { diff --git a/uefi/src/data_types/owned_strs.rs b/uefi/src/data_types/owned_strs.rs index 8e673c2f2..bc2274f4d 100644 --- a/uefi/src/data_types/owned_strs.rs +++ b/uefi/src/data_types/owned_strs.rs @@ -1,4 +1,4 @@ -use super::chars::{Char16, NUL_16}; +use super::chars::{Char16, NUL_16, SliceLikeChar16}; use super::strs::{CStr16, FromSliceWithNulError}; use crate::data_types::strs::EqStrUntilNul; use crate::data_types::UnalignedSlice; @@ -109,6 +109,12 @@ impl CString16 { pub fn is_empty(&self) -> bool { self.num_chars() == 0 } + + /// Checks if all characters in this string are within the ASCII range. + #[must_use] + pub fn is_ascii(&self) -> bool { + self.0.is_ascii() + } } impl Default for CString16 { diff --git a/uefi/src/data_types/strs.rs b/uefi/src/data_types/strs.rs index 4cfd2f7c4..863a57855 100644 --- a/uefi/src/data_types/strs.rs +++ b/uefi/src/data_types/strs.rs @@ -1,4 +1,4 @@ -use super::chars::{Char16, Char8, NUL_16, NUL_8}; +use super::chars::{Char16, Char8, NUL_16, NUL_8, SliceLikeChar16}; use super::UnalignedSlice; use crate::polyfill::maybe_uninit_slice_assume_init_ref; use core::borrow::Borrow; @@ -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.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`] From 38d87ad2fde50e47add24c992fbf20750a863332 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Wed, 8 Nov 2023 20:14:53 +0100 Subject: [PATCH 2/2] uefi(filesystem): make `open` public in `uefi::fs::FileSystem` `open` might a bit too low-level, but it is a powerful API to bridge between high-level structures and low-level structures, it would be a shame to force end-users to reinvent it. --- uefi/src/fs/file_system/fs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uefi/src/fs/file_system/fs.rs b/uefi/src/fs/file_system/fs.rs index d08e1b892..9372d520e 100644 --- a/uefi/src/fs/file_system/fs.rs +++ b/uefi/src/fs/file_system/fs.rs @@ -404,7 +404,7 @@ impl<'a> FileSystem<'a> { /// May create a file if [`UefiFileMode::CreateReadWrite`] is set. May /// create a directory if [`UefiFileMode::CreateReadWrite`] and `create_dir` /// is set. The parameter `create_dir` is ignored otherwise. - fn open( + pub fn open( &mut self, path: &Path, mode: UefiFileMode,