Skip to content

Commit a9dc6a7

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 09a7867 commit a9dc6a7

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

uefi/src/data_types/chars.rs

+17
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ 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+
}
93+
94+
pub trait SliceChar16 {
95+
/// Checks if all char16 in this slice are within the ASCII range.
96+
fn is_ascii(&self) -> bool;
97+
}
98+
99+
impl SliceChar16 for [Char16] {
100+
fn is_ascii(&self) -> bool {
101+
self.into_iter().all(|c| c.is_ascii())
102+
}
86103
}
87104

88105
impl TryFrom<char> for Char16 {

uefi/src/data_types/strs.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::chars::{Char16, Char8, NUL_16, NUL_8};
1+
use super::chars::{Char16, Char8, NUL_16, NUL_8, SliceChar16};
22
use super::UnalignedSlice;
33
use crate::polyfill::maybe_uninit_slice_assume_init_ref;
44
use core::borrow::Borrow;
@@ -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.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)