From 98f3c055f9bd9203ef10fdfee50b1960f0932fa0 Mon Sep 17 00:00:00 2001 From: Xiaoyu Lu Date: Tue, 29 Dec 2020 16:12:07 +0800 Subject: [PATCH] efi-str add string compare --- efi-str/src/lib.rs | 8 ++++++++ efi-str/src/os_str.rs | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/efi-str/src/lib.rs b/efi-str/src/lib.rs index 29762668..277c82c4 100644 --- a/efi-str/src/lib.rs +++ b/efi-str/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(test), no_std)] +#![feature(unicode_internals)] + pub mod encoder; #[macro_use] @@ -28,5 +30,11 @@ mod tests { assert_eq!(path_osstr_nul, "中国\0"); assert_ne!(path_osstr, path_osstr_nul); assert_ne!("中1", path_osstr); + + let str_lower = "hi"; + let str_upper = [0x48, 0x49]; + let str_upper = OsStr::from_u16_slice(&str_upper); + + assert_eq!(str_upper.eq_ignore_ascii_case(str_lower), true); } } diff --git a/efi-str/src/os_str.rs b/efi-str/src/os_str.rs index 4f11acb6..59ebc8af 100644 --- a/efi-str/src/os_str.rs +++ b/efi-str/src/os_str.rs @@ -1,5 +1,6 @@ use core::fmt; use core::slice::Iter; +use core::unicode::conversions; pub struct OsStr([u16]); @@ -92,6 +93,21 @@ impl OsStr { } res } + + pub fn eq_ignore_ascii_case(&self, other: &str) -> bool { + if self.0.len() == other.chars().count() { + let mut i = 0; + for c in other.chars() { + if conversions::to_lower(c)[0] as u32 == self.0[i] as u32 || conversions::to_upper(c)[0] as u32 == self.0[i] as u32 { + i += 1; + } else { + return false; + } + } + return true; + } + return false; + } } impl fmt::Debug for &OsStr {