Skip to content

Commit

Permalink
efi-str add string compare
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoyu Lu committed Dec 29, 2020
1 parent f64e5c4 commit 98f3c05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions efi-str/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg_attr(not(test), no_std)]

#![feature(unicode_internals)]

pub mod encoder;

#[macro_use]
Expand Down Expand Up @@ -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);
}
}
16 changes: 16 additions & 0 deletions efi-str/src/os_str.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::fmt;
use core::slice::Iter;
use core::unicode::conversions;

pub struct OsStr([u16]);

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 98f3c05

Please sign in to comment.