Skip to content

Commit bea7fdf

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 bea7fdf

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

uefi/src/data_types/chars.rs

+26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
66
use core::fmt::{self, Display, Formatter};
77

8+
use alloc::vec::Vec;
9+
810
/// Character conversion error
911
#[derive(Clone, Copy, Debug)]
1012
pub struct CharConversionError;
@@ -83,6 +85,30 @@ impl Char16 {
8385
pub const unsafe fn from_u16_unchecked(val: u16) -> Self {
8486
Self(val)
8587
}
88+
89+
/// Checks if the value is within the ASCII range.
90+
#[must_use]
91+
pub const fn is_ascii(&self) -> bool {
92+
self.0 <= 127
93+
}
94+
}
95+
96+
/// Provides various functions on slice-like container (e.g. Vec) of Char16.
97+
pub trait SliceLikeChar16 {
98+
/// Checks if all char16 in this slice are within the ASCII range.
99+
fn is_ascii(&self) -> bool;
100+
}
101+
102+
impl SliceLikeChar16 for [Char16] {
103+
fn is_ascii(&self) -> bool {
104+
self.iter().all(|c| c.is_ascii())
105+
}
106+
}
107+
108+
impl SliceLikeChar16 for Vec<Char16> {
109+
fn is_ascii(&self) -> bool {
110+
self.iter().all(|c| c.is_ascii())
111+
}
86112
}
87113

88114
impl TryFrom<char> for Char16 {

uefi/src/data_types/owned_strs.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::chars::{Char16, NUL_16};
1+
use super::chars::{Char16, NUL_16, SliceLikeChar16};
22
use super::strs::{CStr16, FromSliceWithNulError};
33
use crate::data_types::strs::EqStrUntilNul;
44
use crate::data_types::UnalignedSlice;
@@ -109,6 +109,12 @@ impl CString16 {
109109
pub fn is_empty(&self) -> bool {
110110
self.num_chars() == 0
111111
}
112+
113+
/// Checks if all characters in this string are within the ASCII range.
114+
#[must_use]
115+
pub fn is_ascii(&self) -> bool {
116+
self.0.is_ascii()
117+
}
112118
}
113119

114120
impl Default for CString16 {

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, SliceLikeChar16};
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)