Skip to content

Commit

Permalink
uefi(data-types): allow is_ascii function on Char16 and CStr16
Browse files Browse the repository at this point in the history
Offers a way to know if a certain UTF-16 string contains only ASCII characters.
  • Loading branch information
RaitoBezarius committed Nov 15, 2023
1 parent 06c300e commit 712629b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions uefi/src/data_types/chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> for Char16 {
Expand Down
6 changes: 6 additions & 0 deletions uefi/src/data_types/strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down

0 comments on commit 712629b

Please sign in to comment.